DBMS Lab Manual - 21CSL55
DBMS Lab Manual - 21CSL55
LAB EXPERIMENTS
PART A: SQL PROGRAMMING
A. Consider the following schema for a Library Database:
BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES(Book_id, Programme_id, No-of_Copies)
BOOK_LENDING(Book_id, Programme_id, Card_No, Date_Out, Due_Date)
LIBRARY_PROGRAMME(Programme_id, Programme_Name,Address)
Write SQL queries to
1. Retrieve details of all books in the library id, title, name of publisher, authors,
number of copies in each Programme, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from
Jan 2017 to Jun 2017
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its working
with a simple query.
5. Create a view of all books and its number of copies that are currently available in
the Library.
Program Objectives:
This course will enable students to
Foundation knowledge in database concepts, technology and practice to groom students
into well-informed database application developers.
Strong practice in SQL programming through a variety of database problems.
Develop database applications using front-end tools and back-end DBMS.
Table Creation
Table Descriptions
DESC BOOK
;
DESC BOOK_AUTHORS;
DESC PUBLISHER;
DESC BOOK_COPIES
DESC CARD;
DESC LIBRARY_PROGRAMME
AUTHOR_NAME BOOK_ID
NAVATHE 1
NAVATHE 2
ULLMAN 3
CHARLES 4
GALVIN 5
CARDNO
101
102
103
104
105
3 CD PEARSON ULLMAN 7 14
5 OS PEARSON GALVIN 3 10
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 to Jun 2017.
SELECT CARD_NO FROM BOOK_LENDING WHERE DATE_OUT
BETWEEN '2017-01-01'AND '2017-07-01' GROUP BY CARD_NO
HAVING COUNT(*)>3;
4. Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.
5. Create a view of all books and its number of copies that are currently available in the
Library.
CREATE VIEW VW_BOOKS AS SELECT B.BOOK_ID, B.TITLE, C.NO_OF_COPIES
FROM BOOK B, BOOK_COPIES C, LIBRARY_PROGRAMME L WHERE
B.BOOK_ID=C.BOOK_ID AND C.PROGRAMME_ID=L.PROGRAMME_ID;
Program Outcomes:
The students are able to
Create, Update and query on the database.
Demonstrate the working of different concepts of DBMS
Implement, analyze and evaluate the project developed for an application.
Program Objectives:
This course will enable students to
Foundation knowledge in database concepts, technology and practice to groom students
into well-informed database application developers.
Strong practice in SQL programming through a variety of database problems.
Develop database applications using front-end tools and back-end DBMS.
Solution:
Entity-Relationship Diagram
Table Creation
DESC SALESMAN;
DESC CUSTOMER;
DESC ORDERS;
Insertion of Value
2. Find the name and numbers of all salesmen who had more than one customer.
SELECT SALESMAN_ID,NAME
FROM SALESMAN A
WHERE 1 <(SELECT COUNT(*) FROM CUSTOMER
WHERE SALESMAN_ID=A.SALESMAN_ID)
OR
SELECT S.SALESMAN_ID,NAME, FROM CUSTOMER
C,SALESMAN S WHERE
S.SALESMAN_ID=C.SALESMAN_ID GROUP BY
C.SALESMAN_ID HAVING COUNT(*)>1
3. their cities
(Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a
day.
CREATE VIEW VW_ELITSALESMAN AS
SELECT B.ORD_DATE,A.SALESMAN_ID,A.NAME FROM SALESMAN A, ORDERS B
WHERE A.SALESMAN_ID = B.SALESMAN_ID AND B.PURCHASE_AMT=(SELECT
MAX(PURCHASE_AMT) FROM ORDERS C
WHERE C.ORD_DATE = B.ORD_DATE);
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders
must also be deleted.
Use ON DELETE CASCADE at the end of foreign key definitions while creating child table
orders and then execute the following:
Program Outcomes:
The students are able to
Create, Update and query on the database.
Program Objectives:
This course will enable students to
Foundation knowledge in database concepts, technology and practice to groom students
into well-informed database application developers.
Strong practice in SQL programming through a variety of database problems.
Develop database applications using front-end tools and back-end DBMS.
Solution:
Entity-Relationship Diagram
Table Creation
CREATE TABLE ACTOR (
ACT_ID INT (5) PRIMARY KEY,
ACT_NAME VARCHAR (20),
ACT_GENDER CHAR (1));
Table Descriptions
DESC ACTOR;
DESC DIRECTOR;
DESC MOVIES;
DESC MOVIES_CAST;
9563400156);
INSERT INTO DIRECTOR VALUES(102,'ALAN TAYLOR',9971960035);
25);
75);
INSERT INTO DIRECTOR VALUES (105,'HITCHCOCK',7766138911);
INSERT INTO DIRECTOR VALUES (106,'STEVEN SPIELBERG',9966138934);
MOV_ID REV_STARS
501 4
502 2
503 5
504 4
505 3
506 2
507 2
508 4
OR
SELECT MOV_TITLE FROM MOVIES M, DIRECTOR D WHERE M.DIR_ID=D.DIR_ID
AND DIR_NAME='HITCHCOCK';
2. Find the movie names where one or more actors acted in two or more movies.
SELECT MOV_TITLE FROM MOVIES M,MOVIES_CAST MV
WHERE M.MOV_ID=MV.MOV_ID AND ACT_ID IN(SELECT ACT_ID FROM
MOVIES_CAST GROUP BY ACT_ID HAVING COUNT(ACT_ID)>1) GROUP BY
MOV_TITLE HAVING COUNT(*)>1;
3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN
operation).
SELECT ACT_NAME, MOV_TITLE, MOV_YEAR FROM ACTOR A JOIN
MOVIE_CAST C ON A.ACT_ID=C.ACT_ID INNER JOIN MOVIES M
ON C.MOV_ID=M.MOV_ID WHERE M.MOV_YEAR NOT BETWEEN 2000 AND 2015;
5. 5
UPDATE RATING SET REV_STARS=5 WHERE MOV_ID IN(SELECT MOV_ID FROM
MOVIES WHERE DIR_ID IN(SELECT DIR_ID FROM DIRECTOR
WHERE DIR_NAME='STEVEN SPIELBERG'));
OR
UPDATE RATING R, MOVIES M, DIRECTOR D SET REV_STARS=5 WHERE
R.MOV_ID=M.MOV_ID AND M.DIR_ID=D.DIR_ID AND DIR_NAME='STEVEN
SPIELBERG';
Program Outcomes:
The students are able to
Create, Update and query on the database.
Demonstrate the working of different concepts of DBMS
Implement, analyze and evaluate the project developed for an application.
Give these details only for 8th semester A, B, and C section students.
Program Objectives:
This course will enable students to
Foundation knowledge in database concepts, technology and practice to groom students
into well-informed database application developers.
Strong practice in SQL programming through a variety of database problems.
Develop database applications using front-end tools and back-end DBMS.
Solution:
Entity - Relationship Diagram
Table Creation
CREATE TABLE STUDENT (
USN VARCHAR (10) PRIMARY KEY,
SNAME VARCHAR (25),
ADDRESS VARCHAR (25),
PHONE BIGINT (10),
GENDER CHAR (1));
Table Descriptions
DESC STUDENT;
DESC SEMSEC;
DESC CLASS;
DESC IAMARKS;
S LECT * FROM
SELECT S.*, SS.SEM, SS.SEC FROM STUDENT S, SEMSEC SS, CLASS C WHERE
2. Compute the total number of male and female students in each semester and ineach section.
SELECT SS.SEM, SS.SEC, S.GENDER, COUNT (S.GENDER) AS COUNT FROM
STUDENT S, SEMSEC SS, CLASS C
WHERE S.USN = C.USN AND SS.SSID = C.SSID
GROUP BY SS.SEM, SS.SEC, S.GENDER ORDER BY SEM;
3. subjects.
CREATE VIEW VW_STUDENT_TEST AS SELECT TEST1,SUBCODE FROM
IAMARKS WHERE USN= 4AD13CS091';
Note: Before execution above SQL statement, IAMARKS table contents are:
UPDATE IAMARKS
SET FINALIA=GREATEST(TEST1+TEST2,TEST2+TEST3,TEST1+TEST3)/2;
Give these details only for 8th semester A, B, and C section students.
SELECT S.USN,S.SNAME,S.ADDRESS,S.PHONE,S.GENDER,
(CASE
WHEN IA.FINALIA BETWEEN 17 AND 20 THEN 'OUTSTANDING'
WHEN IA. FINALIA BETWEEN 12 AND 16 THEN 'AVERAGE'
ELSE 'WEAK'
END) AS CAT
FROM STUDENT S, SEMSEC SS, IAMARKS IA, SUBJECT SUB WHERE S.USN = IA.USN
AND SS.SSID = IA.SSID AND SUB.SUBCODE = IA.SUBCODE AND SUB.SEM = 8;
Program Outcomes:
The students are able to
Create, Update and query on the database.
Demonstrate the working of different concepts of DBMS
Implement, analyze and evaluate the project developed for an application.
project.
2. Show the resulting salaries if every employee
percent raise.
3.
the maximum salary, the minimum salary, and the average salary in this department
4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator).
5. For each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than Rs. 6,00,000.
Program Objectives:
This course will enable students to
Foundation knowledge in database concepts, technology and practice to groom students
into well-informed database application developers.
Strong practice in SQL programming through a variety of database problems.
Develop database applications using front-end tools and back-end DBMS.
Solution:
Entity-Relationship Diagram
Table Creation
NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter department
table to add foreign constraint MGRSSN using sql command
ALTER TABLE DEPARTMENT ADD FOREIGN KEY(MGRSSN) REFERENCES
Table Descriptions
DESC EMPLOYEE;
DESC DEPARTMENT;
DESC PROJECT;
DESC PROJECT;
DESC WORKS_ON;
Note: update entries of employee table to fill missing fields SUPERSSN and DNO
UPDATE EMPLOYEE SET SUPE
UPDATE EMPLOYEE
1. Make a list of all project numbers for projects that involve an employee whose last name
UNION
(SELECT DISTINCT P1.PNO FROM PROJECT P1, WORKS_ON W, EMPLOYEE E1 WHERE
2.
percent raise.
SELECT E.FNAME, E.LNAME, 1.1*E.SALARY AS INCR_SAL FROM EMPLOYEE E,
WORKS_ON W, PROJECT P WHERE E.SSN=W.SSN AND W.PNO=P.PNO AND
3.
maximum salary, the minimum salary, and the average salary in this department
SELECT SUM (E.SALARY), MAX (E.SALARY), MIN (E.SALARY), AVG (E.SALARY)
FROM EMPLOYEE E, DEPARTMENT D WHERE E.DNO=D.DNO AND
5. For each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than Rs. 6, 00,000.
SELECT D.DNO, COUNT (*)
FROM DEPARTMENT D, EMPLOYEE E
WHERE D.DNO=E.DNO
AND E.SALARY>600000
AND D.DNO IN (SELECT E1.DNO
FROM EMPLOYEE E1
GROUP BY E1.DNO
HAVING COUNT (*)>5)
GROUP BY D.DNO;
Program Outcomes:
The students are able to
Create, Update and query on the database.
Demonstrate the working of different concepts of DBMS
Implement, analyze and evaluate the project developed for an application.
TRUNCATE DELETE
44. What is the use of the ADD OR DROP option in the ALTER TABLE command?
It is used to add/drop columns or add/drop constraints specified on the table
45. What is the use of DESC in SQL?
DESC has two purposes. It is used to describe a schema as well as to retrieve rows from table in
descending order.
The query SELECT * FROM EMP ORDER BY ENAME DESC will display the output sorted
on ENAME in descending order
46. What is the use of ON DELETE CASCADE?
Whenever rows in the master (referenced) table are deleted ,the respective rows of the child
(referencing) table with a matching foreign key column will get deleted as well. This is called a
cascade delete
INSERT command
Insert the values into the table as specified.
1) Insert into Employee -11-
2) Insert into Employee values(1001, -05- 45000, 10, 42);
3) Insert into Employee values(1002, ' -01- 000, 20, 28);
4) Insert into Employee values(100 -05- 15000, 40, 34);
5) Insert into Employee values(10 -10- 60000, 50, 45);
6) Insert into Employee values(1005, -7-