-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1.sql
192 lines (139 loc) · 3.62 KB
/
hw1.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
create database <JW_L>;
\c <JW_L>;
/*problem 1*/
create table hw1_sailor(
Sid INTEGER,
Sname VARCHAR(20),
Rating INTEGER,
Age INTEGER,
PRIMARY KEY (Sid)
);
create table hw1_boat(
Bid INTEGER,
Bname VARCHAR(15),
Color VARCHAR(15),
PRIMARY KEY (Bid)
);
create table hw1_reserves(
Sid INTEGER,
Bid INTEGER,
Day VARCHAR(20),
FOREIGN KEY (Sid) REFERENCES hw1_sailor(Sid),
FOREIGN KEY (Bid) REFERENCES hw1_boat(Bid)
);
insert into hw1_boat (Bid,Bname,Color)values
(101,'Interlake','blue'),
(102,'Sunset','red'),
(103,'Clipper','green'),
(104,'Marine','red');
insert into hw1_sailor (Sid,Sname,Rating,Age)values
(22,'Dustin',7,45),
(29,'Brutus',1,33),
(31,'Lubber',8,55),
(32,'Andy',8,25),
(58,'Rusty',10,35),
(64,'Horatio',9,35),
(71,'Zorba',10,16),
(74,'Horatio',9,35),
(85,'Art',3,25),
(95,'Bob',3,63);
insert into hw1_reserves (Sid,Bid,Day)values
(22,101,'Monday'),
(22,102,'Tuesday'),
(22,103,'Wednesday'),
(31,102,'Thusrday'),
(31,103,'Friday'),
(31,104,'Saturday'),
(64,101,'Sunday'),
(64,102,'Monday'),
(74,102,'Saturday');
/*problem 2*/
/*add into primary key*/
Insert into hw1_sailor (Sid,Sname,Rating,Age)
values (22,'Allen',2,56);
/*ERROR: insert or update on table "hw1_sailor" violates primary key constraint*/
/*add into foreign key*/
Insert into hw1_reserves (Sid,Bid,Day)
values (80,104,'Sunday');
/*ERROR: insert or update on table "hw1_reserves" violates foreign key constraint */
/*alter table drop foreign key*/
ALTER TABLE hw1_reserves
DROP CONSTRAINT Sid;
/*alter table drop primary key*/
ALTER TABLE hw1_sailor
DROP CONSTRAINT Sid;
/*insert when no primary key*/
Insert into hw1_reserves (Sid,Bid,Day)
values (80,'Allen',2,56);
/*no primary key violation*/
/*delete the inserted tuple*/
Delete from hw1_reserves
where Sid=80;
/*alter table add back primary key*/
alter TABLE hw1_sailor
ADD PRIMARY KEY Sid;
/*alter table add back foreign key*/
alter TABLE hw1_reserves
ADD FOREIGN KEY Sid;
/*problem 3*/
/*3.a*/
select S.Rating
from hw1_sailor S;
/*3.b*/
select B.Bid, B.color
from hw1_boat B;
/*3.c*/
select S.Sname
from hw1_sailor S
where S.Age>=15 and S.Age<=30;
/*3.d*/
select B.Bname
from hw1_boat B,hw1_reserves R
where B.Bid=R.Bid and (R.Day='Saturday'or R.Day='Sunday');
/*3.e*/
select distinct S1.Sname
from hw1_sailor S1,hw1_boat B1,hw1_reserves R1
where S1.Sid = R1.Sid and B1.Color ='red' and B1.Bid=R1.Bid
intersect
select distinct S2.Sname
from hw1_sailor S2,hw1_boat B2,hw1_reserves R2
where S2.Sid = R2.Sid and B2.Color ='green'and B2.Bid=R2.Bid;
/*3.f */
select distinct s.sname
from
(
select distinct S1.sid, s1.sname
from hw1_sailor S1,hw1_boat B1,hw1_reserves R1
where S1.Sid = R1.Sid and R1.Bid=B1.Bid and B1.Color='red'
EXCEPT
select distinct S2.sid, s2.sname
from hw1_sailor S2,hw1_boat B2,hw1_reserves R2
where S2.Sid = R2.Sid And R2.Bid=B2.Bid and (B2.Color='green'
or B2.Color='blue' )
) s;
/*3.g*/
select distinct S.Sname
from hw1_sailor S,hw1_boat B,hw1_reserves R1,hw1_reserves R2
where S.Sid = R1.Sid and R1.Sid=R2.Sid and R1.Bid <> R2.Bid;
/*3.h*/
select S.Sid
from hw1_sailor S,hw1_reserves R
where S.Sid <> R.Sid
EXCEPT
select S1.Sid
from hw1_sailor S1,hw1_sailor S2,hw1_reserves R
where S1.Sid = R.Sid and S1.Sid = S2.Sid;
/*3.i*/
select S1.Sid,S2.Sid
from hw1_sailor S1,hw1_sailor S2,hw1_reserves R
where S1.Sid <> S2.Sid and S1.Sid = R.Sid and R.Day='Saturday';
/*3.j */
select B.Bid
from hw1_sailor S,hw1_boat B,hw1_reserves R1,hw1_reserves R2
where B.Bid = R1.Bid
EXCEPT
select B.Bid
from hw1_sailor S,hw1_boat B,hw1_reserves R1,hw1_reserves R2
where R1.Sid <> R2.Sid and R1.Bid = R2.Bid and B.Bid = R1.Bid;
\c postgres;
drop database <JW_L>;