-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_dump.sql
604 lines (443 loc) · 17 KB
/
database_dump.sql
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*Person Table */
CREATE TABLE person(PersonId int Primary key not null,
firstName varchar(45),
lastName varchar(45));
alter table person add emailid varchar(45);
alter table person add phonenumber varchar(15);
select * from person;
Update person
set emailid = '[email protected]'
where personid = 1;
Update person
set phonenumber = '1234567890'
where personid = 1;
Update person
set emailid = '[email protected]'
where personid = 111;
Update person
set phonenumber = '7894561230'
where personid = 111;
Insert into person values(1,'chitra','paryani');
Insert into person values(2,'priya','paryani','[email protected]','123456789');
Insert into person values (111, 'omega traders','terry turner');
Insert into person values (222, 'chappels','roasters','[email protected]','785632156');
select * from person;
/*Address Table */
CREATE TABLE Address(AddressId int primary key not null,
Street1 varchar(45),
Street2 varchar(45),
country varchar(45),
city varchar(45),
zipcode varchar(45),
personId int,
foreign key (personId) references person(personId));
alter table address add state varchar(45);
Insert into address values(1,'65-2','St germain street','USA','Boston','02115',1,'MA');
Insert into address values(2,'55-2','park street','USA','Boston','02116',1,'MA');
Insert into address values(111,'45-2','Symphony street','USA','Boston','02115',1,'MA');
Insert into address values(222,'45-2','mass avenue','USA','Boston','02115',1,'MA');
Select * from address;
/* Query to get address details, person details of a person */
Select * from person
inner join address
on person.personid = address.personid;
/*Customer Table */
CREATE TABLE customer( CustomerId int primary key not null,
creditlimit int,
lastrevised date,
foreign key (customerid) references person(personId));
Insert into customer values(1, 100000, '2017-12-13');
Insert into customer values(2, 500000, '2017-12-14');
/* Query to get customer name, address and credit limit */
SELECT * from customer
INNER JOIN person
INNER JOIN address
on person.personid = customer.customerid and
person.personid = address.personid;
/*Vendor Table */
CREATE TABLE vendor(VendorId int primary key not null,
foreign key (vendorId) references person(personId));
Insert into vendor values(111);
Insert into vendor values(222);
/* Query to get vendor details */
select * from vendor
INNER JOIN person
on vendor.vendorId = person.personId;
/* Customer order Table */
CREATE TABLE Customer_Order(orderid int primary key not null,
orderdate date,
customerid int,
foreign key(customerid) references customer(customerid)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
Alter table customer_order add requesteddate date;
update customer_order
set requesteddate = '2017-12-15'
where orderid = 1;
update customer_order
set requesteddate = '2017-12-15'
where orderid = 2;
INSERT INTO customer_order values(1, '2017-12-13',1);
INSERT INTO customer_order values(2, '2017-12-14',2);
select * from customer_order;
select * from customer;
select * from address;
/* Query to get customer order data */
SELECT * from customer_order
INNER JOIN customer
INNER JOIN Person
INNER JOIN Address
on customer_order.customerid = customer.customerid and
customer.customerid = person.personid and
person.personid = address.personid;
/* Product Table */
CREATE TABLE product(ProductId int primary key not null,
Name varchar(45) Not NUll,
Description varchar(45) Null);
Alter table product add price int;
Alter table product add currency varchar(10);
INSERT INTO product values(1,'HP Notebook','LCD Touchscreen, AMD A8-7410 APU',111,457, 'dollar');
INSERT INTO product values(2,'HP ENVY','16GB RAM, AMD A8-7410 APU',111,1000, 'dollar');
INSERT INTO product values(3,'DELL','8GB RAM, AMD A8-7410 APU',222,800, 'dollar');
Select * from product;
/*Query to get vendor details*/
SELECT * from product
Inner join vendor
Inner Join person
on vendor.vendorId = product.vendorId and
vendor.vendorId = person.personId;
select * from product;
delete from product where productId = 1;
Alter table product add vendorid int;
Alter table product add constraint fk_product foreign key (vendorid) references vendor(vendorid);
drop table product;
/*OrderItem Table*/
CREATE TABLE OrderItem(ID int primary key not null,
orderid int not null,
productid int not null,
quantity int not null,
foreign key (orderId) references customer_order(orderId)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
foreign key (productId) references product(productId)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
Insert into orderItem values(1,1,1,1);
Insert into orderItem values(2,2,2,1);
Select * from orderItem;
/*Query to get product and order details */
Select * from product
Inner Join orderItem
on orderitem.productId = product.productId;
drop table orderitem;
/* CREATE INDEX */
CREATE INDEX OrderIdIndex on customer_order(orderid);
CREATE INDEX OrderItemIndex on OrderItem(orderId);
CREATE INDEX productItemIndex on OrderItem(productId);
desc orderitem;
/* CREATE TABLE PICK */
CREATE TABLE PICK(PickId int not null primary key,
pickdate date,
pickedby varchar(45),
orderid int,
foreign key (orderid) references customer_order(orderid));
INSERT INTO PICK values(1,'2017-12-13','Suresh',1);
INSERT INTO PICK values(2,'2017-12-14','Ramesh',2);
/* SQL query to get order pick details */
SELECT * FROM customer
INNER JOIN person
INNER JOIN address
INNER JOIN customer_order
INNER JOIN product
INNER JOIN orderitem
INNER JOIN pick
on customer.customerId = person.personId and
person.personId = address.personId and
customer.customerId = customer_order.customerId and
customer_order.orderId = orderitem.orderId and
orderitem.productId = product.productId and
orderitem.orderid = pick.orderid;
/*Create Table Stock Pick */
CREATE TABLE STOCKPICK(id int not null primary key,
pickId int,
productId int,
quantity int not null,
foreign key (pickId) references pick(pickId)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
foreign key (productId) references product(productId)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
INSERT INTO stockpick values(1,1,1,1);
INSERT INTO stockpick values(2,2,2,1);
/*Create Table ScheduleDelivery */
CREATE TABLE ScheduleDelivery(DeliveryId int primary key not null,
Deliverydate date,
shippedby varchar(45),
pickId int,
customerid int,
invoiceid int,
foreign key (pickId) references pick(pickId),
foreign key (customerId) references customer(customerid)
);
show create table scheduledelivery;
Alter table scheduledelivery drop foreign key scheduledelivery_ibfk_3;
alter table scheduledelivery drop invoiceid;
select * from scheduledelivery;
desc scheduledelivery;
Insert into scheduledelivery values(1,'2017-12-15','Best Buy',1,1);
Insert into scheduledelivery values(2,'2017-12-16','Amazon',2,2);
/*Create Invoice Table */
CREATE TABLE invoice(invoiceId int primary key not null,
invoicedate date,
deliveryid int,
invoicetotal int,
customerid int,
foreign key (customerid) references customer(customerid));
Insert into invoice values(1, '2017-12-15',1,30000,1);
Insert into invoice values(2, '2017-12-16',2,50000,2);
/*Create Delivery Invoice Table */
CREATE TABLE DeliveryInvoice(Id int primary key not null,
deliveryid int,
invoiceid int,
invoicedate date,
foreign key (deliveryid) references scheduledelivery(deliveryid)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
foreign key (invoiceId) references invoice(invoiceId)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
INSERT INTO DeliveryInvoice values(1, 1, 1, '2017-12-15');
INSERT INTO DeliveryInvoice values(2, 2, 2, '2017-12-16');
/*Top Customer*/
SELECT person.firstName, person.lastName, product.name AS productName, DeliveryInvoice.invoicedate ,
product.price,product.currency,ScheduleDelivery.shippedby from customer
INNER JOIN scheduledelivery
INNER JOIN stockpick
INNER JOIN product
INNER JOIN person
INNER JOIN DeliveryInvoice
on customer.customerid = scheduledelivery.customerid and
customer.customerid = person.personid and
scheduledelivery.pickid = stockpick.pickid and
stockpick.productid = product.productid and
deliveryinvoice.deliveryId = scheduledelivery.deliveryId
ORDER BY product.price DESC
LIMIT 1;
/*Delayed Delivery */
SELECT customer.customerid, person.firstName, person.lastName,
ScheduleDelivery.deliverydate FROM customer_order
INNER JOIN ScheduleDelivery
INNER JOIN customer
INNER JOIN person
on customer.customerid = customer_order.customerid and
person.personid = customer.customerid and
customer_order.customerid = ScheduleDelivery.customerid
where requesteddate < deliverydate;
alter table invoice add constraint fk_delivery foreign key (deliveryid) references ScheduleDelivery(deliveryid);
drop table invoice;
show create table invoice;
Alter table invoice drop foreign key fk_delivery;
Alter table invoice drop foreign key customerid;
/* Stored procedure to maintain history data for analysis purpose */
Create table history(firstName varchar(45), lastName varchar(45));
desc person;
desc address;
desc customer;
desc customer_order;
desc orderItem;
desc product;
desc vendor;
desc stockpick;
desc pick;
desc scheduledelivery;
desc deliveryinvoice;
desc invoice;
desc productcategory;
desc v_rating;
desc c_rating;
/* Created temp inventory table to show stock available in warehouse*/
Create table inventory(id int not null primary key,warehouse varchar(45),
productid int, stockAvailable int,
foreign key (productId) references product(productId));
alter table inventory add Asofdate date;
Update inventory
set Asofdate = '2017-12-13';
Insert into inventory values (1, 'M1',1,100);
Insert into inventory values (2, 'M1',2,100);
Insert into inventory values (3, 'M1',3,100);
select * from inventory;
/*Created View to view total stock available including ordered and available */
CREATE View TotalAvailProduct
AS
SELECT orderid, quantity from orderitem
UNION ALL
select productid, stockavailable from inventory;
CREATE view stock
AS
SELECT orderid, sum(quantity) from totalavailproduct group by orderid;
select * from stock;
/* Trigger to change order status flow */
Alter table customer_order add column order_status_flow varchar(45);
select * from customer_order;
Delimiter //
CREATE TRIGGER changeStatusOrder
BEFORE INSERT on customer_order
FOR EACH ROW
BEGIN
SET new.order_status_flow = 'Entered';
END //
Delimiter ;
UPDATE customer_order
SET order_status_flow = 'Entered';
select * from customer_order;
Insert into customer_order (orderid,orderdate,customerid,requesteddate)
values (3,'2017-12-14',1,'2017-12-20');
select * from orderItem;
UPDATE orderitem
SET item_status_flow = 'Ordered';
alter table orderitem add item_status_flow varchar(45);
Delimiter //
CREATE TRIGGER changeStatusItem
BEFORE INSERT on orderitem
FOR EACH ROW
BEGIN
SET new.item_status_flow = 'Ordered';
END //
Delimiter ;
Insert into orderitem (Id, orderid, productid, quantity) values
(3,3,1,1);
select * from orderitem;
/*Trigger for pick */
select * from pick;
alter table pick add pick_status_flow varchar(45);
alter table pick drop pick_status_flow;
update pick
set pick_status_flow = 'Picked';
Delimiter //
CREATE TRIGGER changeStatusPick
BEFORE INSERT on pick
FOR EACH ROW
BEGIN
SET new.pick_status_flow = 'Picked';
END //
Delimiter ;
Insert into pick (pickid, pickdate, pickedby, orderid) values
(3,'2017-12-14','Mahesh',3);
select * from pick;
/* Trigger for Delivery of Items */
select * from scheduledelivery;
alter table scheduledelivery add delivery_status_flow varchar(45);
update scheduledelivery
SET delivery_status_flow = 'Delivered';
Delimiter //
CREATE TRIGGER changeStatusDelivery
BEFORE INSERT on scheduledelivery
FOR EACH ROW
BEGIN
SET new.delivery_status_flow = 'Delivered';
END //
Delimiter ;
Insert into scheduledelivery
(deliveryid, deliverydate, shippedby, pickid, customerid) values
(3,'2017-12-20','Best Buy',3,1);
delete from scheduledelivery where DeliveryId = 3;
select * from scheduledelivery;
/* User Access */
/* Service Clerk */
CREATE USER ServiceClerk IDENTIFIED BY 'password';
-- Revoke all privileges for the user
REVOKE ALL privileges, grant option from ServiceClerk;
-- Grant needed privileges
GRANT SELECT ON customer to ServiceClerk;
GRANT UPDATE (firstName, lastName, EmailId, PhoneNumber)
ON customer TO ServiceClerk;
GRANT SELECT, DELETE ON customer_order to ServiceClerk;
GRANT INSERT, SELECT, DELETE on orderItem to ServiceClerk;
GRANT SELECT on inventory to ServiceClerk;
/* Entry Clerk */
CREATE USER EntryClerk IDENTIFIED BY 'password';
-- Revoke all privileges for the user
REVOKE ALL privileges, grant option from ServiceClerk;
-- Grant needed privileges
GRANT INSERT, SELECT on customer TO EntryClerk;
GRANT UPDATE (firstName, lastName, EmailId, PhoneNumber)
ON customer TO EntryClerk;
GRANT SELECT, DELETE ON customer_order to EntryClerk;
GRANT INSERT, SELECT, DELETE on orderItem to EntryClerk;
GRANT SELECT on inventory to EntryClerk;
/* Table product category */
desc filmclub.filmcategory;
Select * from product;
Alter table product add categoryid int;
Alter table product add constraint fk_cat foreign key (categoryId) references ProductCategory(categoryId);
Alter table product drop category;
CREATE TABLE ProductCategory(CategoryId int, Category varchar(45));
Insert into ProductCategory values (1, 'Laptop');
Insert into ProductCategory values (2, 'Desktop');
UPDATE product
SET category = 'Laptop';
select * from product;
Update product
SET categoryid = 1;
select * from productcategory;
select * from productcategory;
/* Procedure to get product Category */
Delimiter //
Create procedure product_cat_view(IN categoryvar int)
BEGIN
Select productcategory.Category, product.Name from product, productcategory
where product.categoryid = productcategory.Categoryid and
productcategory.CategoryId = categoryvar;
END //
Delimiter ;
/*count of products based on category */
Delimiter //
Create procedure product_cat_count(IN categoryvar int, OUT total int)
BEGIN
Select productcategory.Category, count(product.Name) from product, productcategory
where product.categoryid = productcategory.Categoryid and
productcategory.CategoryId = categoryvar;
END //
Delimiter ;
/* Create Vendor rating table */
create table v_rating(vendorid int, rating int,
foreign key (vendorid) references vendor(vendorid));
select * from vendor;
insert into v_rating values(111,5);
insert into v_rating values(222,4);
/* Create Customer rating */
Create table c_rating(customerid int, rating int,
foreign key (customerid) references customer(customerid));
select * from customer;
select * from c_rating;
Insert into c_rating values(1, 5);
Insert into c_rating values(2, 8);
/*created function to find vendor rating */
Delimiter %%
create function fn_vendorRating(vendorID int)
returns varchar(200)
begin
DECLARE result varchar(200);
SET result:=
(select concat(vendorid, ' | ',repeat('*',rating))
as rating
from vendor v,v_rating vr
where v.vendorid = vr.vendorid and vr.vendorid = vendorId);
RETURN result;
end %%
DELIMITER ;
/* Create function to find customer rating */
Delimiter %%
create function fn_custRating(customerID int)
returns varchar(200)
begin
DECLARE result varchar(200);
SET result:=
(select concat(customerid, ' | ',repeat('*',rating))
as rating
from customer v,c_rating vr
where v.customerid = vr.customerid and vr.customerid = customerID);
RETURN result;
end %%
DELIMITER ;