-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs4400_phase3_stored_procedures_team27.sql
1312 lines (579 loc) · 25.8 KB
/
cs4400_phase3_stored_procedures_team27.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- CS4400: Introduction to Database Systems (Spring 2024)
-- Phase III: Stored Procedures & Views [v0] Sunday, March 24, 2024 @ 8:00pm EST
-- Team 27
-- Benjamin Colton (bcolton8)
-- Sheikh Munim Riddhi (sriddhi3)
-- Colin Naughton (cnaughton7)
-- Ryan Otsuka (rotsuka6)
-- Joshua Buchsbaum (jbuchsbaum3)
-- Directions:
-- Please follow all instructions for Phase III as listed on Canvas.
-- Fill in the team number and names and GT usernames for all members above.
-- Create Table statements must be manually written, not taken from an SQL Dump file.
-- This file must run without error for credit.
/* This is a standard preamble for most of our scripts. The intent is to establish
a consistent environment for the database behavior. */
set global transaction isolation level serializable;
set global SQL_MODE = 'ANSI,TRADITIONAL';
set names utf8mb4;
set SQL_SAFE_UPDATES = 0;
set @thisDatabase = 'drone_dispatch';
drop database if exists drone_dispatch;
create database if not exists drone_dispatch;
use drone_dispatch;
-- -----------------------------------------------
-- table structures
-- -----------------------------------------------
create table users (
uname varchar(40) not null,
first_name varchar(100) not null,
last_name varchar(100) not null,
address varchar(500) not null,
birthdate date default null,
primary key (uname)
) engine = innodb;
create table customers (
uname varchar(40) not null,
rating integer not null,
credit integer not null,
primary key (uname)
) engine = innodb;
create table employees (
uname varchar(40) not null,
taxID varchar(40) not null,
service integer not null,
salary integer not null,
primary key (uname),
unique key (taxID)
) engine = innodb;
create table drone_pilots (
uname varchar(40) not null,
licenseID varchar(40) not null,
experience integer not null,
primary key (uname),
unique key (licenseID)
) engine = innodb;
create table store_workers (
uname varchar(40) not null,
primary key (uname)
) engine = innodb;
create table products (
barcode varchar(40) not null,
pname varchar(100) not null,
weight integer not null,
primary key (barcode)
) engine = innodb;
create table orders (
orderID varchar(40) not null,
sold_on date not null,
purchased_by varchar(40) not null,
carrier_store varchar(40) not null,
carrier_tag integer not null,
primary key (orderID)
) engine = innodb;
create table stores (
storeID varchar(40) not null,
sname varchar(100) not null,
revenue integer not null,
manager varchar(40) not null,
primary key (storeID)
) engine = innodb;
create table drones (
storeID varchar(40) not null,
droneTag integer not null,
capacity integer not null,
remaining_trips integer not null,
pilot varchar(40) not null,
primary key (storeID, droneTag)
) engine = innodb;
create table order_lines (
orderID varchar(40) not null,
barcode varchar(40) not null,
price integer not null,
quantity integer not null,
primary key (orderID, barcode)
) engine = innodb;
create table employed_workers (
storeID varchar(40) not null,
uname varchar(40) not null,
primary key (storeID, uname)
) engine = innodb;
-- -----------------------------------------------
-- referential structures
-- -----------------------------------------------
alter table customers add constraint fk1 foreign key (uname) references users (uname)
on update cascade on delete cascade;
alter table employees add constraint fk2 foreign key (uname) references users (uname)
on update cascade on delete cascade;
alter table drone_pilots add constraint fk3 foreign key (uname) references employees (uname)
on update cascade on delete cascade;
alter table store_workers add constraint fk4 foreign key (uname) references employees (uname)
on update cascade on delete cascade;
alter table orders add constraint fk8 foreign key (purchased_by) references customers (uname)
on update cascade on delete cascade;
alter table orders add constraint fk9 foreign key (carrier_store, carrier_tag) references drones (storeID, droneTag)
on update cascade on delete cascade;
alter table stores add constraint fk11 foreign key (manager) references store_workers (uname)
on update cascade on delete cascade;
alter table drones add constraint fk5 foreign key (storeID) references stores (storeID)
on update cascade on delete cascade;
alter table drones add constraint fk10 foreign key (pilot) references drone_pilots (uname)
on update cascade on delete cascade;
alter table order_lines add constraint fk6 foreign key (orderID) references orders (orderID)
on update cascade on delete cascade;
alter table order_lines add constraint fk7 foreign key (barcode) references products (barcode)
on update cascade on delete cascade;
alter table employed_workers add constraint fk12 foreign key (storeID) references stores (storeID)
on update cascade on delete cascade;
alter table employed_workers add constraint fk13 foreign key (uname) references store_workers (uname)
on update cascade on delete cascade;
-- -----------------------------------------------
-- table data
-- -----------------------------------------------
insert into users values
('jstone5', 'Jared', 'Stone', '101 Five Finger Way', '1961-01-06'),
('sprince6', 'Sarah', 'Prince', '22 Peachtree Street', '1968-06-15'),
('awilson5', 'Aaron', 'Wilson', '220 Peachtree Street', '1963-11-11'),
('lrodriguez5', 'Lina', 'Rodriguez', '360 Corkscrew Circle', '1975-04-02'),
('tmccall5', 'Trey', 'McCall', '360 Corkscrew Circle', '1973-03-19'),
('eross10', 'Erica', 'Ross', '22 Peachtree Street', '1975-04-02'),
('hstark16', 'Harmon', 'Stark', '53 Tanker Top Lane', '1971-10-27'),
('echarles19', 'Ella', 'Charles', '22 Peachtree Street', '1974-05-06'),
('csoares8', 'Claire', 'Soares', '706 Living Stone Way', '1965-09-03'),
('agarcia7', 'Alejandro', 'Garcia', '710 Living Water Drive', '1966-10-29'),
('bsummers4', 'Brie', 'Summers', '5105 Dragon Star Circle', '1976-02-09'),
('cjordan5', 'Clark', 'Jordan', '77 Infinite Stars Road', '1966-06-05'),
('fprefontaine6', 'Ford', 'Prefontaine', '10 Hitch Hikers Lane', '1961-01-28');
insert into customers values
('jstone5', 4, 40),
('sprince6', 5, 30),
('awilson5', 2, 100),
('lrodriguez5', 4, 60),
('bsummers4', 3, 110),
('cjordan5', 3, 50);
insert into employees values
('awilson5', '111-11-1111', 9, 46000),
('lrodriguez5', '222-22-2222', 20, 58000),
('tmccall5', '333-33-3333', 29, 33000),
('eross10', '444-44-4444', 10, 61000),
('hstark16', '555-55-5555', 20, 59000),
('echarles19', '777-77-7777', 3, 27000),
('csoares8', '888-88-8888', 26, 57000),
('agarcia7', '999-99-9999', 24, 41000),
('bsummers4', '000-00-0000', 17, 35000),
('fprefontaine6', '121-21-2121', 5, 20000);
insert into store_workers values
('eross10'),
('hstark16'),
('echarles19');
insert into stores values
('pub', 'Publix', 200, 'hstark16'),
('krg', 'Kroger', 300, 'echarles19');
insert into employed_workers values
('pub', 'eross10'),
('pub', 'hstark16'),
('krg', 'eross10'),
('krg', 'echarles19');
insert into drone_pilots values
('awilson5', '314159', 41),
('lrodriguez5', '287182', 67),
('tmccall5', '181633', 10),
('agarcia7', '610623', 38),
('bsummers4', '411911', 35),
('fprefontaine6', '657483', 2);
insert into drones values
('pub', 1, 10, 3, 'awilson5'),
('pub', 2, 20, 2, 'lrodriguez5'),
('krg', 1, 15, 4, 'tmccall5'),
('pub', 9, 45, 1, 'fprefontaine6');
insert into products values
('pr_3C6A9R', 'pot roast', 6),
('ss_2D4E6L', 'shrimp salad', 3),
('hs_5E7L23M', 'hoagie sandwich', 3),
('clc_4T9U25X', 'chocolate lava cake', 5),
('ap_9T25E36L', 'antipasto platter', 4);
insert into orders values
('pub_303', '2024-05-23', 'sprince6', 'pub', 1),
('pub_305', '2024-05-22', 'sprince6', 'pub', 2),
('krg_217', '2024-05-23', 'jstone5', 'krg', 1),
('pub_306', '2024-05-22', 'awilson5', 'pub', 2);
insert into order_lines values
('pub_303', 'pr_3C6A9R', 20, 1),
('pub_303', 'ap_9T25E36L', 4, 1),
('pub_305', 'clc_4T9U25X', 3, 2),
('pub_306', 'hs_5E7L23M', 3, 2),
('pub_306', 'ap_9T25E36L', 10, 1),
('krg_217', 'pr_3C6A9R', 15, 2);
-- -----------------------------------------------
-- stored procedures and views
-- -----------------------------------------------
-- add customer
delimiter //
create procedure add_customer
(in ip_uname varchar(40), in ip_first_name varchar(100),
in ip_last_name varchar(100), in ip_address varchar(500),
in ip_birthdate date, in ip_rating integer, in ip_credit integer)
sp_main: begin
-- place your solution here
if (ip_uname is Null or ip_first_name is Null or ip_last_name is Null or ip_address is Null or ip_rating is Null or ip_credit is Null)
then leave sp_main;
elseif (ip_uname = '' or ip_first_name = '' or ip_last_name = '' or ip_address = '' or ip_rating < 1 or ip_rating > 5 or ip_credit < 0 )
then leave sp_main;
end if;
if ip_uname IN (select uname from customers)
then leave sp_main;
end if;
if (ip_uname in (select uname from users)) then
if ((ip_uname, ip_first_name, ip_last_name, ip_address, ip_birthdate) in (select * from users))
then INSERT INTO customers (uname, rating, credit) VALUES (ip_uname, ip_rating, ip_credit);
else leave sp_main;
end if;
else
INSERT INTO users (uname, first_name, last_name, address, birthdate) VALUES (ip_uname, ip_first_name, ip_last_name, ip_address, ip_birthdate);
INSERT INTO customers (uname, rating, credit) VALUES (ip_uname, ip_rating, ip_credit);
end if;
end //
delimiter ;
-- add drone pilot
delimiter //
create procedure add_drone_pilot
(in ip_uname varchar(40), in ip_first_name varchar(100),
in ip_last_name varchar(100), in ip_address varchar(500),
in ip_birthdate date, in ip_taxID varchar(40), in ip_service integer,
in ip_salary integer, in ip_licenseID varchar(40),
in ip_experience integer)
sp_main: begin
if (ip_uname is Null or ip_first_name is Null or ip_last_name is Null or ip_address is Null or ip_taxID is Null or ip_service is Null or ip_salary is Null or ip_licenseID is Null or ip_experience is Null)
then leave sp_main;
elseif (ip_uname = '' or ip_first_name = '' or ip_last_name = '' or ip_address = '' or ip_taxID = '' or ip_service < 0 or ip_salary < 0 or ip_licenseID = '' or ip_experience < 0)
then leave sp_main;
end if;
if (ip_uname IN (select uname from store_workers) OR
#ip_taxID IN (select taxID from employees) OR
ip_licenseID IN (select licenseID from drone_pilots))
then leave sp_main;
end if;
if (ip_uname in (select uname from users)) then
if (ip_uname, ip_first_name, ip_last_name, ip_address, ip_birthdate) not in (select * from users) then
leave sp_main;
end if;
else INSERT into users values (ip_uname, ip_first_name, ip_last_name, ip_address, ip_birthdate);
end if;
if (ip_taxID in (select taxID from employees)) then
if (ip_uname, ip_taxID, ip_service, ip_salary) not in (select * from employees) then
delete from users where uname = ip_uname;
leave sp_main;
end if;
else INSERT into employees values (ip_uname, ip_taxID, ip_service, ip_salary);
end if;
-- INSERT into users values
-- (ip_uname, ip_first_name, ip_last_name, ip_address, ip_birthdate);
--
-- INSERT into employees values
-- (ip_uname, ip_taxID, ip_service, ip_salary);
INSERT into drone_pilots values (ip_uname, ip_licenseID, ip_experience);
end //
delimiter ;
-- add product
delimiter //
create procedure add_product
(in ip_barcode varchar(40), in ip_pname varchar(100),
in ip_weight integer)
sp_main: begin
-- place your solution here
if (ip_barcode is Null or ip_pname is Null or ip_barcode = '' or ip_pname = '' or ip_weight <= 0)
then leave sp_main;
end if;
if ip_barcode in (select barcode from drone_dispatch.products)
then leave sp_main;
end if;
insert into products values (ip_barcode, ip_pname, ip_weight);
end //
delimiter ;
-- add drone
delimiter //
create procedure add_drone
(in ip_storeID varchar(40), in ip_droneTag integer,
in ip_capacity integer, in ip_remaining_trips integer,
in ip_pilot varchar(40))
sp_main: begin
if (ip_droneTag is Null or ip_capacity is Null or ip_remaining_trips is Null)
then leave sp_main;
elseif (ip_capacity <= 0 or ip_remaining_trips < 0)
then leave sp_main;
end if;
if (ip_storeID not in (select storeID from stores)) then leave sp_main;
end if;
if (ip_droneTag in (select droneTag from drones where storeID = ip_storeID)) then leave sp_main;
end if;
if (ip_pilot NOT IN (select uname from drone_pilots)) then leave sp_main;
end if;
if (ip_pilot IN (select pilot from drones)) then leave sp_main;
end if;
INSERT into drones values
(ip_storeID, ip_droneTag, ip_capacity, ip_remaining_trips, ip_pilot);
end //
delimiter ;
-- increase customer credits
delimiter //
create procedure increase_customer_credits
(in ip_uname varchar(40), in ip_money integer)
sp_main: begin
-- place your solution here
if ip_money > 0
then update customers set credit = credit + ip_money where uname = ip_uname;
end if;
end //
delimiter ;
-- swap drone control
delimiter //
create procedure swap_drone_control
(in ip_incoming_pilot varchar(40), in ip_outgoing_pilot varchar(40))
sp_main: begin
if (ip_incoming_pilot NOT IN (select uname from drone_pilots) OR
ip_outgoing_pilot NOT IN (select uname from drone_pilots) OR
ip_incoming_pilot NOT IN (select uname from drone_pilots left join drones on uname = pilot where droneTag is NULL) OR
ip_outgoing_pilot IN (select uname from drone_pilots left join drones on uname = pilot where droneTag is NULL))
then leave sp_main;
end if;
UPDATE drones set pilot = ip_incoming_pilot where pilot = ip_outgoing_pilot;
end //
delimiter ;
-- repair and refuel a drone
delimiter //
create procedure repair_refuel_drone
(in ip_drone_store varchar(40), in ip_drone_tag integer,
in ip_refueled_trips integer)
sp_main: begin
-- place your solution here
if ip_refueled_trips > 0
then update drone_dispatch.drones set remaining_trips = remaining_trips + ip_refueled_trips
where storeID = ip_drone_store and droneTag = ip_drone_tag;
end if;
end //
delimiter ;
-- begin order
delimiter //
create procedure begin_order
(in ip_orderID varchar(40), in ip_sold_on date,
in ip_purchased_by varchar(40), in ip_carrier_store varchar(40),
in ip_carrier_tag integer, in ip_barcode varchar(40),
in ip_price integer, in ip_quantity integer)
sp_main: begin
-- place your solution here
declare cost int;
declare capacity int;
declare item_weight int;
declare allocated_credit int;
select credit_already_allocated into allocated_credit from customer_credit_check where customer_name = ip_purchased_by;
select ip_price * ip_quantity into cost;
select drones.capacity into capacity from drones where droneTag = ip_carrier_tag and storeID = ip_carrier_store limit 1;
select products.weight into item_weight from products where barcode = ip_barcode limit 1;
if ip_orderID in (select orderID from orders)
then leave sp_main;
end if;
if (ip_orderID is Null or ip_sold_on is Null or ip_purchased_by is Null or ip_carrier_store is Null or ip_carrier_tag is Null or ip_barcode is Null or ip_price is Null or ip_quantity is Null)
then leave sp_main;
elseif (ip_orderID = '' or ip_carrier_store = '' or ip_barcode = '' or ip_purchased_by = '')
then leave sp_main;
end if;
if ip_price >= 0 and ip_quantity > 0
and (select credit from customers where uname = ip_purchased_by) >= cost + allocated_credit
and capacity >= ip_quantity * item_weight
then
insert into orders values (ip_orderID, ip_sold_on, ip_purchased_by, ip_carrier_store, ip_carrier_tag);
insert into order_lines values (ip_orderID, ip_barcode, ip_price, ip_quantity);
end if;
end //
delimiter ;
-- add order line
delimiter //
create procedure add_order_line
(in ip_orderID varchar(40), in ip_barcode varchar(40),
in ip_price integer, in ip_quantity integer)
sp_main: begin
-- place your solution here
declare cost int;
declare customer_uname varchar(40);
declare drone_weight int;
declare allocated_credit int;
select ip_price * ip_quantity into cost;
select purchased_by into customer_uname from orders where orderID = ip_orderID limit 1;
select sum(quantity*weight) into drone_weight from order_lines join products on products.barcode = order_lines.barcode
group by orderID having orderID = ip_orderID;
select credit_already_allocated into allocated_credit from customer_credit_check where customer_name = customer_uname;
if (ip_orderID is Null or ip_barcode is Null or ip_orderID = '' or ip_barcode = '')
then leave sp_main;
end if;
if ip_barcode not in (select barcode from order_lines where orderID = ip_orderID)
and ip_price >= 0 and ip_quantity > 0
and (select credit from customers where uname = customer_uname) >= cost + allocated_credit
and drone_weight + (select weight from products where barcode = ip_barcode) * ip_quantity <=
(select capacity from orders join drones on (carrier_store, carrier_tag) = (storeID, droneTag) where ip_orderID = orderID)
then
insert into order_lines values (ip_orderID, ip_barcode, ip_price, ip_quantity);
end if;
end //
delimiter ;
-- deliver order
delimiter //
create procedure deliver_order
(in ip_orderID varchar(40))
sp_main: begin
DECLARE tripsLeft int;
DECLARE droneNum int;
DECLARE store varchar(40);
DECLARE dronePilot varchar(40);
DECLARE buyer varchar(40);
DECLARE cost int;
if (ip_orderID NOT IN (select orderID from orders)) then leave sp_main;
end if;
select droneTag, storeID, remaining_trips, purchased_by, pilot into droneNum, store, tripsLeft, buyer, dronePilot
from orders join drones on carrier_store = storeID and carrier_tag = droneTag
where orderID = ip_orderID;
if (tripsLeft <= 0) then leave sp_main;
end if;
select temp.cost into cost from (select orderID, sum(price * quantity)
as 'cost' from orders natural join order_lines group by orderID) as temp
where orderID = ip_orderID;
UPDATE customers set credit = (credit - cost) where uname = buyer;
if ((select rating from customers where uname = buyer) < 5 and cost > 25) then
UPDATE customers set rating = rating + 1 where uname = buyer;
end if;
UPDATE stores set revenue = (revenue + cost) where storeID = store;
UPDATE drones set remaining_trips = (remaining_trips - 1) where (storeID = store AND droneTag = droneNum);
UPDATE drone_pilots set experience = (experience + 1) where uname = dronePilot;
DELETE from orders where orderID = ip_orderID;
end //
delimiter ;
-- cancel an order
delimiter //
create procedure cancel_order
(in ip_orderID varchar(40))
sp_main: begin
-- place your solution here
DECLARE orderCost INTEGER;
-- Calculate the total cost of the order to refund to the customer
SELECT SUM(price * quantity) INTO orderCost