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.
Related
I am just learning SQL and when I try to create the "Order" table I get the error "missing right parentheses". When I remove the FK constraint I get the error "invalid identifier"
DROP TABLE CUSTOMER;
CREATE TABLE CUSTOMER
(
CUST_ID INT PRIMARY KEY,
COMP_NAME VARCHAR(40),
CONT_LNAME VARCHAR(30),
CONT_FNAME VARCHAR(30),
PHONE VARCHAR(30),
EMAIL VARCHAR(40),
ADDRESS VARCHAR(40),
CITY VARCHAR(30),
ZIP NUMBER
);
INSERT INTO CUSTOMER (CUST_ID, COMP_NAME, CONT_LNAME, CONT_FNAME, PHONE, EMAIL, ADDRESS, CITY,ZIP) VALUES
(1,'Atomic Liqours','Signor','Rose','518-990-8765','rose#atomicliqours.com','76 Hackett Blvd','Albany','12204');
INSERT INTO CUSTOMER (CUST_ID, COMP_NAME, CONT_LNAME, CONT_FNAME, PHONE, EMAIL, ADDRESS, CITY,ZIP) VALUES
(2,'HBD Bar','Capozolli','Rose','889-908-6666','rose#hbd.com','888 Project Rd','Troy','12180');
INSERT INTO CUSTOMER (CUST_ID, COMP_NAME, CONT_LNAME, CONT_FNAME, PHONE, EMAIL, ADDRESS, CITY,ZIP) VALUES
(3,'Lee Harvey','Smith','Seth','675-888-9999','seth#lh.com','78 Healy Ave','Troy','12222');
-- CREATING ORDER TABLE
CREATE TABLE ORDERS
(
ORD_ID INT PRIMARY KEY,
CUST_ID INT FOREIGN KEY REFERENCES Customers (CUST_ID),
ORD_DATE DATE,
DEL_DATE DATE,
);
Looks like your have a typo in your script. Your table name is CUSTOMER, but in CREATE TABLE you references to CUSTOMERS. So, I think that your create table statement must looks like:
CREATE TABLE ORDERS
(
ORD_ID INT PRIMARY KEY,
CUST_ID INT FOREIGN KEY REFERENCES Customer (CUST_ID),
ORD_DATE DATE,
DEL_DATE DATE,
);
I'm creating a database design for our water refilling system and I'm just new to databases. I am stuck with creating table that provides two different prices for a product. To further explain my problem, here's an example, a product's ('5 GALLON') price changes when it is delivered or bought on point by a customer. For example a delivered ('5 GALLON') is 45 pesos while a bought on point gallon is only 40 pesos. Can someone help me please?
Here's my codes so far
create table Product (
product_id int primary key,
prodtype_id int,
product_name varchar(55),
product_quantity int
)
-----NOT SURE IF THESE TWO TABLES ARE CORRECT
create table DeliveryPrice (
prod_id int,
product_price money,
foreign key (prod_id) references Product
)
create table OnPointPrice(
prod_id int,
product_price money,
foreign key (prod_id) references Product
)
You're likely better off just having the two prices in the Product table. They are attributes of the product, so that's where they belong.
Also, you should specify which columns are NOT NULL in your database (which should be most of them).
So is this correct?
create table Product(
product_id int primary key,
product_name varchar(55) not null,
product_quantity int not null,
pickup_price money not null,
delivery_price money not null
)
create table Customer(
customer_id int primary key,
customer_name varchar (255) not null,
customer_address varchar(200),
customer_phone int
)
create table INVOICE(
inv_number int primary key,
customer_id varchar(5),
foreign key (customer_id) references Customer,
inv_date date not null,
bought_mode char(10) not null
)
create table LINE(
INV_NUMBER int,
foreign key (INV_NUMBER) references INVOICE,
LINE_NUMBER INT not null,
PRIMARY KEY (INV_NUMBER, LINE_NUMBER),
line_quantity int not null,
line_price money not null
)
create table customer(
custno number(10) constraint foo primary key,
custname character(15),
city character(15),
phone number(10));
create table invoice(
invo number(10) constraint inv primary key,
invdate date(10),
constarint c references customer(custno));
I cannot able to create the second table and am having doubt of the data type date and If I neglected that attribute(invdate) still I cannot able to create the second table.Reason please.
create table customer
( custno int constraint foo primary key
,custname character(15)
,city character(15)
,phone int
)
go
create table invoice
( invo int constraint inv primary key
constraint ck references customer(custno)
,invdate date
)
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.
I have a University project and I have to create a DB for a plant shop.
I have a problem with the order table. At the moment it only allows a customer to buy one product at a time but in real life a customer buys many products at a time.
For example,
We have a customer John Doe, and he bus two products that are in the product table. How do I pull those two (or more) products and add them to one order table?
Below is the SQL code I wrote:
CREATE TABLE customer(
customer_id INT(3),
customer_fname VARCHAR(20),
customer_lname VARCHAR(20),
customer_gender CHAR(1),
customer_tel VARCHAR(20),
customer_email VARCHAR(30),
customer_dateJoined DATE,
address_id INT(3),
PRIMARY KEY(customer_id),
INDEX(customer_id),
FOREIGN KEY(customer_id) REFERENCES address);
CREATE TABLE address(
adress_id INT(3),
customer_street VARCHAR(30),
customer_town VARCHAR(30),
customer_postcode CHAR(7),
PRIMARY KEY(address_id),
INDEX(address_id),
FOREIGN KEY(address_id) REFERENCES customer(address_id),
FOREIGN KEY(address_id) REFERENCES employee(address_id));
CREATE TABLE product(
product_id INT(5),
product_name VARCHAR(20),
product_season VARCHAR(15),
product_price NUMERIC(4,2),
product_origin VARCHAR(15),
product_type VARCHAR(15),
product_inStock BOOLEAN,
PRIMARY KEY(product_id),
INDEX(product_id));
CREATE TABLE orders(
order_id INT(3),
customer_id INT(3),
employee_id INT(3),
product_name VARCHAR(20),
quantity INT(4),
order_date TIMESTAMP,
PRIMARY KEY(order_id),
INDEX(order_id));
CREATE TABLE employee(
employee_id INT(3),
employee_fname VARCHAR(20),
employee_lname VARCHAR(20),
address_id INT (3),
employee_pay NUMERIC(2,2),
employee_daysOff INT(2),
employee_hoursWorked INT(3),
PRIMARY KEY(staff_id),
INDEX(staff_id));
You have to create Kettle Table customer_orders, in this table you store customer_id and order_id and connect them with foreign keys to the customer and orders tables.
Like in the following query:
CREATE TABLE customer_orders(
customer_id INT(3),
order_id INT(3),
PRIMARY KEY(customer_id, order_id),
FOREIGN KEY(customer_id) REFERENCES customer(customer_id),
FOREIGN KEY(order_id) REFERENCES orders(order_id)
);
CREATE TABLE sales.stores (
store_id INT IDENTITY (1, 1) PRIMARY KEY,
store_name VARCHAR (255) NOT NULL,
phone VARCHAR (25),
email VARCHAR (255),
street VARCHAR (255),
city VARCHAR (255),
state VARCHAR (10),
zip_code VARCHAR (5)
);
One way to design this is to have two tables for Orders.
OrderHeader :- This contains order id with customer details.
OrderItem :- Contains Line items per order.
OrderHeader fields could be
order_id //primary key
customer_id
employee_id
order_date
OrderItem :- Fields could be
order_id //composite key
line_Item_id //composite key
product_id,
product_quant