0% found this document useful (0 votes)
8 views

DBMS Lab Exp

Uploaded by

ipathan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DBMS Lab Exp

Uploaded by

ipathan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DBMS LAB EXP-5

Sailors Relation

Sid sname rating Age

22 Dustin 7 45.0

29 Brutus 1 33.0

31 Lubber 8 55.5

32 Andy 8 25.5

58 Rusty 10 35.0

64 Horatio 7 35.0

71 Zorba 10 16.0

74 Horatio 9 35.0

85 Art 3 25.5

95 Bob 3 63.5

Reserves Relation

Sid Bid day

22 101 10/10/98

22 102 10/10/98

22 103 10/8/98

22 104 10/7/98

31 102 11/10/98

31 103 11/6/98

31 104 11/12/98

64 101 9/5/98

64 102 9/8/98

74 103 9/8/98
Boats Relation

Bid bname color

101 Interlake Blue

102 Interlake Red

103 Clipper Green

104 Marine Red

1) Draw an ER Diagram of above DB with attribute and specify the keys.

Keys in the ER Diagram

Primary Keys:

Sailors: Sid
Reserves: Sid, day
Boats: Bid

Foreign Keys:

Reserves: Sid (references Sailors), Bid (references Boats)


2) Create Relations or Tables of above DB with Primary Key and Foreign Key Constraints.

CREATE TABLE Sailors (

Sid INT PRIMARY KEY,

sname VARCHAR(50) NOT NULL,

rating INT,

Age FLOAT

);

CREATE TABLE boats (

Bid INTEGER PRIMARY KEY,

bname VARCHAR(50) NOT NULL,

color VARCHAR(20)

);

CREATE TABLE reserves (

Sid INT,

Bid INT,

day DATE

);
3) Insert the Values into Relations or Tables

INSERT INTO Sailors (Sid, sname, rating, Age) VALUES

(22, 'Dustin', 7, 45.0),

(29, 'Brutus', 1, 33.0),

(31, 'Lubber', 8, 55.5),

(32, 'Andy', 8, 25.5),

(58, 'Rusty', 10, 35.0),

(64, 'Horatio', 7, 35.0),

(71, 'Zorba', 10, 16.0),

(74, 'Horatio', 9, 35.0),

(85, 'Art', 3, 25.5),

(95, 'Bob', 3, 63.5);

INSERT INTO boats (Bid, bname, color) VALUES

(101, 'Interlake', 'Blue'),

(102, 'Interlake', 'Red'),

(103, 'Clipper', 'Green'),

(104, 'Marine', 'Red');

INSERT INTO reservess (Sid, Bid, day) VALUES

(22, 101, '1998-10-10'),

(22, 102, '1998-10-10'),

(22, 103, '1998-10-08'),

(22, 104, '1998-10-07'),

(31, 102, '1998-11-10'),

(31, 103, '1998-11-06'),

(31, 104, '1998-11-12'),

(64, 101, '1998-09-05'),

(64, 102, '1998-09-08'),

(74, 103, '1998-09-08');


4) Display the Structure of DB( DESC)

DESC Sailors;

DESC Boats;

DESC reserves;
5) Display the contents of Sailors Relation, Reserves Relation and Boat Relation.(SELECT)

SELECT * FROM Sailors;

SELECT * FROM reserves;

SELECT * FROM Boats;


6) CREATE a sailors 1 table by copying the content and structure from students table.

CREATE TABLE Sailors1 AS

SELECT * FROM Sailors;

7) Find the names and ages of all sailors without Duplication.(DISTINCT)

SELECT DISTINCT sname, Age

FROM Sailors;
8) Find all sailors with rating above 7.

SELECT * FROM Sailors

WHERE rating > 7;

G) Find the names of sailors who have reserved boat number 103

SELECT s.sname

FROM Sailo s

JOIN reservess r ON s.Sid = r.Sid

WHERE r.Bid = 103;


10) Find the sids of sailors who have reserved a red boat.

SELECT DISTINCT r.Sid

FROM reserves r

JOIN Boats b ON r.Bid = b.Bid

WHERE b.color = 'Red';

11) Find the names of sailors who have reserved a red boat.

SELECT DISTINCT b.color

FROM Sailors s

JOIN reserves r ON s.Sid = r.Sid

JOIN Boats b ON r.Bid = b.Bid

WHERE s.sname = 'Lubber';


12) Find the colors of boats reserved by Lubber

SELECT DISTINCT b.color

FROM Sailors s

JOIN reserves r ON s.Sid = r.Sid

JOIN Boats b ON r.Bid = b.Bid

WHERE s.sname = 'Lubber';

13) Find the names of sailors who have reserved at least one boat.(NESTED QUERY USING
IN)

SELECT sname

FROM Sailors

WHERE Sid IN (

SELECT DISTINCT Sid

FROM reserves

);
14) Find the ages of sailors whose name begins and ends with B and has at least three
characters.

SELECT Age

FROM Sailors

WHERE sname LIKE 'B%B' AND LENGTH(sname) >= 3;

15) Insert the Value of Sid=G5 in sailors Relations and write the output.

INSERT INTO Sailo (Sid, sname, rating, Age) VALUES

(95, 'Bob', 3, 63.5);

SELECT * FROM Sailo WHERE Sid = 95;

16) Insert the Value of Sid=100 and bid = 105 and day 15/10/G6 in Reserves Relations and
write the output.

INSERT INTO reserves (Sid, Bid, day)

VALUES (100, 105, '1996-10-15');

SELECT * FROM reserves WHERE Sid = 100 AND Bid = 105;


UNION, INTERSECT AND EXCEPT Operations

17) Find the names of sailors who have reserved a red or a green boat

SELECT DISTINCT s.sname

FROM Sailors s

JOIN reserves r ON s.Sid = r.Sid

JOIN Boats b ON r.Bid = b.Bid

WHERE b.color IN ('Red', 'Green');

18) Find the sids of all sailors who have reserved red boats but not green boats.

SELECT DISTINCT r1.Sid

FROM reserves r1

JOIN Boats b1 ON r1.Bid = b1.Bid

WHERE b1.color = 'Red'

AND r1.Sid NOT IN (

SELECT r2.Sid

FROM reserves r2
JOIN Boats b2 ON r2.Bid = b2.Bid

WHERE b2.color = 'Green'

);
1G) Find all sids of sailors who have rating of 10 or Reserved boat 104.

SELECT DISTINCT s.Sid

FROM Sailors s

WHERE s.rating = 10

OR s.Sid IN (

SELECT r.Sid

FROM reserves r

WHERE r.Bid = 104

);
DROP, TRUNCATE, DELETE, UPDATE, RENAME COMMANDS

20) ALTER the sailor table to add a new column ‘Credits’.

ALTER TABLE Sailors

ADD Credits INT;

21) Delete the Row where sname is bob.

DELETE FROM Sailors

WHERE sname = 'Bob';


22) Perform update command.

UPDATE Sailors

SET rating = 8

WHERE Sid = 85;

23) Perform Rename sname to names, DROP Commands.

ALTER TABLE Sailors

CHANGE COLUMN sname names VARCHAR(50);

ALTER TABLE Sailors

DROP COLUMN Credits;

DROP TABLE Sailors;


24) Create Separate Tables and Implement operations No Action, CASCADE, NOT NULL,
UNIQUE and CHECK Constraint.

-- Create Sailors Table

CREATE TABLE sailors (

Sid INT NOT NULL AUTO_INCREMENT,

sname VARCHAR(50) NOT NULL,

rating INT NOT NULL,

Age FLOAT,

PRIMARY KEY (Sid),

UNIQUE (sname),

CHECK (rating BETWEEN 1 AND 10)

);

-- Create Boats Table

CREATE TABLE boats (

Bid INT NOT NULL AUTO_INCREMENT,

bname VARCHAR(50),

color VARCHAR(20) NOT NULL,

PRIMARY KEY (Bid),

UNIQUE (bname)

);

-- Create reserves Table

CREATE TABLE reserves (

Sid INT,

Bid INT,

day DATE NOT NULL,

PRIMARY KEY (Sid, Bid, day),

FOREIGN KEY (Sid) REFERENCES Sailors(Sid) ON DELETE CASCADE ON UPDATE NO ACTION,

FOREIGN KEY (Bid) REFERENCES Boats(Bid) ON DELETE NO ACTION ON UPDATE CASCADE

);
25) Display the Relations or tables of above operations.

SHOW TABLES;

DESCRIBE Sailors;

DESCRIBE Boats;

DESCRIBE Reserves;
SHOW CREATE TABLE Sailors;

SHOW CREATE TABLE Boats;

SHOW CREATE TABLE Reserves;

You might also like