DBMS Lab 1
DBMS Lab 1
Give these details only for 8th semester A, B, and C section student
5. Consider the schema for Company Database:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo, DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON( SSN, PNo, Hours)
Write SQL queries to
1. Make a list of all project numbers for projects that involve an employee whose last
name is either as a worker or as a manager of the department that controls the
project.
2. Show the resulting salaries if is given a
10 percent raise.
3. Find the sum of the salaries of all employees of the department, as well as
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.
Experiment distribution
1. For laboratories having only one part: Students are allowed to pick one experiment from
the lot with equal opportunity.
2. For laboratories having PART A and PART B: Students are allowed to pick one
experiment from PART A and one experiment from PART B, with equal opportunity.
3. Change of experiment is allowed only once and marks allotted for procedure to be made
zero of the changed part only.
4. Marks Distribution (Coursed to change in accordance with university regulations)
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.
BKEC Page 01
DBMS Laboratory with mini Project 21CSL55
Solution:
Entity-Relationship Diagram
BKEC Page 02
DBMS Laboratory with mini Project 21CSL55
Schema Diagram
BKEC Page 03
DBMS Laboratory with mini Project 21CSL55
Table Creation
BKEC Page 04
DBMS Laboratory with mini Project 21CSL55
Table Descriptions
DESC BOOK
;
DESC BOOK_AUTHORS;
DESC PUBLISHER;
DESC BOOK_COPIES
BKEC Page 05
DBMS Laboratory with mini Project 21CSL55
DESC BOOK_LENDING;
DESC CARD;
DESC LIBRARY_PROGRAMME
BKEC Page 06
DBMS Laboratory with mini Project 21CSL55
Insertion of Values to Tables
BKEC Page 07
DBMS Laboratory with mini Project 21CSL55
SELECT * FROM BOOK;
AUTHOR_NAME BOOK_ID
NAVATHE 1
NAVATHE 2
ULLMAN 3
CHARLES 4
GALVIN 5
BKEC Page 08
DBMS Laboratory with mini Project 21CSL55
CARDNO
101
102
103
104
105
BKEC Page 09
DBMS Laboratory with mini Project 21CSL55
Queries:
1. Retrieve details of all books in the library id, title, name of publisher, authors, number
of copies in each branch, etc.
SELECT B.BOOK_ID, B.TITLE, B.PUBLISHER_NAME, A.AUTHOR_NAME,
C.NO_OF_COPIES, L.PROGRAMME_ID FROM BOOK B, BOOK_AUTHORS A, BOOK_COPIES
C, LIBRARY_PROGRAMME L WHERE B.BOOK_ID=A.BOOK_ID AND
B.BOOK_ID=C.BOOK_ID AND L.PROGRAMME_ID=C.PROGRAMME_ID;
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;
BKEC Page 10
DBMS Laboratory with mini Project 21CSL55
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.
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;
BKEC Page 11
DBMS Laboratory with mini Project 21CSL55
SELECT * FROM VW_BOOKS;
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.
BKEC Page 12
DBMS Laboratory with mini Project 21CSL55
B. Consider the following schema for Order Database:
SALESMAN (Salesman_id, Name, City, Commission)
CUSTOMER (Customer_id, Cust_Name, City, Grade, Salesman_id)
ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id)
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
BKEC Page 13
DBMS Laboratory with mini Project 21CSL55
Schema Diagram
Table Creation
BKEC Page 14
DBMS Laboratory with mini Project 21CSL55
Table Descriptions
DESC SALESMAN;
DESC CUSTOMER;
DESC ORDERS;
Insertion of Value
BKEC Page 15
DBMS Laboratory with mini Project 21CSL55
BKEC Page 16
DBMS Laboratory with mini Project 21CSL55
Queries
1. average.
SELECT GRADE, COUNT (CUSTOMER_ID) FROM
CUSTOMER GROUP BY GRADE
HAVING GRADE > (SELECT AVG (GRADE) FROM
CUSTOMER WHERE CITY='BANGALORE');
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.)
BKEC Page 17
DBMS Laboratory with mini Project 21CSL55
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:
BKEC Page 18
DBMS Laboratory with mini Project 21CSL55
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
BKEC Page 19
DBMS Laboratory with mini Project 21CSL55
Schema Diagram
Table Creation
CREATE TABLE ACTOR (
ACT_ID INT (5) PRIMARY KEY,
ACT_NAME VARCHAR (20),
ACT_GENDER CHAR (1));
BKEC Page 20
DBMS Laboratory with mini Project 21CSL55
CREATE TABLE RATING (
MOV_ID INT (5) PRIMARY KEY,
REV_STARS VARCHAR (25),
FOREIGN KEY (MOV_ID) REFERENCES MOVIES (MOV_ID));
Table Descriptions
DESC ACTOR;
DESC DIRECTOR;
DESC MOVIES;
DESC MOVIES_CAST;
BKEC Page 21
DBMS Laboratory with mini Project 21CSL55
DESC RATING;
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);
BKEC Page 22
DBMS Laboratory with mini Project 21CSL55
SELECT * FROM ACTOR;
BKEC Page 23
DBMS Laboratory with mini Project 21CSL55
MOV_ID REV_STARS
501 4
502 2
503 5
504 4
505 3
506 2
507 2
508 4
BKEC Page 24
DBMS Laboratory with mini Project 21CSL55
Queries:
1. List the titles of all movies directed by
SELECT MOV_TITLE FROM MOVIES WHERE DIR_ID IN (SELECT DIR_ID FROM
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;
BKEC Page 25
DBMS Laboratory with mini Project 21CSL55
4. Find the title of movies and number of stars for each movie that has at least one rating and
find the highest number of stars that movie received. Sort the result by movie title.
SELECT MOV_TITLE,MAX(REV_STARS) FROM MOVIES M ,RATING R WHERE
M.MOV_ID=R.MOV_ID GROUP BY MOV_TITLE HAVING MAX(REV_STARS)>0 ORDER
BY MOV_TITLE;
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.
BKEC Page 26
DBMS Laboratory with mini Project 21CSL55
D. Consider the schema for College Database:
STUDENT (USN, SName, Address, Phone, Gender)
SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID)
SUBJECT (Subcode, Title, Sem, Credits)
IAMARKS (USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
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
BKEC Page 27
DBMS Laboratory with mini Project 21CSL55
Schema Diagram
Table Creation
CREATE TABLE STUDENT (
USN VARCHAR (10) PRIMARY KEY,
SNAME VARCHAR (25),
ADDRESS VARCHAR (25),
PHONE BIGINT (10),
GENDER CHAR (1));
BKEC Page 28
DBMS Laboratory with mini Project 21CSL55
CREATE TABLE IAMARKS (
USN VARCHAR (10),
SUBCODE VARCHAR (8),
SSID VARCHAR (5),
TEST1 INT (2),
TEST2 INT (2),
TEST3 INT (2),
FINALIA INT (2),
PRIMARY KEY (USN, SUBCODE, SSID),
FOREIGN KEY (USN) REFERENCES STUDENT (USN),
FOREIGN KEY (SUBCODE) REFERENCES SUBJECT (SUBCODE), FOREIGN
KEY (SSID) REFERENCES SEMSEC (SSID));
Table Descriptions
DESC STUDENT;
DESC SEMSEC;
DESC CLASS;
BKEC Page 29
DBMS Laboratory with mini Project 21CSL55
DESC SUBJECT;
DESC IAMARKS;
BKEC Page 30
DBMS Laboratory with mini Project 21CSL55
INSERT INTO SEMSEC VALUES ('CSE8A', 8,'A');
INSERT INTO SEMSEC VALUES ('CSE8B', 8,'B');
INSERT INTO SEMSEC VALUES ('CSE8C', 8,'C');
INSERT INTO SEMSEC VALUES ('CSE7A', 7,'A');
INSERT INTO SEMSEC VALUES ('CSE7B', 7,'B');
INSERT INTO SEMSEC VALUES ('CSE7C', 7,'C');
INSERT INTO SEMSEC VALUES ('CSE6A', 6,'A');
INSERT INTO SEMSEC VALUES ('CSE6B', 6,'B');
INSERT INTO SEMSEC VALUES ('CSE6C', 6,'C');
INSERT INTO SEMSEC VALUES ('CSE5A', 5,'A');
INSERT INTO SEMSEC VALUES ('CSE5B', 5,'B');
INSERT INTO SEMSEC VALUES ('CSE5C', 5,'C');
INSERT INTO SEMSEC VALUES ('CSE4A', 4,'A');
INSERT INTO SEMSEC VALUES ('CSE4B', 4,'B');
INSERT INTO SEMSEC VALUES ('CSE4C', 4,'C');
INSERT INTO SEMSEC VALUES ('CSE3A', 3,'A');
INSERT INTO SEMSEC VALUES ('CSE3B', 3,'B');
INSERT INTO SEMSEC VALUES ('CSE3C', 3,'C');
INSERT INTO SEMSEC VALUES ('CSE2A', 2,'A');
INSERT INTO SEMSEC VALUES ('CSE2B', 2,'B');
INSERT INTO SEMSEC VALUES ('CSE2C', 2,'C');
INSERT INTO SEMSEC VALUES ('CSE1A', 1,'A');
INSERT INTO SEMSEC VALUES ('CSE1B', 1,'B');
INSERT INTO SEMSEC VALUES ('CSE1C', 1,'C');
BKEC Page 31
DBMS Laboratory with mini Project 21CSL55
INSERT INTO SUBJECT VALUES ('10CS72','ECS', 7, 4);
INSERT INTO SUBJECT VALUES ('10CS73','PTW', 7, 4);
INSERT INTO SUBJECT VALUES ('10CS74','DWDM', 7, 4); I
INSERT INTO SUBJECT VALUES ('10CS75','JAVA', 7, 4);
INSERT INTO SUBJECT VALUES ('10CS76','SAN', 7, 4);
INSERT INTO SUBJECT VALUES ('15CS51', 'ME', 5, 4);
INSERT INTO SUBJECT VALUES ('15CS52','CN', 5, 4);
INSERT INTO SUBJECT VALUES ('15CS53','DBMS', 5, 4);
INSERT INTO SUBJECT VALUES ('15CS54','ATC', 5, 4);
INSERT INTO SUBJECT VALUES ('15CS55','JAVA', 5, 3);
INSERT INTO SUBJECT VALUES ('15CS56','AI', 5, 3);
INSERT INTO SUBJECT VALUES ('15CS41','M4', 4, 4);
INSERT INTO SUBJECT VALUES ('15CS42','SE', 4, 4);
INSERT INTO SUBJECT VALUES ('15CS43','DAA', 4, 4);
INSERT INTO SUBJECT VALUES ('15CS44','MPMC', 4, 4);
INSERT INTO SUBJECT VALUES ('15CS45','OOC', 4, 3);
INSERT INTO SUBJECT VALUES ('15CS46','DC', 4, 3);
INSERT INTO SUBJECT VALUES ('15CS31','M3', 3, 4);
INSERT INTO SUBJECT VALUES ('15CS32','ADE', 3, 4);
INSERT INTO SUBJECT VALUES ('15CS33','DSA', 3, 4);
INSERT INTO SUBJECT VALUES ('15CS34','CO', 3, 4);
INSERT INTO SUBJECT VALUES ('15CS35','USP', 3, 3);
INSERT INTO SUBJECT VALUES ('15CS36','DMS', 3, 3);
SELECT * FROM
BKEC Page 32
DBMS Laboratory with mini Project 21CSL55
SELECT * FROM SEMSEC;
BKEC Page 33
DBMS Laboratory with mini Project 21CSL55
BKEC Page 34
DBMS Laboratory with mini Project 21CSL55
Queries:
1. section.
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 in each 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';
BKEC Page 35
DBMS Laboratory with mini Project 21CSL55
4. Calculate the FinalIA (average of best two test marks) and update the corresponding
table for all students.
UPDATE IAMARKS
SET FINALIA=GREATEST(TEST1+TEST2,TEST2+TEST3,TEST1+TEST3)/2;
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;
BKEC Page 36
DBMS Laboratory with mini Project 21CSL55
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.
BKEC Page 37
DBMS Laboratory with mini Project 21CSL55
E. Consider the schema for Company Database:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN,
DNo) DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)
Write SQL queries to
1. Make a list of all project numbers for projects that involve an employee whose last
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
BKEC Page 38
DBMS Laboratory with mini Project 21CSL55
Schema 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
EMPLOYEE(SSN);
BKEC Page 39
DBMS Laboratory with mini Project 21CSL55
CREATE TABLE DLOCATION (
DLOC VARCHAR (20),
DNO VARCHAR (20),
PRIMARY KEY (DNO, DLOC),
FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNO));
Table Descriptions
DESC EMPLOYEE;
DESC DEPARTMENT;
BKEC Page 40
DBMS Laboratory with mini Project 21CSL55
DESC DLOCATION;
DESC PROJECT;
DESC PROJECT;
DESC WORKS_ON;
BKEC Page 41
DBMS Laboratory with mini Project 21CSL55
INSERT INTO EMPLOYEE VALUES ('ATMEIT01','NAGESH','HR','BANGALORE','M',
500000,NULL,NULL);
Note: update entries of employee table to fill missing fields SUPERSSN and DNO
UPDATE EMPLOYEE SET SUPE
UPDATE EMPLOYEE
BKEC Page 42
DBMS Laboratory with mini Project 21CSL55
INSERT INTO PROJECT VALUES (106,'OPENSTACK','BANGALORE','4');
INSERT INTO PROJECT VALUES (107,'SMART CITY','BANGALORE','2');
BKEC Page 43
DBMS Laboratory with mini Project 21CSL55
SELECT * FROM DLOCATION ;
BKEC Page 44
DBMS Laboratory with mini Project 21CSL55
Queries:
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
BKEC Page 45
DBMS Laboratory with mini Project 21CSL55
4. Retrieve the name of each employee who works on all the projects Controlled by
department number 5 (use NOT EXISTS operator).
SELECT E.FNAME,E.LNAME FROM EMPLOYEE E WHERE NOT EXISTS
(SELECT PNO FROM PROJECT P WHERE DNO=5 AND PNO NOT IN
(SELECT PNO FROM WORKS_ON W WHERE E.SSN=SSN));
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.
BKEC Page 46
DBMS Laboratory with mini Project 21CSL55
BKEC Page 47
DBMS Laboratory with mini Project 21CSL55
BKEC Page 48