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

DBMS_Lab_Program-2

The document outlines a Database Management Systems lab manual, detailing objectives to create relational database tables, insert sample data, and write SQL queries. It includes schemas for EMPLOYEE, DEPARTMENT, PROJECT, WORKS_ON, DEPENDENT, SAILORS, BOATS, RESERVES, STUDENT, CLASS, ENROLLED, and FACULTY, along with SQL commands for table creation, data insertion, and various queries. Additionally, it provides instructions for students and evaluation criteria for their work.

Uploaded by

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

DBMS_Lab_Program-2

The document outlines a Database Management Systems lab manual, detailing objectives to create relational database tables, insert sample data, and write SQL queries. It includes schemas for EMPLOYEE, DEPARTMENT, PROJECT, WORKS_ON, DEPENDENT, SAILORS, BOATS, RESERVES, STUDENT, CLASS, ENROLLED, and FACULTY, along with SQL commands for table creation, data insertion, and various queries. Additionally, it provides instructions for students and evaluation criteria for their work.

Uploaded by

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

DATABASE MANAGEMENT SYSTEMS LAB

MANUAL
Program – 1

Objective
- To create relational database tables with appropriate constraints.
- To insert sample data into the tables.
- To write SQL queries to retrieve and manipulate data effectively.

Database Schema
EMPLOYEE (Ename: string, Ssn: integer, Bdate: date, Sex: string, Address: string, Salary:
real, Mgrssn: integer, Dno: integer)
DEPARTMENT (Dname: string, Dnumber: integer, Mgrssn: integer, Mgr_start_date: date)
PROJECT (Pname: string, Pnumber: integer, Plocation: string, Dnum: integer)
WORKS_ON (Essn: integer, Pno: integer, Hours: real)
DEPENDENT (Essn: integer, Dependent_name: string, Sex: string)

Table Creation with Constraints


CREATE TABLE DEPARTMENT (
Dname VARCHAR(50) NOT NULL,
Dnumber INT PRIMARY KEY,
Mgrssn INT,
Mgr_start_date DATE
);

CREATE TABLE EMPLOYEE (


Ename VARCHAR(50) NOT NULL,
Ssn INT PRIMARY KEY,
Bdate DATE,
Sex VARCHAR(10),
Address VARCHAR(100),
Salary REAL,
Mgrssn INT,
Dno INT,
FOREIGN KEY (Mgrssn) REFERENCES EMPLOYEE(Ssn),
FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber)
);
ALTER TABLE DEPARTMENT
ADD CONSTRAINT FK_Mgrssn FOREIGN KEY (Mgrssn) REFERENCES EMPLOYEE(Ssn);

CREATE TABLE PROJECT (

Pname VARCHAR(50) NOT NULL,

Pnumber INT PRIMARY KEY,

Plocation VARCHAR(100),

Dnum INT,

FOREIGN KEY (Dnum) REFERENCES DEPARTMENT(Dnumber)

);

CREATE TABLE WORKS_ON (

Essn INT,

Pno INT,

Hours REAL,

PRIMARY KEY (Essn, Pno),

FOREIGN KEY (Essn) REFERENCES EMPLOYEE(Ssn),

FOREIGN KEY (Pno) REFERENCES PROJECT(Pnumber)

);

CREATE TABLE DEPENDENT (

Essn INT,

Dependent_name VARCHAR(50),

Sex VARCHAR(10),

PRIMARY KEY (Essn, Dependent_name),

FOREIGN KEY (Essn) REFERENCES EMPLOYEE(Ssn)


);

Sample Data Insertion


INSERT INTO DEPARTMENT VALUES ('ISE', 10, NULL, '2020-01-01');

INSERT INTO DEPARTMENT VALUES ('CSE', 20, NULL, '2019-03-01');

INSERT INTO DEPARTMENT VALUES ('ECE', 30, NULL, '2021-06-15');

INSERT INTO EMPLOYEE VALUES ('John Doe', 101, '1990-01-01', 'M', '123 Main St', 50000,
NULL, 10);

INSERT INTO EMPLOYEE VALUES ('Jane Smith', 102, '1992-05-15', 'F', '456 Elm St', 60000,
NULL, 20);

INSERT INTO EMPLOYEE VALUES ('Alice Johnson', 103, '1985-03-10', 'F', '789 Oak St',
70000, NULL, 10);

INSERT INTO EMPLOYEE VALUES ('Bob Brown', 104, '1993-07-25', 'M', '321 Pine St',
55000, NULL, 30);

INSERT INTO EMPLOYEE VALUES ('Charlie Green', 105, '1988-11-30', 'M', '654 Cedar St',
45000, NULL, 20);

UPDATE DEPARTMENT SET Mgrssn = 103 WHERE Dname = 'ISE';

UPDATE DEPARTMENT SET Mgrssn = 101 WHERE Dname = 'CSE';

UPDATE DEPARTMENT SET Mgrssn = 104 WHERE Dname = 'ECE';

SQL Queries
1. Retrieve the name and address of all employees who work for the "ISE" department:

SELECT Ename, Address


FROM EMPLOYEE
WHERE Dno = (SELECT Dnumber FROM DEPARTMENT WHERE Dname = 'ISE');

2. For each employee, retrieve the employee's name and the name of his or her immediate
supervisor:

SELECT E1.Ename AS Employee, E2.Ename AS Supervisor


FROM EMPLOYEE E1
LEFT JOIN EMPLOYEE E2 ON E1.Mgrssn = E2.Ssn;

3. Find the sum of all salaries of all employees:

SELECT SUM(Salary) AS Total_Salary


FROM EMPLOYEE;

4. For each department, retrieve the department name and the average salary of employees
in that department:

SELECT Dname, AVG(Salary) AS Avg_Salary


FROM EMPLOYEE E
JOIN DEPARTMENT D ON E.Dno = D.Dnumber
GROUP BY Dname;

Instructions for Students


1. Write the provided SQL commands in your chosen SQL environment (e.g., MySQL, SQL
Server, or PostgreSQL).
2. Execute each query and note the output for documentation.
3. Ensure you maintain proper formatting for improved readability.

Evaluation Criteria
- Correct table creation syntax and constraints.
- Successful insertion of sample data.
- Correct and optimized SQL queries for the given tasks.
- Clear documentation of output.
Program – 2
Consider the following relation schema:

• SAILORS (Sid: integer, Sname: string, Rating: integer, Age: real)


• BOATS (Bid: integer, Bname: string, Color: string)
• RESERVES (sid: integer, Bid: integer, Day:date)

Create above tables by specifying primary key, foreign key and other suitable constraints.

Insert atleast 5 tuples to each created table.

Design a database to satisfy the above requirements and answer following queries

i. Find all sailors with a rating above 7


ii. Find the names of sailors who have reserved boat number 103
iii. Find the names of sailors who have reserved a red boat
iv. Find the names of sailors who have reserved a red or a green boat 3.

Objective
- To create relational database tables with appropriate constraints.
- To insert sample data into the tables.
- To write SQL queries to retrieve and manipulate data effectively.

Database Schema
SAILORS (Sid: integer, Sname: string, Rating: integer, Age: real)
BOATS (Bid: integer, Bname: string, Color: string)
RESERVES (Sid: integer, Bid: integer, Day: date)

Table Creation with Constraints

CREATE TABLE SAILORS (


Sid INT PRIMARY KEY,
Sname VARCHAR(50) NOT NULL,
Rating INT,
Age REAL
);

CREATE TABLE BOATS (


Bid INT PRIMARY KEY,
Bname VARCHAR(50) NOT NULL,
Color VARCHAR(20)
);
CREATE TABLE RESERVES (
Sid INT,
Bid INT,
Day DATE,
PRIMARY KEY (Sid, Bid, Day),
FOREIGN KEY (Sid) REFERENCES SAILORS(Sid),
FOREIGN KEY (Bid) REFERENCES BOATS(Bid)
);

Sample Data Insertion

INSERT INTO SAILORS VALUES (101, 'John Doe', 8, 25.5);


INSERT INTO SAILORS VALUES (102, 'Jane Smith', 6, 30.0);
INSERT INTO SAILORS VALUES (103, 'Alice Johnson', 9, 28.7);
INSERT INTO SAILORS VALUES (104, 'Bob Brown', 5, 22.3);
INSERT INTO SAILORS VALUES (105, 'Charlie Green', 7, 29.1);

INSERT INTO BOATS VALUES (201, 'Sea Breeze', 'Red');


INSERT INTO BOATS VALUES (202, 'Ocean Wave', 'Blue');
INSERT INTO BOATS VALUES (203, 'Sunset Sail', 'Green');
INSERT INTO BOATS VALUES (103, 'Wind Whisper', 'Red');
INSERT INTO BOATS VALUES (204, 'Golden Horizon', 'Yellow');

INSERT INTO RESERVES VALUES (101, 201, '2024-03-01');


INSERT INTO RESERVES VALUES (102, 202, '2024-03-02');
INSERT INTO RESERVES VALUES (103, 103, '2024-03-03');
INSERT INTO RESERVES VALUES (104, 203, '2024-03-04');
INSERT INTO RESERVES VALUES (105, 201, '2024-03-05');

SQL Queries
1. Find all sailors with a rating above 7:

SELECT Sname
FROM SAILORS
WHERE Rating > 7;

2. Find the names of sailors who have reserved boat number 103:
SELECT Sname
FROM SAILORS
WHERE Sid IN (
SELECT Sid
FROM RESERVES
WHERE Bid = 103
);

3. Find the names of sailors who have reserved a red boat:

SELECT Sname
FROM SAILORS S
JOIN RESERVES R ON S.Sid = R.Sid
JOIN BOATS B ON R.Bid = B.Bid
WHERE B.Color = 'Red';

4. Find the names of sailors who have reserved a red or a green boat:

SELECT 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');

Instructions for Students


1. Write the provided SQL commands in your chosen SQL environment (e.g., MySQL, SQL
Server, or PostgreSQL).
2. Execute each query and note the output for documentation.
3. Ensure you maintain proper formatting for improved readability.

Evaluation Criteria
• Correct table creation syntax and constraints.
• Successful insertion of sample data.
• Correct and optimized SQL queries for the given tasks.
• Clear documentation of output.
• Answering Viva Questions
Viva Questions
1. What is a foreign key, and how does it ensure referential integrity?
2. Explain the purpose of the IN clause in SQL.
3. How does a JOIN clause help in combining multiple tables?
4. What is the difference between INNER JOIN and OUTER JOIN?
5. What is the significance of using WHERE clauses for filtering results?
Program – 3
Consider the following relation schema:

STUDENT (Snum: integer, Sname: string, Major: string, Level: string, Age: integer)

CLASS (Cname: string, Meets at: string, Room: string, Fid: integer)

ENROLLED (Snum: integer, Cname: string)

FACULTY (Fid: integer, Fname: string, Deptid: integer)

The meaning of these relations is straightforward; for example, enrolled has one record per
student-class pair such that the student is enrolled in the class. Level is a two-character code
with 4 different values (example: Junior: JR etc)

Write the following queries in SQL. No duplicates should be printed in any of the answers.

i. Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof.
Harshith
ii. Find the names of all classes that either meet in room R128 or have five or more
Students enrolled.
iii. Find the names of all students who are enrolled in two classes that meet at the
same time.
iv. Find the names of faculty members who teach in every room in which some class
is taught.

Objective
1. To create relational database tables with appropriate constraints.
2. To insert sample data into the tables.
3. To write SQL queries to retrieve and manipulate data effectively.

Table Creation with Constraints


CREATE TABLE STUDENT (

Snum INT PRIMARY KEY,

Sname VARCHAR(50) NOT NULL,

Major VARCHAR(50),

Level VARCHAR(2),

Age INT

);
CREATE TABLE FACULTY (

Fid INT PRIMARY KEY,

Fname VARCHAR(50) NOT NULL,

Deptid INT

);

CREATE TABLE CLASS (

Cname VARCHAR(50) PRIMARY KEY,

Meets_at VARCHAR(20),

Room VARCHAR(20),

Fid INT,

FOREIGN KEY (Fid) REFERENCES FACULTY(Fid)

);

CREATE TABLE ENROLLED (

Snum INT,

Cname VARCHAR(50),

PRIMARY KEY (Snum, Cname),

FOREIGN KEY (Snum) REFERENCES STUDENT(Snum),

FOREIGN KEY (Cname) REFERENCES CLASS(Cname)

);

Sample Data Insertion


-- Faculty

INSERT INTO FACULTY VALUES (1, 'Harshith', 101);

INSERT INTO FACULTY VALUES (2, 'Meena', 102);


INSERT INTO FACULTY VALUES (3, 'Ravi', 103);

-- Student

INSERT INTO STUDENT VALUES (101, 'Alice', 'CS', 'JR', 20);

INSERT INTO STUDENT VALUES (102, 'Bob', 'EC', 'SR', 21);

INSERT INTO STUDENT VALUES (103, 'Charlie', 'ME', 'JR', 19);

INSERT INTO STUDENT VALUES (104, 'David', 'CS', 'FR', 18);

INSERT INTO STUDENT VALUES (105, 'Eva', 'IS', 'JR', 20);

-- Class

INSERT INTO CLASS VALUES ('DBMS', '10AM', 'R128', 1);

INSERT INTO CLASS VALUES ('CN', '11AM', 'R130', 2);

INSERT INTO CLASS VALUES ('OS', '10AM', 'R129', 3);

INSERT INTO CLASS VALUES ('AI', '11AM', 'R128', 1);

INSERT INTO CLASS VALUES ('ML', '2PM', 'R140', 2);

-- Enrolled

INSERT INTO ENROLLED VALUES (101, 'DBMS');

INSERT INTO ENROLLED VALUES (103, 'DBMS');

INSERT INTO ENROLLED VALUES (105, 'AI');

INSERT INTO ENROLLED VALUES (101, 'AI');

INSERT INTO ENROLLED VALUES (102, 'CN');

INSERT INTO ENROLLED VALUES (103, 'OS');

INSERT INTO ENROLLED VALUES (101, 'OS');

INSERT INTO ENROLLED VALUES (105, 'OS');


SQL Queries
1. Find the names of all Juniors (level = JR) who are enrolled in a class taught by
Prof. Harshith

SELECT DISTINCT S.Sname

FROM STUDENT S

JOIN ENROLLED E ON S.Snum = E.Snum

JOIN CLASS C ON E.Cname = C.Cname

JOIN FACULTY F ON C.Fid = F.Fid

WHERE S.Level = 'JR' AND F.Fname = 'Harshith';

2. Find the names of all classes that either meet in room R128 or have five or
more students enrolled

SELECT DISTINCT Cname

FROM CLASS

WHERE Room = 'R128'

UNION

SELECT Cname

FROM ENROLLED

GROUP BY Cname

HAVING COUNT(Snum) >= 5;


3. Find the names of all students who are enrolled in two classes that meet at the
same time

SELECT DISTINCT S1.Sname

FROM STUDENT S1

JOIN ENROLLED E1 ON S1.Snum = E1.Snum

JOIN CLASS C1 ON E1.Cname = C1.Cname

JOIN ENROLLED E2 ON S1.Snum = E2.Snum AND E1.Cname <> E2.Cname

JOIN CLASS C2 ON E2.Cname = C2.Cname

WHERE C1.Meets_at = C2.Meets_at;

4. Find the names of faculty members who teach in every room in which some
class is taught

SELECT F.Fname

FROM FACULTY F

WHERE NOT EXISTS (

SELECT Room

FROM CLASS

EXCEPT

SELECT DISTINCT C.Room

FROM CLASS C

WHERE C.Fid = F.Fid

);
Program – 4
Consider the relation schema for book dealer database:
AUTHOR (Author-id:int, Name:string, City:string, Country:string)
PUBLISHER (Publisher-id:int, Name:string, City:string, Country:string)
CATALOG (Book-id:int, Title:string, Author-id:int, Publisher-id:int, Category-id:int,
Year:int, Price:int)
CATEGORY (Category-id:int, Description:string)
ORDER-DETAILS (Order-no:int, Book-id:int, Quantity:int)
Create the above tables by properly specifying the primary keys and the foreign keys.
Enter at least five tuples for each relation.
i. Give the details of the authors who have 2 or more books in the catalog and the
price of the books is greater than the average price of the books in the catalog and
the year of publication is after 2000.
ii. Find the author of the book which has maximum sales.
iii. Demonstrate how you increase the price of books published by a specific publisher
by 10%
iv. List any department that has all its adopted books published by a specific publisher

Objective

• To create relational database tables with appropriate constr


• To insert sample data into the tables.
• To write complex SQL queries for data retrieval and manipulation.

Database Schema

• AUTHOR (Author_id: INT, Name: VARCHAR, City: VARCHAR, Country: VARCHAR)


• PUBLISHER (Publisher_id: INT, Name: VARCHAR, City: VARCHAR, Country:
VARCHAR)
• CATALOG (Book_id: INT, Title: VARCHAR, Author_id: INT, Publisher_id: INT,
Category_id: INT, Year: INT, Price: INT)
• CATEGORY (Category_id: INT, Description: VARCHAR)
• ORDER_DETAILS (Order_no: INT, Book_id: INT, Quantity: INT)

Table Creation with Constraints


CREATE TABLE AUTHOR (

Author_id INT PRIMARY KEY,

Name VARCHAR(50),

City VARCHAR(50),

Country VARCHAR(50)
);

CREATE TABLE PUBLISHER (

Publisher_id INT PRIMARY KEY,

Name VARCHAR(50),

City VARCHAR(50),

Country VARCHAR(50)

);

CREATE TABLE CATEGORY (

Category_id INT PRIMARY KEY,

Description VARCHAR(100)

);

CREATE TABLE CATALOG (

Book_id INT PRIMARY KEY,

Title VARCHAR(100),

Author_id INT,

Publisher_id INT,

Category_id INT,

Year INT,

Price INT,

FOREIGN KEY (Author_id) REFERENCES AUTHOR(Author_id),

FOREIGN KEY (Publisher_id) REFERENCES PUBLISHER(Publisher_id),

FOREIGN KEY (Category_id) REFERENCES CATEGORY(Category_id)

);
CREATE TABLE ORDER_DETAILS (

Order_no INT,

Book_id INT,

Quantity INT,

PRIMARY KEY (Order_no, Book_id),

FOREIGN KEY (Book_id) REFERENCES CATALOG(Book_id)

);

Sample Data Insertion


INSERT INTO AUTHOR VALUES (1, 'George Orwell', 'London', 'UK');

INSERT INTO AUTHOR VALUES (2, 'J.K. Rowling', 'Edinburgh', 'UK');

INSERT INTO AUTHOR VALUES (3, 'Mark Twain', 'Missouri', 'USA');

INSERT INTO AUTHOR VALUES (4, 'Jane Austen', 'Bath', 'UK');

INSERT INTO AUTHOR VALUES (5, 'Chetan Bhagat', 'Delhi', 'India');

INSERT INTO PUBLISHER VALUES (1, 'Penguin', 'New York', 'USA');

INSERT INTO PUBLISHER VALUES (2, 'HarperCollins', 'London', 'UK');

INSERT INTO PUBLISHER VALUES (3, 'Scholastic', 'New York', 'USA');

INSERT INTO PUBLISHER VALUES (4, 'Bloomsbury', 'Delhi', 'India');

INSERT INTO PUBLISHER VALUES (5, 'Random House', 'Berlin', 'Germany');

INSERT INTO CATEGORY VALUES (1, 'Fiction');

INSERT INTO CATEGORY VALUES (2, 'Fantasy');

INSERT INTO CATEGORY VALUES (3, 'Classic');

INSERT INTO CATEGORY VALUES (4, 'Romance');

INSERT INTO CATEGORY VALUES (5, 'Drama');


INSERT INTO CATALOG VALUES (101, '1984', 1, 1, 1, 2003, 300);

INSERT INTO CATALOG VALUES (102, 'Harry Potter', 2, 3, 2, 2005, 500);

INSERT INTO CATALOG VALUES (103, 'The Adventures of Tom Sawyer', 3, 2, 3, 1995, 250);

INSERT INTO CATALOG VALUES (104, 'Pride and Prejudice', 4, 2, 4, 2001, 350);

INSERT INTO CATALOG VALUES (105, 'Half Girlfriend', 5, 4, 5, 2014, 200);

INSERT INTO ORDER_DETAILS VALUES (1001, 101, 5);

INSERT INTO ORDER_DETAILS VALUES (1002, 102, 15);

INSERT INTO ORDER_DETAILS VALUES (1003, 103, 3);

INSERT INTO ORDER_DETAILS VALUES (1004, 104, 7);

INSERT INTO ORDER_DETAILS VALUES (1005, 105, 10);

SQL Queries
1. Give the details of authors who have 2 or more books in the catalog, and the price of
the books is greater than the average price of the books in the catalog and the year
of publication is after 2000.

SELECT A.*

FROM AUTHOR A

JOIN CATALOG C ON A.Author_id = C.Author_id

WHERE C.Price > (SELECT AVG(Price) FROM CATALOG)

AND C.Year > 2000

AND A.Author_id IN (

SELECT Author_id

FROM CATALOG

GROUP BY Author_id

HAVING COUNT(*) >= 2

);
2. Find the author of the book which has maximum sales.

SELECT A.Name

FROM AUTHOR A

JOIN CATALOG C ON A.Author_id = C.Author_id

JOIN ORDER_DETAILS O ON C.Book_id = O.Book_id

WHERE O.Quantity = (

SELECT MAX(Quantity) FROM ORDER_DETAILS

);

3. Demonstrate how you increase the price of books published by a specific publisher
by 10%.

UPDATE CATALOG

SET Price = Price * 1.10

WHERE Publisher_id = 2; -- Replace 2 with specific publisher ID

4. List any department (category) that has all its adopted books published by a specific
publisher.

SELECT Category_id

FROM CATALOG

GROUP BY Category_id

HAVING COUNT(DISTINCT Publisher_id) = 1;

Expected Output
• List of authors matching all filtering conditions.
• Name of the author whose book had maximum sales.
• All prices of books are updated for a specific publisher.
• Category IDs where all books were published by the same publisher.

Instructions for Students


1. Create the tables and insert data using the given SQL scripts.
2. Execute each query and record the output.
3. Use proper formatting and SQL practices.

Evaluation Criteria
• Proper creation of tables with all constraints.
• Correct data insertion.
• Accurate results for complex SQL queries.
• Neatly presented output and documentation.

Viva Questions
1. How do you implement aggregation in SQL?
2. What is a GROUP BY clause and when do we use HAVING?
3. How can we perform an update on multiple rows?
4. Explain how foreign keys help maintain integrity across tables.
5. How do subqueries help in filtering or calculating intermediate results?

You might also like