-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw7.sql
234 lines (206 loc) · 5.42 KB
/
hw7.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
--1a
create or replace function setintersection(A anyarray, B anyarray)
returns anyarray as
$$ select ARRAY(select unnest(A)
intersect
select unnest(B));
$$ language sql;
--1b
create or replace function setdifference(A anyarray, B anyarray)
returns anyarray as
$$ select ARRAY(select unnest(A)
except
select unnest(B));
$$ language sql;
create or replace function memberof(x anyelement, A anyarray)
returns boolean as
$$
select x = SOME(A);
$$ language sql;
--2
create or replace view student_books as
select s.sid as sid, array(select t.bookno
from buys t
where t.sid = s.sid
order by bookno) as books
from student s
order by sid;
select *
from student_books;
--2a
create or replace view book_students as
select b.bookno as bookno,setunion(array(select t.sid
from buys t
where t.bookno = b.bookno
order by bookno),ARRAY[]::int[]) as students
from book b
order by bookno;
select *
from book_students;
--2b
create or replace view book_citedbooks as
select b.bookno,setunion(array(select c.citedbookno
from cites c
where c.bookno = b.bookno
order by bookno),ARRAY[]::int[]) as citedbooks
from book b
order by bookno;
select *
from book_citedbooks;
--2c
create or replace view book_citingbooks as
select b.bookno,setunion(array(select c.bookno
from cites c
where c.citedbookno = b.bookno
order by bookno),ARRAY[]::int[]) as citingbooks
from book b
order by bookno;
select *
from book_citingbooks;
--2d
create or replace view major_students as
select distinct m.major,array(select m1.sid
from major m1
where m1.major = m.major
order by sid) as students
from major m
order by major;
select *
from major_students;
--2e
create or replace view student_majors as
select s.sid,setunion(array(select m.major
from major m
where m.sid = s.sid
order by sid),ARRAY[]::VARCHAR[]) as majors
from student s
order by sid;
select *
from student_majors;
--3a
select sb.sid
from student_books sb
where cardinality(sb.books)=2;
--3b
select sb.sid
from student_books sb
where cardinality(setdifference((select sb.books
from student_books sb
where sb.sid=1001), sb.books))=0;
--3c
create or replace view book_less_than_30 as
select ARRAY(select b.bookno
from book b
where b.price<=30) as bookno;
select bc.bookno
from book_citedbooks bc,book_less_than_30
where cardinality(setdifference(bc.citedbooks,book_less_than_30.bookno))<2;
--3d
create or replace view t1(bookno,sid) as
SELECT bs.bookno,bs.students as sid
FROM book_students bs;
create or replace view t2(sid) as
select setintersection((select ms.students
from major_students ms
where ms.major='CS'),
(select ms.students
from major_students ms
where ms.major='Math')) as sid;
select distinct t1.bookno,b.title
from t1,t2,book b
where cardinality(setdifference(t1.sid,t2.sid))>0 and b.bookno=t1.bookno;
--3e
create or replace view e1 as
select array(
select b.bookno
from book b
where b.price<50) as bookno;
create or replace view e2 as
select array(
select bcting.bookno
from book_citingbooks bcting,e1
where cardinality(setintersection(bcting.citingbooks,e1.bookno))>=2) as bookno;
select sb.sid,unnest(setintersection(sb.books,e2.bookno))
from student_books sb,e2
where cardinality(setintersection(sb.books,e2.bookno))>=1;
--3f
select array(
select s.sid
from student s
where memberof(s.sid, (select setintersection((select ms.students
from major_students ms
where ms.major='CS'),
(select ms.students
from major_students ms
where ms.major='Math')) as sid))
) as students;
--3g
create or replace view g1(books) as
select sb.books
from student_books sb
where sb.sid=1001;
select sm.sid,sm.majors
from student_majors sm,student_books sb,g1
where sm.sid=sb.sid and cardinality(setintersection(sb.books,g1.books))=0;
--3h
select array(
select distinct p.*
from(
select unnest (sb.books)
from student_books sb,major_students ms
where sb.sid=some(ms.students) and ms.major='CS') p) as books;
--3i
select array(
select distinct p.*
from (select unnest (bs.students)
from book_students bs,book_citedbooks bc
where bs.bookno=bc.bookno and cardinality(bc.citedbooks)>=2) p
);
--3j
create or replace view j1 as
select ms.students
from major_students ms
where ms.major='CS';
select bs.bookno as b,setintersection(bs.students,j1.students)
from book_students bs,j1;
--3k
create or replace view k1 as
select array(
select unnest(sb.books) as bookno
from major_students ms,student_books sb
where ms.major='Math' and sb.sid=some(ms.students)) as bookno;
create or replace view k2 as
select array(
select unnest (bs.students)
from k1,book_students bs
where memberof(bs.bookno,k1.bookno)) as students;
select setdifference(ms.students,k2.students)
from major_students ms,k2
where ms.major='CS';
--3l
select bs1.bookno,bs2.bookno
from book_students bs1,book_students bs2
where bs1.bookno<>bs2.bookno
and cardinality(setdifference(bs1.students,bs2.students))=0
and cardinality(setdifference(bs2.students,bs1.students))=0;
--3m
create or replace view m1 as
select ms.students
from major_students ms
where ms.major='Math';
create or replace view m2 as
select ms.students
from major_students ms
where ms.major='CS';
select bs1.bookno,bs2.bookno
from book_students bs1,book_students bs2,m1,m2
where cardinality(setintersection(bs1.students,m1.students))<cardinality(setintersection(bs2.students,m2.students))
and bs1.bookno<>bs2.bookno;
--3n
create or replace view n1 as
select array(
select b.bookno
from book b where b.price>50) as bookno;
select sb.sid
from student_books sb,n1
where cardinality(setdifference(n1.bookno,sb.books))=1;