-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-orderprocessing.sql
More file actions
346 lines (314 loc) · 9.62 KB
/
3-orderprocessing.sql
File metadata and controls
346 lines (314 loc) · 9.62 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* Consider the database schemas given below.
Write ER diagram and schema diagram. The primary keys are underlined and the data types are
specified.
Create tables for the following schema listed below by properly specifying the primary keys and
foreign keys.
Enter at least five tuples for each relation.
Order processing database
Customer (Cust#:int, cname: string, city: string)
Order (order#:int, odate: date, cust#: int, order-amt: int)
Order-item (order#:int, Item#: int, qty: int)
Item (item#:int, unitprice: int)
Shipment (order#:int, warehouse#: int, ship-date: date)
Warehouse (warehouse#:int, city: string)
1. List the Order# and Ship_date for all orders shipped from Warehouse# "W2".
2. List the Warehouse information from which the Customer named "Kumar" was supplied his
orders. Produce a listing of Order#, Warehouse#.
3. Produce a listing: Cname, #ofOrders, Avg_Order_Amt, where the middle column is the total
number of orders by the customer and the last column is the average order amount for that
customer. (Use aggregate functions)
4. Delete all orders for customer named "Kumar".
5. Find the item with the maximum unit price.
6. A trigger that updates order_amout based on quantity and unitprice of order_item
7. Create a view to display orderID and shipment date of all orders shipped from a warehouse
*/
CREATE DATABASE ORDERPROCESSING;
USE ORDERPROCESSING;
CREATE TABLE CUSTOMER(
cust_id int primary key,
cname varchar(35) not null,
city varchar(35) not null
);
CREATE TABLE ORDERS(
order_id int primary key,
odate date not null,
cust_id int not null,
order_amt int not null,
foreign key (cust_id) references CUSTOMER(cust_id) on delete cascade
);
CREATE TABLE ITEM(
item_id int primary key,
unitprice int not null
);
CREATE TABLE ORDER_ITEM(
order_id int not null,
item_id int not null,
qty int not null,
foreign key (order_id) references ORDERS(order_id) on delete cascade,
foreign key (item_id) references ITEM(item_id) on delete cascade
);
CREATE TABLE WAREHOUSE(
warehouse_id int primary key,
city varchar(15) not null
);
CREATE TABLE SHIPMENT(
order_id int not null,
warehouse_id int not null,
ship_date date not null,
foreign key (order_id) references ORDERS(order_id) on delete cascade,
foreign key (warehouse_id) references WAREHOUSE(warehouse_id) on delete cascade
);
INSERT INTO CUSTOMER VALUES
(1,'Sia','Mysuru'),
(2,'Mia','Bengaluru'),
(3,'Pia','Mumbai'),
(4,'Kumar','Delhi'),
(5,'Tia','Chennai');
INSERT INTO ORDERS VALUES
(1001, "2020-01-14", 1, 2000),
(1002, "2021-04-13", 2, 500),
(1003, "2019-10-02", 3, 2500),
(1004, "2019-05-12", 5, 1000),
(1005, "2020-12-23", 4, 1200),
(1006, "2021-12-12", 3, 5000);
INSERT INTO ITEM VALUES
(10001, 400),
(10002, 200),
(10003, 1000),
(10004, 100),
(10005, 500);
INSERT INTO ORDER_ITEM VALUES
(1001, 10001, 5),
(1002, 10005, 1),
(1003, 10005, 5),
(1004, 10003, 1),
(1005, 10004, 12);
INSERT INTO WAREHOUSE VALUES
(501, "Mysuru"),
(502, "Bengaluru"),
(503, "Mumbai"),
(504, "Dehli"),
(505, "Chennai");
INSERT INTO SHIPMENT VALUES
(1001, 501, "2020-01-16"),
(1002, 502, "2021-04-14"),
(1003, 502, "2019-10-07"),
(1004, 504, "2019-05-16"),
(1005, 505, "2020-12-23");
SELECT * FROM CUSTOMER;
/*+---------+-------+-----------+
| cust_id | cname | city |
+---------+-------+-----------+
| 1 | Sia | Mysuru |
| 2 | Mia | Bengaluru |
| 3 | Pia | Mumbai |
| 4 | Kumar | Delhi |
| 5 | Tia | Chennai |
+---------+-------+-----------+
5 rows in set (0.00 sec)
*/
SELECT * FROM ORDERS;
/*
+----------+------------+---------+-----------+
| order_id | odate | cust_id | order_amt |
+----------+------------+---------+-----------+
| 1001 | 2020-01-14 | 1 | 2000 |
| 1002 | 2021-04-13 | 2 | 500 |
| 1003 | 2019-10-02 | 3 | 2500 |
| 1004 | 2019-05-12 | 5 | 1000 |
| 1005 | 2020-12-23 | 4 | 1200 |
| 1006 | 2021-12-12 | 3 | 5000 |
+----------+------------+---------+-----------+
6 rows in set (0.00 sec)
*/
SELECT * FROM ITEM;
/*
+---------+-----------+
| item_id | unitprice |
+---------+-----------+
| 10001 | 400 |
| 10002 | 200 |
| 10003 | 1000 |
| 10004 | 100 |
| 10005 | 500 |
+---------+-----------+
5 rows in set (0.00 sec)
*/
SELECT * FROM ORDER_ITEM;
/*
+----------+---------+-----+
| order_id | item_id | qty |
+----------+---------+-----+
| 1001 | 10001 | 5 |
| 1002 | 10005 | 1 |
| 1003 | 10005 | 5 |
| 1004 | 10003 | 1 |
| 1005 | 10004 | 12 |
+----------+---------+-----+
5 rows in set (0.00 sec)
*/
SELECT * FROM WAREHOUSE;
/*
+--------------+-----------+
| warehouse_id | city |
+--------------+-----------+
| 501 | Mysuru |
| 502 | Bengaluru |
| 503 | Mumbai |
| 504 | Dehli |
| 505 | Chennai |
+--------------+-----------+
5 rows in set (0.00 sec)
*/
SELECT * FROM SHIPMENT;
/*
+----------+--------------+------------+
| order_id | warehouse_id | ship_date |
+----------+--------------+------------+
| 1001 | 501 | 2020-01-16 |
| 1002 | 502 | 2021-04-14 |
| 1003 | 502 | 2019-10-07 |
| 1004 | 504 | 2019-05-16 |
| 1005 | 505 | 2020-12-23 |
+----------+--------------+------------+
5 rows in set (0.00 sec)
*/
-- List the Order# and Ship_date for all orders shipped from Warehouse# "W2" (warehouse_id=502).
SELECT order_id,ship_date
FROM SHIPMENT
WHERE warehouse_id=502;
/*
+----------+------------+
| order_id | ship_date |
+----------+------------+
| 1002 | 2021-04-14 |
| 1003 | 2019-10-07 |
+----------+------------+
2 rows in set (0.00 sec)
*/
-- List the Warehouse information from which the Customer named "Kumar" was supplied his
-- orders. Produce a listing of Order#, Warehouse#.
SELECT s.order_id,s.warehouse_id
FROM SHIPMENT s,ORDERS o,CUSTOMER c
WHERE cname LIKE '%kumar%' AND c.cust_id=o.cust_id AND o.order_id=s.order_id;
/*
+----------+--------------+
| order_id | warehouse_id |
+----------+--------------+
| 1005 | 505 |
+----------+--------------+
1 row in set (0.00 sec)
*/
-- Produce a listing: Cname, #ofOrders, Avg_Order_Amt, where the middle column is the total
-- number of orders by the customer and the last column is the average order amount for that
-- customer. (Use aggregate functions)
SELECT cname,COUNT(order_id),AVG(order_amt)
FROM CUSTOMER c,ORDERS o
WHERE c.cust_id=o.cust_id
GROUP BY cname;
/*
+-------+-----------------+----------------+
| cname | COUNT(order_id) | AVG(order_amt) |
+-------+-----------------+----------------+
| Sia | 1 | 2000.0000 |
| Mia | 1 | 500.0000 |
| Pia | 2 | 3750.0000 |
| Kumar | 1 | 1200.0000 |
| Tia | 1 | 1000.0000 |
+-------+-----------------+----------------+
5 rows in set (0.00 sec)
*/
-- Delete all orders for customer named "Kumar".
DELETE FROM ORDERS
WHERE cust_id=(SELECT cust_id FROM CUSTOMER WHERE cname LIKE "%kumar%");
-- Query OK, 1 row affected (0.04 sec)
SELECT * FROM ORDERS;
/*
+----------+------------+---------+-----------+
| order_id | odate | cust_id | order_amt |
+----------+------------+---------+-----------+
| 1001 | 2020-01-14 | 1 | 2000 |
| 1002 | 2021-04-13 | 2 | 500 |
| 1003 | 2019-10-02 | 3 | 2500 |
| 1004 | 2019-05-12 | 5 | 1000 |
| 1006 | 2021-12-12 | 3 | 5000 |
+----------+------------+---------+-----------+
5 rows in set (0.00 sec)
*/
-- Find the item with the maximum unit price.
SELECT MAX(unitprice) FROM ITEM;
/*
+----------------+
| MAX(unitprice) |
+----------------+
| 1000 |
+----------------+
1 row in set (0.00 sec)
*/
SELECT * FROM ITEM
WHERE unitprice=(SELECT MAX(unitprice) FROM ITEM);
/*
+---------+-----------+
| item_id | unitprice |
+---------+-----------+
| 10003 | 1000 |
+---------+-----------+
1 row in set (0.00 sec)
*/
SELECT item_id,unitprice AS max_unitprice
FROM ITEM
WHERE unitprice=(SELECT MAX(unitprice) FROM ITEM);
/*
+---------+---------------+
| item_id | max_unitprice |
+---------+---------------+
| 10003 | 1000 |
+---------+---------------+
1 row in set (0.00 sec)
*/
-- A trigger that updates order_amout based on quantity and unitprice of order_item
DELIMITER $$
CREATE TRIGGER UpdateOrderAmount
AFTER INSERT ON ORDER_ITEM
FOR EACH ROW
BEGIN
UPDATE ORDERS SET order_amt=(new.qty*(SELECT unitprice FROM ITEM WHERE item_id=new.item_id)) WHERE ORDERS.order_id=NEW.order_id;
END; $$
-- Query OK, 0 rows affected (0.06 sec)
DELIMITER ;
INSERT INTO ORDERS VALUES
(1007,'2022-01-01',5,1300);
-- Query OK, 1 row affected (0.06 sec)
INSERT INTO ORDER_ITEM VALUES
(1007,10005, 3);
-- Query OK, 1 row affected (0.06 sec)
SELECT *FROM ORDERS;
/*
+----------+------------+---------+-----------+
| order_id | odate | cust_id | order_amt |
+----------+------------+---------+-----------+
| 1001 | 2020-01-14 | 1 | 2000 |
| 1002 | 2021-04-13 | 2 | 500 |
| 1003 | 2019-10-02 | 3 | 2500 |
| 1004 | 2019-05-12 | 5 | 1000 |
| 1006 | 2021-12-12 | 3 | 5000 |
| 1007 | 2022-01-01 | 5 | 1500 |
+----------+------------+---------+-----------+
6 rows in set (0.00 sec)
*/
-- Create a view to display orderID and shipment date of all orders shipped from a warehouse
CREATE VIEW OrderInfoOfWarehouse AS
SELECT order_id,ship_date
FROM SHIPMENT
WHERE warehouse_id=502;
-- Query OK, 0 rows affected (0.06 sec)
SELECT * FROM OrderInfoOfWarehouse;
/*
+----------+------------+
| order_id | ship_date |
+----------+------------+
| 1002 | 2021-04-14 |
| 1003 | 2019-10-07 |
+----------+------------+
2 rows in set (0.00 sec)
*/