-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoffeDBLoadScript.sql
More file actions
executable file
·79 lines (66 loc) · 1.84 KB
/
CoffeDBLoadScript.sql
File metadata and controls
executable file
·79 lines (66 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
CREATE TABLE coffees
(
coffeeID INTEGER NOT NULL primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
ProdNum CHAR(6) NOT NULL,
Description VARCHAR(25) NOT NULL,
Price DECIMAL(10,2) NOT NULL
);
CREATE TABLE customers
(
customerID INTEGER NOT NULL primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
Name VARCHAR(25) NOT NULL,
Address VARCHAR(40),
City VARCHAR(30),
State CHAR(2),
Zip VARCHAR(5),
Balance DECIMAL(10,2)
);
CREATE TABLE orders
(
orderID INTEGER NOT NULL primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
customerID INTEGER NOT NULL,
coffeeID INTEGER NOT NULL,
quantity INTEGER NOT NULL,
Cost DECIMAL(10,2) NOT NULL,
orderDate DATE NOT NULL
);
alter table orders
add constraint fk foreign key (customerID)
references Customers(customerID)
;
alter table Orders
add constraint fk2 foreign key (coffeeID)
references Coffees(coffeeID)
;
insert into Customers(name,balance)
values ( 'David' , 0.00 ),
( 'Meir' , 0.00 ),
( 'Sam' , 0.00 ),
( 'Yehuda' , 0.00 ),
( 'Yitshak' , 0.00 )
;
insert into coffees(description, price, ProdNum)
values('Mocha',1.25,'14-001'),
('Italian',1.50,'14-002'),
('Columbian', 1.50,'14-003'),
('Breakfast Special',2.00,'14-004');
CREATE TRIGGER OrdersInsertTrigger
AFTER INSERT ON Orders
MODE DB2SQL
update Customers
set balance =
(
select sum(cost)
from orders, coffees
where orders.coffeeID = coffees.coffeeID AND orders.customerid = customers.customerid
group by orders.customerid
);
/*
insert into Orders(customerID,coffeeID,quantity)
values (1 , 2 , 2 ) ,
(2 , 4 , 4 ) ,
(3 , 2 , 3 ) ,
(3 , 1 , 5 ) ,
(4 , 3 , 1 )
;
*/