-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw3.sql
973 lines (824 loc) · 17.5 KB
/
hw3.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
create database jw_l;
\c jw_l;
--Q1)
create table A(
x INTEGER
);
insert into A (x)values
(1),
(2),
(3),
(4),
(5)
;
select a.x, sqrt(a.x) as square_root_x,power(a.x,2) as x_squared, power(2,a.x) as two_to_the_power_x, factorial(a.x) as x_factorial,log(a.x) as logarithm_x
from A a;
--Q2)
create table A(
x INTEGER
);
create table B(
x INTEGER
);
insert into A (x)values
(1),
(2),
(3)
;
insert into B (x)values
(1),
(3),
(4),
(5)
;
select not exists(select a.x
from A a
except
select b.x
from B b) as empty_a_minus_b,
exists((select a.x
from A a
except
select b.x
from B b)
union
(select b.x
from B b
except
select a.x
from A a)) as not_empty_symmetric_difference,
not exists(select a.x
from A a
intersect
select b.x
from B b) as empty_a_intersection_b;
--Q3)
create table Pairs(
x INTEGER,
y INTEGER
);
insert into Pairs (x,y)values
(1,1),
(2,2),
(3,4),
(4,6),
(5,9),
(1,3),
(5,2),
(7,7)
;
select P1.x as x1,P1.y as y1,P2.x as x2,P2.y as y2
from pairs P1, pairs P2
where (P1.x<>P2.x and P1.y<>P2.y)
and P1.x+P1.y=P2.x+P2.y;
--Q4)
create table A(
p boolean
);
create table B(
Q boolean
);
create table C(
R boolean
);
insert into A(P) values
(TRUE),
(FALSE),
(NULL);
insert into B(Q) values
(TRUE),
(FALSE),
(NULL);
insert into C(R) values
(TRUE),
(FALSE),
(NULL);
select a.p,b.q, c.r, (not (
not A.p
or
B.q
)
or
C.r
)
from A,B,C;
--Q5)
--a)
--a1)
select EXISTS(SELECT A.x
FROM A
intersect
SELECT B.x
FROM B
) as answer;
--a2)
select EXISTS(SELECT A.x
FROM A
where A.x in
(SELECT B.x
FROM B)
) as answer;
--b)
--b1)
select not EXISTS(SELECT A.x
FROM A
except
SELECT B.x
FROM B
) as answer;
--b2)
select not EXISTS(SELECT A.x
FROM A
where A.x not in
(SELECT B.x
FROM B
)) as answer;
--c)
--c1)
select not EXISTS(SELECT B.x
FROM B
except
SELECT A.x
FROM A
) as answer;
--c2)
select not EXISTS(SELECT B.x
FROM B
where B.x not in
(SELECT A.x
FROM A
)
) as answer;
--d)
--d1)
select EXISTS(SELECT A.x
FROM A
except
SELECT B.x
FROM B
)
union
select EXISTS(SELECT B.x
FROM B
except
SELECT A.x
FROM A
)
;
--d2)
select EXISTS(SELECT A.x
FROM A
where A.x not in
(SELECT B.x
FROM B
)
)
or
EXISTS(SELECT B.x
FROM B
where B.x not in
(SELECT A.x
FROM A
)
)
as answer;
--e)
--e1)
with intersection as
(select A.x
from A, B
where A.x=B.x)
select not exists (
select i1.x
from intersection i1, intersection i2, intersection i3
where i1.x<>i2.x and i2.x<>i3.x and i1.x<>i3.x
)
;
--e2)
select not exists (
select A1.x
from A A1,A A2,A A3, B B1,B B2,B B3
where A1.x<>A2.x and A2.x<>A3.x and A1.x<>A3.x
and A1.x=B1.x and A2.x=B2.x and A3.x=B3.x
and B1.x<>B2.x and B1.x<>B3.x and B3.x<>B2.x
)
;
--f)
--f1)
select not EXISTS((SELECT A.x
FROM A
union
SELECT B.x
FROM B)
except
SELECT C.x
FROM C
) as answer;
--f2)
select not EXISTS(SELECT A.x
FROM A
where A.x NOT IN
(SELECT C.x
FROM C)
)
OR
not EXISTS(SELECT B.x
FROM B
where B.x NOT IN
(SELECT C.x
FROM C)
) as answer;
--g)
--g1)
with u as
((select A.x
from A
except
select B.x
from B)
union
(select B.x
from B
except
select C.x
from C)
)
select exists (
select u.x
from u
) and not exists (
select u1.x
from u u1, u u2
where u1.x<>u2.x
)
;
--g2)
select
(
exists (
select A.x
from A
where A.x not in(
select B.x
FROM B)
) or exists (select B.x
from B
where B.x not in(
select C.x
FROM C)
)
)
and
(
not exists (
select A1.x,A2.x
from A A1, A A2
where A1.x<>A2.x AND A1.x NOT IN
(SELECT B.x
from B)
and A2.x NOT IN
(SELECT B.x
from B)
)
)
and
(
not
exists (
select B1.x,B2.x
from B B1, B B2
where B1.x<>B2.x AND B1.x NOT IN
(SELECT C.x
from C)
and B2.x NOT IN
(SELECT C.x
from C)
)
)
and
(
not
exists (
select distinct A.x, B.x
from A,B,C
where A.x NOT IN
(SELECT B1.x
from B B1)
and B.x NOT IN
(SELECT C1.x
from C C1)
and A.x<>B.x
)
)
;
--Q6
--use count aggregate to solve Q5
--a)
select exists
(
SELECT COUNT(1)
FROM (SELECT A.x
FROM A
INTERSECT
SELECT B.x
FROM B
) q)>=1
;
--b)
select not exists
(
SELECT COUNT(1)
FROM (SELECT A.x
FROM A
except
SELECT B.x
FROM B
) q)>=1
;
--c)
select not exists
(
SELECT COUNT(1)
FROM (SELECT B.x
FROM B
except
SELECT A.x
FROM A
) q)>=1
;
--d)
select
not EXISTS
(select count(1)
from
(
(SELECT A.x
FROM A
except
SELECT B.x
FROM B)
union
(SELECT B.x
FROM B
except
SELECT A.x
FROM A)
) q
)
≥1;
--e)
select
not EXISTS
(select count(1)
from
(
(SELECT A.x
FROM A
intersect
SELECT B.x
FROM B) q
)
≥2;
--f)
select
not EXISTS
(select count(1)
from
(
(SELECT A.x
FROM A
union
SELECT B.x
FROM B)
except
SELECT C.x
FROM C) q
)
≥1;
--g)
select
(
select count(1)
from
( (SELECT A.x
FROM A
except
SELECT B.x
FROM B)
union
(SELECT B.x
FROM B
except
SELECT C.x
FROM C)) q
)
= 1
;
--Q7)
create table W(
A INTEGER,
B VARCHAR(5)
);
insert into W (A,B)values
(1,'John'),
(2,'Ellen'),
(3,'Ann'),
(4,'Ann')
;
CREATE VIEW W_view AS
SELECT W.A, W.B
FROM W;
SELECT distinct WV.A
FROM W_view WV, W
where (WV.A=W.A and WV.B<>W.B
and exists (
SELECT WV.A
FROM W_view WV, W
where WV.A=W.A and WV.B<>W.B))
or not exists (
SELECT WV.A
FROM W_view WV, W
where WV.A=W.A and WV.B<>W.B)
;
--Q8)
--a)
--a1)
CREATE FUNCTION booksBoughtbyStudent (studentid INTEGER,OUT BookNo int, OUT title VARCHAR(30),OUT price int)
RETURNS SETOF RECORD
AS
$$
SELECT BO.BookNo, BO.title, BO.price
from Book BO, Student S, Buys BU
where S.Sid=studentid and S.Sid=BU.Sid and BU.BookNo=BO.BookNo
;
$$ LANGUAGE SQL;
--a2)
SELECT distinct t.BookNo, t.title,t.price
FROM booksBoughtbyStudent (1001) t;
SELECT distinct t.BookNo, t.title,t.price
FROM booksBoughtbyStudent (1015) t;
--a3)
--a3A)
select S.Sid,S.Sname
from Student S
where
(select count(1)
from
(SELECT distinct t.BookNo
FROM booksBoughtbyStudent (S.Sid) t
where t.price<50) q
)=1
;
--a3B)
select distinct S1.Sid,S2.Sid
from Student S1,Student S2
where not exists (select t1.BookNo
from booksBoughtbyStudent (S1.Sid) t1
where t1.BookNo not in
(select t2.BookNo
from booksBoughtbyStudent (S2.Sid) t2))
and not exists
(select t2.BookNo
from booksBoughtbyStudent (S2.Sid) t2
where t2.BookNo not in
(select t1.BookNo
from booksBoughtbyStudent (S1.Sid) t1))
and S1.Sid<>S2.Sid
;
--b)
--b1)
CREATE FUNCTION studentsWhoBoughtStudent (bn INTEGER,OUT sid int, OUT sname VARCHAR(15))
RETURNS SETOF RECORD
AS
$$
SELECT S.Sid, S.Sname
from Book BO, Student S, Buys BU
where S.Sid=BU.Sid and BU.BookNo=BO.BookNo and BO.BookNo=bn
;
$$ LANGUAGE SQL;
--b2)
SELECT distinct t.sid, t.sname
FROM studentsWhoBoughtStudent (2010) t;
--b3)
select distinct BU.BookNo
from Buys BU
where
(
select count(1)
from
(select distinct TS.Sid
from studentsWhoBoughtStudent(BU.BookNo) TS, Major M
where TS.Sid=M.Sid and M.Major='CS' and exists (select BS.BookNo
from booksBoughtbyStudent (TS.Sid) BS
where BS.price>30)) p)>=2;
--c)
--c1)
select distinct S.Sid, M.Major
from Student S,Major M
where
S.Sid=M.Sid
and
(select count(1)
from
(SELECT distinct t.BookNo
FROM booksBoughtbyStudent (S.Sid) t
where t.price>30) q
)>=4;
--c2)
select distinct S1.Sid,S2.Sid
from Student S1,Student S2, booksBoughtbyStudent (S1.Sid) t1, booksBoughtbyStudent (S2.Sid) t2
where (SELECT SUM(t1.price) as sumprice1
FROM booksBoughtbyStudent (S1.Sid) t1) =
(SELECT SUM(t2.price) as sumprice2
FROM booksBoughtbyStudent (S2.Sid) t2)
and S1.Sid<>S2.Sid
;
--c3)
select distinct S.Sid, S.Sname
from Student S
where
(select sum(t1.price)
from booksBoughtbyStudent (S.Sid) t1
)
> (
select avg(v.l)
from (SELECT sum(q.t) as l
from (select p.k, p.t
from (
select S1.Sid as k,t2.price as t
from Student S1,Major M,booksBoughtbyStudent (S1.Sid) t2
where S1.Sid=M.Sid and M.Major='CS'
) p) q
GROUP BY(q.k)) v
)
;
--c4)
select t.bookno, t.title
from (
select BO.BookNo,BO.title,BO.price,rank() OVER (ORDER BY price desc)
from Book BO
) t
where t.rank=3;
--c5)
select distinct BO.bookno, BO.title
from Book BO
where (select count(1)
from (
select t.sid
from studentsWhoBoughtStudent (BO.bookno) t
except
select M.Sid
from Major M
where M.Major='CS') q
)=0;
--c6)
select distinct S.Sid, S.Sname
from Student S
where exists (select t.BookNo
from booksBoughtbyStudent (S.Sid) t
where t.BookNo in
(select distinct BU.BookNo
from Buys BU
where (
select count(1)
from (select t.Sid
from studentsWhoBoughtStudent(BU.BookNo) t,Major M
where t.sid=M.Sid and M.Major='CS') q
)<2));
--c7)
select distinct S1.Sid, t.BookNo
from Student S1,booksBoughtbyStudent (S1.Sid) t
where t.price< all(
select AVG(t1.price)
from booksBoughtbyStudent (S1.Sid) t1
);
--c8)
select distinct S1.Sid, S2.Sid
from Student S1,Student S2, Major M1,Major M2, booksBoughtbyStudent (S1.Sid) t
where S1.Sid<>S2.Sid and M1.Sid=S1.Sid and M2.Sid=S2.Sid and M1.Major=M2.Major and
(select count(1)
from (select t1.BookNo
from booksBoughtbyStudent (S1.Sid) t1
) p)
=
(select count(1)
from (select t2.BookNo
from booksBoughtbyStudent (S2.Sid) t2
) q)
;
--c9)
select distinct S1.Sid, S2.Sid, (select count(1) from (select t1.BookNo
from booksBoughtbyStudent (S1.Sid) t1
except
select t2.BookNo
from booksBoughtbyStudent (S2.Sid) t2
) p) as n
from Major M1,Major M2,Student S1,Student S2, booksBoughtbyStudent (S1.Sid) t1,booksBoughtbyStudent (S2.Sid) t2
where S1.Sid<>S2.Sid and S1.Sid=M1.SID AND S2.Sid=M2.SID and M1.Major=M2.Major
;
--c10)
select distinct BU.BookNo
from Buys BU
where
(select count(1)
from (select TS.Sid
from studentsWhoBoughtStudent(BU.BookNo) TS, Major M
where TS.Sid=M.Sid and M.Major='CS') q)
=
(select count(1)
from (select M.Sid
from Major M
where M.Major='CS') p)-1
;
--Q9)
--data preparation
create table Student(
Sid INT,
Sname text,
PRIMARY KEY (Sid)
);
create table Course(
cno INT,
cname text,
total int,
Max int,
PRIMARY KEY (Cno)
);
create table Prerequisite(
cno INT,
prereq int,
FOREIGN KEY (cno) REFERENCES course (cno),
FOREIGN KEY (cno) REFERENCES course (cno)
);
create table HasTaken(
Sid INT,
cno int,
FOREIGN KEY (sid) REFERENCES student (sid),
FOREIGN KEY (cno) REFERENCES course (cno)
);
create table Enroll(
Sid INT,
cno int,
FOREIGN KEY (sid) REFERENCES student (sid),
FOREIGN KEY (cno) REFERENCES course (cno)
);
create table Waitlist(
Sid INT,
cno int,
Position int,
FOREIGN KEY (sid) REFERENCES student (sid),
FOREIGN KEY (cno) REFERENCES course (cno)
);
INSERT INTO Course(cno,cname,total,max)values(201,'Programming',0,3);
INSERT INTO Course(cno,cname,total,max)values(202,'Calculus',0,3);
INSERT INTO Course(cno,cname,total,max)values(203,'Probability',0,3);
INSERT INTO Course(cno,cname,total,max)values(204,'AI',0,2);
INSERT INTO Course(cno,cname,total,max)values(301,'DiscreteMathematics',0,2);
INSERT INTO Course(cno,cname,total,max)values(302,'OS',0,2);
INSERT INTO Course(cno,cname,total,max)values(303,'Databases',0,2);
INSERT INTO Course(cno,cname,total,max)values(401,'DataScience',0,2);
INSERT INTO Course(cno,cname,total,max)values(402,'Networks',0,2);
INSERT INTO Course(cno,cname,total,max)values(403,'Philosophy',0,2);
INSERT INTO Prerequisite(cno,prereq)values(301,201);
INSERT INTO Prerequisite(cno,prereq)values(301,202);
INSERT INTO Prerequisite(cno,prereq)values(302,201);
INSERT INTO Prerequisite(cno,prereq)values(302,202);
INSERT INTO Prerequisite(cno,prereq)values(302,203);
INSERT INTO Prerequisite(cno,prereq)values(401,301);
INSERT INTO Prerequisite(cno,prereq)values(401,204);
INSERT INTO Prerequisite(cno,prereq)values(402,302);
INSERT INTO Student(sid,sname)values(1,'Jean');
INSERT INTO Student(sid,sname)values(2,'Eric');
INSERT INTO Student(sid,sname)values(3,'Ahmed');
INSERT INTO Student(sid,sname)values(4,'Qin');
INSERT INTO Student(sid,sname)values(5,'Filip');
INSERT INTO Student(sid,sname)values(6,'Pam');
INSERT INTO Student(sid,sname)values(7,'Lisa');
INSERT INTO Hastaken(sid,cno)values(1,201);
INSERT INTO Hastaken(sid,cno)values(1,202);
INSERT INTO Hastaken(sid,cno)values(1,301);
INSERT INTO Hastaken(sid,cno)values(2,201);
INSERT INTO Hastaken(sid,cno)values(2,202);
INSERT INTO Hastaken(sid,cno)values(3,201);
INSERT INTO Hastaken(sid,cno)values(4,201);
INSERT INTO Hastaken(sid,cno)values(4,202);
INSERT INTO Hastaken(sid,cno)values(4,203);
INSERT INTO Hastaken(sid,cno)values(4,204);
INSERT INTO Hastaken(sid,cno)values(5,201);
INSERT INTO Hastaken(sid,cno)values(5,202);
INSERT INTO Hastaken(sid,cno)values(5,301);
INSERT INTO Hastaken(sid,cno)values(5,204);
--requirement1:the prerequisites requirement
CREATE OR REPLACE FUNCTION check_prerequisites_constraint()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
IF exists (
select Prerequisite.prereq
from Prerequisite
where Prerequisite.prereq not IN (SELECT cno
FROM hastaken
where hastaken.sid=new.sid)
and Prerequisite.CNO=new.cno
) THEN
RAISE EXCEPTION 'not satisfy prerequisite';
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER check_prerequisites_constraint
BEFORE INSERT ON enroll
FOR EACH ROW EXECUTE PROCEDURE check_prerequisites_constraint();
--INSERT INTO enroll(sid,cno)values(1,402); ERROR: not satisfy prerequisite
--the increment of total enrollment
CREATE FUNCTION increment_the_enrollcourse_number()
RETURNS TRIGGER
AS $$
BEGIN
UPDATE course SET total = total + 1
where course.CNO=NEW.cno;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER increment_the_enrollcourse
AFTER INSERT ON enroll
FOR EACH ROW EXECUTE PROCEDURE increment_the_enrollcourse_number();
--INSERT INTO enroll(sid,cno)values(4,302); total of 302 will become 1
--constraint 2: the maximum constraint
CREATE OR REPLACE FUNCTION check_maximun_constraint()
RETURNS TRIGGER
AS $$
BEGIN
IF ( select exists (select course.total
from course
where course.total <= course.max and course.CNO=new.cno) ) THEN
INSERT INTO Waitlist(Sid,cno,position) VALUES (NEW.sid, NEW.cno, count_the_waitlist (new.cno));
END IF;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE FUNCTION count_the_waitlist (corsno INT)
RETURNS int AS
$$
select CAST (p.cont+1 AS int)
from (select count(1) as cont
from waitlist
where waitlist.cno=corsno) p
$$ LANGUAGE SQL;
CREATE TRIGGER check_maximun_no_constraint
BEFORE INSERT ON enroll
FOR EACH ROW EXECUTE PROCEDURE check_maximun_constraint();
--when the insertion to the enrollment exceed the maximun, it will show:ERROR: exceed the maximun
--requirments3: drop course and the waitlist get enrolled
CREATE OR REPLACE FUNCTION drop_a_course()
RETURNS TRIGGER
AS $$
BEGIN
--when there are students on the waitlist
IF exists (select waitlist.sid
from waitlist
where waitlist.position>=1 and waitlist.CNO=old.cno)
THEN
INSERT INTO enroll VALUES(sid_of_waitlist (old.cno), waitlist.cno);
--no one on the waitlist
elsif not exists (select waitlist.sid
from waitlist
where waitlist.position>=1 and waitlist.CNO=old.cno)
THEN
UPDATE course
SET total = total-1
where course.cno=old.cno;
END IF;
END;
$$ LANGUAGE 'plpgsql';
CREATE FUNCTION sid_of_waitlist (corsno INT)
RETURNS int AS
$$
select sid
from waitlist
where waitlist.position=1 and waitlist.cno=corsno
$$ LANGUAGE SQL;
CREATE TRIGGER drop_course
AFTER DELETE ON enroll
FOR EACH ROW
EXECUTE PROCEDURE drop_a_course();
--requirements 4:drop waitlist by himself and adjust on the waitlist
CREATE OR REPLACE FUNCTION remove_from_waitlist_byhimself()
RETURNS TRIGGER
AS $$
BEGIN
IF ( select exists (select waitlist.position
from waitlist
where waitlist.position > old.position and course.CNO=old.cno)) THEN
UPDATE Waitlist SET position = position - 1
where (waitlist.position > old.position and course.cno=old.cno);END IF;
RETURN OLD;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER selfremove_from_waitlist
AFTER DELETE ON waitlist
FOR EACH ROW
EXECUTE PROCEDURE remove_from_waitlist_byhimself();
\c postgres;
drop database jw_l;