0% found this document useful (0 votes)
46 views6 pages

DBMS Assignment 4

The document contains SQL queries to create tables for sailors, boats, and reservations. It then inserts data into the tables and provides example queries to retrieve information from the tables, including: 1) Finding sailors who reserved a specific boat 2) Selecting sailor names and details where their rating is present 3) Finding the color of boats reserved by a specific sailor 4) Finding sailor names that have made at least one reservation

Uploaded by

subham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views6 pages

DBMS Assignment 4

The document contains SQL queries to create tables for sailors, boats, and reservations. It then inserts data into the tables and provides example queries to retrieve information from the tables, including: 1) Finding sailors who reserved a specific boat 2) Selecting sailor names and details where their rating is present 3) Finding the color of boats reserved by a specific sailor 4) Finding sailor names that have made at least one reservation

Uploaded by

subham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Table creation.

Query:
CREATE TABLE SAILOR(SID VARCHAR(32) NOT NULL,SNAME VARCHAR(32),MNAME
VARCHAR(32),SURNAME VARCHAR(32),RATING INTEGER,AGE REAL, CONSTRAINT
PK_SAILOR PRIMARY KEY (SID));
CREATE TABLE BOAT(BID VARCHAR(32) NOT NULL,BNAME VARCHAR(32),COLOR
VARCHAR(32), CONSTRAINT PK_BOAT PRIMARY KEY (BID));
CREATE TABLE RESERVE(SID VARCHAR(32) NOT NULL,BID VARCHAR(32) NOT
NULL,R_DAY VARCHAR(32),CONSTRAINT PK_RESERVE PRIMARY KEY
(SID,BID,R_DAY),FOREIGN KEY (SID) REFERENCES SAILOR(SID),FOREIGN KEY (BID)
REFERENCES BOAT(BID));
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s22','Fredrico','','Roberts',7,45);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s31','Lubber','','Sheen',8,55.5);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s32','Charlotte','','Gordin',8,25.5);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s58','Mary','Beth','Lyon',10,35);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s64','Horatio','','Powell',7,35.4);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s71','Zorba','','Alex',10,16);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s29','Brutus','','Slater',1,33.8);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s95','Deep','Graceb','Davis',3,63.
5);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s74','Horatio','','Forrest',9,35);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s85','Sara','Art','Powell',3,25.5);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s80','Deep','Kumar','Kumar',6,17
);
INSERT INTO
SAILOR(SID,SNAME,MNAME,SURNAME,RATING,AGE)VALUES('s87','Deep','Kumar','JHA',8,51);
INSERT INTO BOAT(BID,BNAME,COLOR)VALUES('b11','Interlake','blue');
INSERT INTO BOAT(BID,BNAME,COLOR)VALUES('b12','Interlake','red');
INSERT INTO BOAT(BID,BNAME,COLOR)VALUES('b13','Clipper','green');
INSERT INTO BOAT(BID,BNAME,COLOR)VALUES('b14','Marine','red');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s22','b11','10-OCT-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s22','b13','10-AUG-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s22','b12','10-OCT-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s22','b14','10-JUL-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s31','b12','11-OCT-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s31','b12','11-JUN-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s31','b14','11-DEC-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s64','b11','09-MAY-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s64','b12','09-AUG-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s74','b13','09-AUG-98');

INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s80','b12','07-JUL-98');


INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s87','b11','08-JUL-98');
INSERT INTO RESERVE(SID,BID,R_DAY)VALUES('s87','b12','12-DEC-98');
SELECT * FROM SAILOR;
SELECT * FROM BOAT;
SELECT * FROM RESERVE;
Output:
S
R
S MU
A
SN NR A
T
IA AN G
I
DM MA E
N
E EM
G
E
L
S
u 5
s h
b 5
3 -e 8
b .
1 e
e 5
n
r
C
h
G
a
o 2
sr
r 5
3l - 8
d .
2o
i 5
t
n
t
e
G
D
D r 6
s a
e a 3
9 v3
e c .
5 i
p e 5
s
b
Z
A
so
l 11
7r -
e 06
1b
x
a
F
R
r
o
e
s b
d 4
2 -e 7
r 5
2 r
i
t
c
s
o
sH - F 9 3
o o
r r
7a r
5
4t e
i s
o t
P
S o 2
s A
a w 5
8 r 3
r e .
5 t
a l 5
l
M BL
s
a ey 13
5
r to 05
8
y hn
B S
r l 3
s
u a 3
2 - 1
t t .
9
u e 8
s r
H
P
o
o 3
sr
w 5
6a - 7
e .
4t
l 4
i
l
o
KK
D
s uu
e 1
8 mm 6
e 7
0 aa
p
rr
K
D
s uJ
e 5
8 mH 8
e 1
7 aA
p
r

BID BNAME COLOR


b11 Interlake blue
b12 Interlake red
b14 Marine red
b13 Clipper green

SID BID R_DAY


s22 b11 10-OCT-98
s22 b12 10-OCT-98
s22 b13 10-AUG-98
s22 b14 10-JUL-98
s31 b12 11-JUN-98
s31 b12 11-OCT-98
s31 b14 11-DEC-98
s64 b11 09-MAY-98
s64 b12 09-AUG-98
s74 b13 09-AUG-98
s80 b12 07-JUL-98
s87 b11 08-JUL-98
s87 b12 12-DEC-98
SID
s31
1)Find sid of sailors who have reserved boat ‘b14’
s22 Query:
select distinct s.SID from SAILOR s,RESERVE r where s.SID=r.SID and r.BID='b14';
Output:

2)Select names of the sailor for whom rating is present.


Query:
select s.SNAME,s.MNAME,s.SURNAME from SAILOR s where s.RATING != 0;
Output:
MNAM SURNA
SNAME
E ME
Lubber - Sheen
Charlotte - Gordin
Deep Graceb Davis
Zorba - Alex
Fredrico - Roberts
Horatio - Forrest
Sara Art Powell
Mary Beth Lyon
Brutus - Slater
Horatio - Powell
Deep Kumar Kumar
Deep Kumar JHA
3)Select names for rating absent.
Query:
select s.SNAME,s.MNAME,s.SURNAME from SAILOR s where s.RATING = 0;
Output:
no data found
4)Find color of boats reserved by Lubber.
Query:
select distinct b.COLOR from SAILOR s,BOAT b , RESERVE R where s.SID=r.SID and
r.BID=b.BID and s.SNAME='Lubber';
Output:
COL
OR
red

5)Find a sailor name that have reserved at least one boat.


Query:
select distinct s.SNAME,s.MNAME,s.SURNAME from SAILOR s,RESERVE r where s.SID=r.SID;
Output:
SNAM MNAM SURNA
E E ME
Deep Kumar Kumar
Horatio - Powell
Horatio - Forrest
Deep Kumar JHA
Fredric
- Roberts
o
Lubber - Sheen
6)Find the ids and names of sailors who have reserved two different boats on the same day.
Query:
SELECT DISTINCT S.SID, S.SNAME,S.MNAME,S.SURNAME FROM SAILOR S, RESERVE
R1, RESERVE R2 WHERE S.SID = R1.SID AND S.SID = R2.SID AND R1.R_DAY = R2.R_DAY
AND R1.BID <> R2.BID;
Output:
SI SNAM MNAM SURNA
D E E ME
Fredric
s22 - Roberts
o
7)Find name of sailors whose name begins and ends with B and has at least 3 characters.
Query:
select distinct S.SNAME,S.MNAME,S.SURNAME FROM SAILOR S where S.SNAME like 'B_
%B';
Output:
no data found
8)Find names of sailors whose name begins and ends with 'B' and has exactly 3 chars.
Query:
select distinct S.SNAME,S.MNAME,S.SURNAME FROM SAILOR S where S.SNAME like 'B_B';
Output:
no data found
9)Display all names(including duplicates) of sailors who have reserved a red boat or a green
boat.
Query:
select s1.sname from SAILOR s1,RESERVE r1,BOAT b1 where s1.sid=r1.sid and r1.bid=b1.bid and
b1.color='red' union select s2.sname from SAILOR s2,RESERVE r2,BOAT b2 where s2.sid=r2.sid and
r2.bid=b2.bid and b2.color='green';
Output:
SNAME
Deep
Fredrico
Horatio
Lubber

10)Find names of sailors who have reserved a red boat or a green boat.
Query:
select distinct s.sname from SAILOR s,BOAT b, RESERVE r where s.sid=r.sid and r.bid=b.bid and
(b.color='red' or b.color='green');
Output:
SNAM
E
Fredric
o
Lubber
Horatio
Deep

11)Find names of sailors who have reserved a red boat and a green boat.
Query:
select s1.sname from SAILOR s1,RESERVE r1,BOAT b1 where s1.sid=r1.sid and r1.bid=b1.bid and
b1.color='red' intersect select s2.sname from SAILOR s2,RESERVE r2,BOAT b2 where s2.sid=r2.sid
and r2.bid=b2.bid and b2.color='green';
Output:
SNAM
E
Fredric
o
Horatio
12)Find names of sailors who have reserved a red boat but not a green boat.
Query:
select distinct s1.sname from SAILOR s1,RESERVE r1,BOAT b1 where s1.sid=r1.sid and
r1.bid=b1.bid and b1.color='red' minus select distinct s2.sname from SAILOR s2,RESERVE
r2,BOAT b2 where s2.sid=r2.sid and r2.bid=b2.bid and b2.color='green';
Output:
SNAM
E
Deep
Lubber

You might also like