SQL Query Error: Invalid Column Name - sql

I am new to SQL and I have this problem, I can't seem to search on google.
This is my school work so please be patient with my coding.
Now the problem is i am doing a select query and SQL does not seem to recognize the column I need
here is an attached picture, it just says invalid column but it is clear that the column exist
Dropbox link to the picture
here is the complete code
create database Hairsalon
use Hairsalon
create table Employee
(
Employee_ID int primary key,
Name varchar(20),
Birthday date,
Gender char(1)
);
create table Inventory
(
Inventory_ID int primary key,
Name varchar(30),
Stock int
)
create table Record
(
Transaction_Number int primary key,
Transaction_Date date,
Transaction_Time time
);
alter table Record
drop column Transaction_Date
alter table Record
drop column Transaction_Time
alter table Record
add Transaction_DT varchar(30)
alter table Record
add Transaction_Description varchar(255)
Create table Inventory_Record
(
Transaction_Number int primary key foreign key references Record(Transaction_Number),
Inventory_ID int foreign key references Inventory(Inventory_ID),
);
create table Customer_Record
(
Transaction_Number int foreign key references Record(Transaction_Number),
Customer_ID int primary key,
Service_Description varchar(255),
Pay money
);
create table Customer
(
Customer_ID int foreign key references Customer_Record(Customer_ID),
Name varchar(20),
Payable money,
Session_DT varchar(20)
);
create table Employee_Record
(
Transaction_Number int primary key foreign key references Record(Transaction_Number),
Employee_ID int foreign key references Employee(Employee_ID),
Time_In time,
Time_Out time,
Record_Date date,
Customer_Count int
);
create table Salon
(
Customer_ID int foreign key references Customer_Record(Customer_ID),
Employee_ID int foreign key references Employee(Employee_ID),
Inventory_ID int foreign key references Inventory(Inventory_ID),
Transaction_Number int foreign key references Record(Transaction_Number)
);
alter table Customer
add Session_DT varchar(20)
alter table Customer
drop column Customer_ID
alter table Customer
add Customer_ID int foreign key references Customer_Record(Customer_ID)
alter table Customer_Record
add Employee_ID int foreign key references Employee(Employee_ID)
alter table Employee
add [Type] varchar(15)
alter table Employee
drop column Birthday
alter table Employee
drop column Birthday
alter table Employee_Record
drop column Time_In
alter table Employee_Record
drop column Time_Out
alter table Employee_Record
drop column Record_Date
alter table Employee_Record
add Time_In varchar(20)
alter table Employee_Record
add Time_Out varchar(20)
alter table Employee_Record
add Record_Date varchar(20)
alter table Employee_Record
drop column Customer_Count
insert into Employee
values(1,'Test Employee','M','Cashier','bday')
insert into Employee
values(-1,'Null','N','Null','Null')
insert into Employee values(1,'test1','M','HairDresser','9/8/2014')
INSERT INTO Record values(2,'')
INSERT INTO Customer_Record values(1,1,'asd',123.00,null)
INSERT INTO Customer values(1,'test1',0,'9/8/2014 8:16 AM')
SELECT * FROM Record
select * from Customer
SELECT * FROM Customer_Record
SELECT * FROM Employee
SELECT DISTINCT Customer.Name as 'Customer Name', Employee.Name as 'Attendee'
FROM Customer, Customer_Record, Employee_Record, Employee
WHERE ( Customer_Record.Transaction_Number = Employee_Record.Transaction_Number
AND Employee_Record.Employee_ID = Employee.Employee_ID
AND Customer.Customer_ID = Customer_Record.Customer_ID
) OR ( Customer_Record.Transaction_Number = Employee_Record.Transaction_Number
AND Customer_Record.Employee_ID = -1
AND Customer.Customer_ID = Customer_Record.Customer_ID
)
insert into Employee_Record values(9,1,'10:00','10:99','date')
select * from Record
select * from Customer
select * from Employee
select * from Employee_Record
select * from Customer_Record
select count(Customer_ID) from Customer
select count(Transaction_Number) from Record
insert into Customer
values(1,'test',120,DATEFROMPARTS(32,12,31))
INSERT INTO Customer_Record values(1,1,'Long Haired Customer: ',0, null)
DELETE FROM Customer WHERE Customer_ID = 1
DELETE FROM Customer
DELETE FROM Customer_Record
DELETE FROM Record
DELETE FROM Employee
DELETE FROM Employee_Record
DELETE FROM Inventory
DELETE FROM Inventory_Record
commit

According to the DDL you have presented, it seems quite obvious that Customer_Record has no Employee_ID.
You may have mixed up Customer_Record with Employee_Record or Employee_ID with Customer_ID, but something is wrong.
EDIT I had missed that you alter the table in your script to add the missing columns. In that case, Intellisense (the auto-completion system) does not take it into account unless you manually reload it (using Ctrl + Shift + R). Your query is correct and the errors will disappear once Intellisense is up to date.

Related

Only one active for each relationship constraint

So i have this problem with my assignment.
I have two entities: Order, Gifr_cupon.
I have two tables: Orders, Gift_Cupons.
Each order can have many cupons or none. Each Cupon is bound to no or an single order. But only one cupon can be active for each order.
How to enforce this by constraint?
Heres a logical and ER view with DDL:
DLL:
CREATE TABLE gift_cupons (
cupon_id INTEGER NOT NULL,
order_order_id INTEGER,
active INTEGER NOT NULL
);
ALTER TABLE gift_cupons ADD CONSTRAINT gift_cupon_pk PRIMARY KEY ( cupon_id
);
ALTER TABLE gift_cupons ADD CHECK gift_cupon_check CHECK(active IS NULL OR ( active >= 0 AND active <=1 ) );
CREATE TABLE orders (
order_id INTEGER NOT NULL
);
ALTER TABLE orders ADD CONSTRAINT order_pk PRIMARY KEY ( order_id );
ALTER TABLE gift_cupons
ADD CONSTRAINT gift_cupon_order_fk FOREIGN KEY ( order_order_id )
REFERENCES orders ( order_id );
Kind of
Cupon - is bound to -> Order;
Order - has active -> Cupon;
Cupon (
Id PK,
orderId FK Order.Id,
Unique ( Id, orderId) -- any superset of PK is unique
);
Order (
Id PK
ActiveCuponId,
(Id, ActiveCuponId) FK Cupon( OrderId, Id)
);
See fiddle https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=596b30905d02a9e5c799b16da5fff5ab
Remove the ACTIVE column from the gift_cupons table, and replace this state by a foreign key in the orders table, as in:
CREATE TABLE gift_cupons (
cupon_id INTEGER NOT NULL,
order_order_id INTEGER,
);
ALTER TABLE gift_cupons ADD CONSTRAINT gift_cupon_pk PRIMARY KEY ( cupon_id
);
CREATE TABLE orders (
order_id INTEGER NOT NULL
active_cupon INTEGER -- nullable
);
ALTER TABLE orders ADD CONSTRAINT order_pk PRIMARY KEY ( order_id );
ALTER TABLE gift_cupons
ADD CONSTRAINT gift_cupon_order_fk FOREIGN KEY ( order_order_id )
REFERENCES orders ( order_id );
alter table orders
add constraint order_active_cupon_fk foreign key (active_cupon)
references gift_cupons (cupon_id);

update a select query in oracle DB

I have 5 tables in a DB like this (script below)::
DB SCRIPT
CREATE TABLE EQUIPE (
code_equipe char(3) primary key,
nom varchar(30),
directeur varchar(30));
CREATE TABLE PAYS (
code_pays varchar(3) primary key,
nom varchar(20));
CREATE TABLE COUREUR (
num_dossart number(3) primary key,
code_equipe char(3),
nom varchar(30),
code_pays varchar(3));
CREATE TABLE ETAPE (
num_etape number(1) primary key,
date_etape date,
kms number(3),
ville_depart varchar(20),
ville_arrivee varchar(20));
CREATE TABLE TEMPS (
num_dossart number(3),
num_etape number(1),
temps_realise number(6),
primary key(num_dossart,num_etape));
ALTER table COUREUR add CONSTRAINT FK_avoir_code_equipe FOREIGN KEY (code_equipe) REFERENCES EQUIPE(code_equipe);
ALTER table COUREUR add CONSTRAINT FK_avoir_code_pays FOREIGN KEY (code_pays) REFERENCES PAYS(code_pays);
ALTER table TEMPS add CONSTRAINT FK_avoir_num_dossart FOREIGN KEY (num_dossart) REFERENCES COUREUR(num_dossart);
ALTER table TEMPS add CONSTRAINT FK_avoir_num_etape FOREIGN KEY (num_etape) REFERENCES ETAPE(num_etape);
and my query
select num_etape,max(temps_realise) from TEMPS group by num_etape
gave this result
and i want to update it to give result like this
this is
If I read it correctly, a simple join does the job:
select t.num_etape,
c.nom,
max(n.temps_realise)
from temps t join coureur c on c.num_dossart = t.num.dossart
group by t.num_etape,
c.nom;
I have solved my question thanks guys
select num_dossart,nom,num_etape,dernier from COUREUR C,
(select num_etape ,max(temps_realise) as dernier from TEMPS GROUP BY num_etape) T
where num_dossart =
(select num_dossart from TEMPS where temps_realise = dernier )

Cross Referencing Table Value for Another Value to allow Insert

How do I look at one table and use it to check another table for data integrity?
I have two SQL Tables.
One that is a person table
CREATE TABLE PERSON
(
ID INT IDENTITY(10000,1) NOT NULL,
Firstname VARCHAR(15),
Lastname VARCHAR(25) NOT NULL,
Birthdate DATE,
Gender VARCHAR(1),
CHECK ( GENDER IN ('M', 'F')),
Street VARCHAR(50),
City VARCHAR(15),
State VARCHAR(2),
CHECK (State IN ('FL','GA','PA')),
Zip INT,
Phone VARCHAR(10),
Employee VARCHAR(1),
CHECK ( Employee IN('Y','N')),
Member VARCHAR(1),
CHECK ( Member IN('Y','N')),
CHECK (Member IN ('Y') or Employee IN ('Y')),
CONSTRAINT PERSON_PK PRIMARY KEY (ID));
And Employee Table
CREATE TABLE EMPLOYEE
(
ID INT NOT NULL,
Datehired DATE DEFAULT GETDATE(),
Status VARCHAR(1),
CHECK ( Status IN ('F','C')),
Position VARCHAR(25),
EmpType VARCHAR(25),
CONSTRAINT EMPLOYEE_PK PRIMARY KEY (ID),
CONSTRAINT EMPLOYEE_PERSON_FK FOREIGN KEY (ID) REFERENCES PERSON);
Let's say someone isn't an employee. I can still insert them into the Employee Table.
INSERT INTO EMPLOYEE
(ID, Status, Position, EmpType)
VALUES
('10000','C','Teaching Classes','Instructor');
How Do I prevent this from happening.
One method is to have a redundant key:
alter table person
contraint unq_person_id_employee
unique (id, employee);
Then add a computed column to employee:
alter table employee add employee as ('Y') persisted;
Finally, add the constraint:
alter table employee
add constraint fk_employee_person
foreign key (id, employee) references person(id, employee);
Now, you are guaranteed that only employees are in the Employee table.

create views and triggers in oracle sql

I need help with creating a view and then triggers for the following: update salesperson commission(10% of sale), inventory quantity, and customer balance when each invoice line item is entered.
This is what I have right now for the view:
CREATE OR REPLACE VIEW NEW_INVOICE_LINE_ITEM
AS
SELECT COMMISSION, INV_QUANTITY, CUSTOMER_BALANCE
FROM SALESPERSON, INVENTORY, CUSTOMER;
This is what I have for the trigger:
CREATE OR REPLACE TRIGGER UPDATE_COMMISSION
AFTER INSERT ON INVOICE_LINE_ITEM FOR EACH ROW
BEGIN
UPDATE SALESPERSON
SET COMMISSION = (SALE_PRICE * QUANTITY_SOLD) *.10
WHERE :NEW.COMMISSION = SALESPERSON.COMMISSION;
END;
I keep on getting the error: bad bind variable 'new.commission'
This my database schema below:
DROP TABLE PO_LINE_ITEM;
DROP TABLE PURCHASE_ORDER;
DROP TABLE VENDOR;
DROP TABLE INVOICE_LINE_ITEM;
DROP TABLE INVENTORY;
DROP TABLE INVOICE;
DROP TABLE SALESPERSON;
DROP TABLE CUSTOMER;
CREATE TABLE CUSTOMER
(CUSTOMER_ID DECIMAL(2,0) PRIMARY KEY,
CUSTOMER_NAME CHAR(25),
CUSTOMER_ADDRESS CHAR(15),
CUSTOMER_ZIPCODE DECIMAL(5,0),
CUSTOMER_CITY CHAR(15),
CUSTOMER_STATE CHAR(2),
CUSTOMER_BALANCE DECIMAL(4,2)
);
CREATE TABLE SALESPERSON
(SALESPERSON_ID DECIMAL(3,0) PRIMARY KEY,
SALESPERSON_NAME CHAR(25),
COMMISSION DECIMAL(5,2)
);
CREATE TABLE INVOICE
(INVOICE_ID DECIMAL(3,0),
CUSTOMER_ID DECIMAL(2,0),
SALESPERSON_ID DECIMAL(3,0),
ITEM_NUM DECIMAL(4,0),
INVOICE_DATE DATE,
PRIMARY KEY (CUSTOMER_ID, SALESPERSON_ID),
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER,
FOREIGN KEY (SALESPERSON_ID) REFERENCES SALESPERSON
);
CREATE TABLE INVENTORY
(INV_NUM DECIMAL(4,0) PRIMARY KEY,
DESCRIPTION CHAR(10),
INV_QUANTITY DECIMAL(4,0),
INV_PRICE DECIMAL(7,2),
INV_COST DECIMAL(7,2),
INVOICE_ID DECIMAL(3,0),
ITEM_NUM DECIMAL(4,0),
FOREIGN KEY (INVOICE_ID, ITEM_NUM) REFERENCES INVOICE
);
CREATE TABLE INVOICE_LINE_ITEM
(QUANTITY_SOLD DECIMAL(4,0),
SALE_PRICE DECIMAL(7,2),
INVOICE_ID DECIMAL(3,0),
INV_NUM DECIMAL(4,0),
ITEM_NUM DECIMAL(4,0),
PRIMARY KEY (INVOICE_ID, INV_NUM, ITEM_NUM),
FOREIGN KEY (INVOICE_ID, ITEM_NUM) REFERENCES INVOICE,
FOREIGN KEY (INV_NUM) REFERENCES INVENTORY
);
CREATE TABLE VENDOR
(VENDOR_ID DECIMAL(2,0) PRIMARY KEY,
VENDOR_NAME CHAR(25),
CITY CHAR(15),
STATE CHAR(2),
VENDOR_BALANCE DECIMAL(4,2)
);
CREATE TABLE PURCHASE_ORDER
(PURCHASE_ORDER_ID DECIMAL(2,0) PRIMARY KEY,
BALANCE DECIMAL(4,2),
SHIPMENT CHAR(10),
PURCHASE_ORDER_DATE DATE,
VENDER_ID DECIMAL (2,0),
FOREIGN KEY (VENDER_ID) REFERENCES VENDOR
);
CREATE TABLE PO_LINE_ITEM
(PO_DATE DATE,
PO_BALANCE DECIMAL(4,0),
ITEM_NUM DECIMAL(4,0),
INV_QUANTITY DECIMAL(4,0),
INV_NUM DECIMAL(4,0),
PURCHASE_ORDER_ID DECIMAL(2,0),
PRIMARY KEY (INV_NUM, PURCHASE_ORDER_ID),
FOREIGN KEY (INV_NUM) REFERENCES INVENTORY,
FOREIGN KEY (PURCHASE_ORDER_ID) REFERENCES PURCHASE_ORDER);
Thanks in advance!
Table invoice_line_item does not contain column commission, this is the reason for bad bind variable error. You should rewrite your trigger code like here:
create or replace trigger update_commission
after insert on invoice_line_item for each row
begin
update salesperson sp
set sp.commission = sp.commission + (:new.sale_price * :new.quantity_sold * .1)
where sp.salesperson_id =
(select salesperson_id
from invoice i
where i.invoice_id = :new.invoice_id);
end;
In your view you did not attach conditions how to join tables, here is the example how to do this:
create or replace view vw_invoices as
select ili.invoice_id, ili.quantity_sold, ili.sale_price,
i.salesperson_id, sp.salesperson_name
from invoice_line_item ili
join invoice i on i.invoice_id = ili.invoice_id
join salesperson sp on sp.salesperson_id = i.salesperson_id;
Depending on which informations you want to show - connect proper tables and use correct joins.

Can i use composite primary key here?

Below are my two tables
Student(rno int primary key,name varchar(20))
Fees(id int identity,name varchar(10), amount decimal(10,2), pdate date,
rno int foreign key references Student(rno))
In Fees table [id name rno] gives unique.
So can i create composite primary key on table Fees? or need to add one more table to normalize?
Most of the search on table Fees is based on column rno. So it would be good if i have index on rno.
You can Create Composite Primary Key if you want to be unique set of (id,rno) by below code
create table my_table (
column_a integer not null,
column_b integer not null,
column_c varchar(50),
primary key (column_a, column_b)
);
once you create composite primary key constraint you won't able to insert duplicate combination of rno and id
create one more table of FeesMaster where you entered the which type of fees and its name, if any due, then the price or other details.
This will help in future expansion.
and give reference the FeesMaster id to Fees as FeesID. Always give primary key to auto-increment if it is numeric field.
So your table like this.
Student(rno int primary key with auto-increment,name varchar(20))
FeesMaster(feesid int primary key with auto-increment,name varchar(20), price int, dueprice int) --last 2 is optional or give other detail which work as a master detail
--use this table as a transaction table of both above master table, the logic is
Fees(
id int identity,
rno int foreign key references Student(rno),
feesid id foreign key references FeesMaster(FeesID),
amount decimal(10,2),
pdate date)
Updated :-
Fees
id is primary-key of Fees table which are auto-increment
rno is refernce-key of Student table's rno column
feesid is refernce-key of FeesMaster table's feesid column
So in insert gives the feesid of fees master based on fees name. and to get data, the query looklike
select s.name as studentname, fm.name as feesname
from student s
inner join Fees f on f.rno = s.rno
inner join FeesMaster fm on f.feesid = fm.feesid