Dbms Lab Manual 18scheme
Dbms Lab Manual 18scheme
BOOK_AUTHORS(Book_id, Author_Name)
CARD(Card_No)
1. Retrieve details of all books in the library – id, title, name of publisher,
authors, number of copies in each branch, 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.
5.Create a view of all books and its number of copies that are currently
available in the Library.
2. Find the name and numbers of all salesmen who had more than one
customer.
3. List all the salesman and indicate those who have and don’t have customers
in their cities (Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the
highest order of a day.
RATING(Mov_id, Rev_Stars)
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.
CLASS(USN, SSID)
1. List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in each semester and
in each section.
4. Calculate the FinalIA (average of best two test marks) and update the
corresponding table for all students.
DLOCATION(DNo, DLoc)
1. Make a list of all project numbers for projects that involve an employee
whose last name is ‘Scott’, either as a worker or as a manager of the
department that controls the project.
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.
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each branch, 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.
Card
Card_no
DATEOUT DATE,
DUEDATE DATE,
AUTHORNAME BOOKID
NAVATHE 1
NAVATHE 2
CARDNO
101
102
103
104
105
1.Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each branch, etc.
SELECT LB.BRANCHNAME, B.BOOKID,TITLE, PNAME,AUTHORNAME, NOOFCOPIES
FROM BOOK B, BOOKAUTHORS BA, BOOKCOPIES BC, LIBRARYBRANCH LB
WHERE B.BOOKID = BA.BOOKID AND
BA.BOOKID = BC.BOOKID AND
BC.BRANCHID = LB.BRANCHID
GROUP BY LB.BRANCHNAME, B.BOOKID, TITLE, PNAME,AUTHORNAME, NOOFCOPIES;
1. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 to Jun 2017
SELECT CARDNO
FROM BOOKLENDING
WHERE DATEOUT BETWEEN ’01-JAN-2017’ AND ’01-JUN-2017’
GROUP BY CARDNO HAVING
COUNT(*)>3;
CARDNO
101
4.Create a view of all books and its number of copies that are currently available in the Library.
CREATE VIEW BOOKSAVAILABLE AS
View Created
4. Create a view that finds the salesman who has the customer with the highest order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must
also be deleted.
Schema Diagram
Table Creation
DESC SALESMAN;
NAME VARCHAR(20)
CITY VARCHAR(20)
COMMISSION NUMBER(3)
DESC CUSTOMER1;
CUSTNAME VARCHAR(20)
CITY VARCHAR(20)
SALESMANID NUMBER(4)
DESC ORDERS;
PURCHASEsAMT NUMBER(10,2)
ORDDATE DATE
CUSTOMERID NUMBER(4)
SALESMANID NUMBER(4)
Queries:
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)
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:
Use ON DELETE SET NULL at the end of foreign key definitions while creating child table
customers and then executes the following:
3. List all actors who acted in a movie before 2000 and also in a movie after 2015
(use JOIN operation).
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.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.
Schema Diagram
DESC ACTOR;
DESC DIRECTOR;
MOVTITLE VARCHAR(25),
MOVYEAR NUMBER(4),
MOVLANG VARCHAR(12),
DESC MOVIES;
DESC RATING;
1 MADHURI DIXIT F
2 AAMIR KHAN M
3 JUHI CHAWLA F
4 SRIDEVI F
INSERT INTO MOVIES VALUES (501,’JAB HARRY MET SEJAL’, 2017, ‘HINDI’, 104); INSERT
INTO MOVIES VALUES (502,’RAJAKUMARA’, 2017, ‘KANNADA’, 103);
INSERT INTO MOVIES VALUES (503,’JOLLY LLB 2’, 2013, ‘HINDI’, 100);
INSERT INTO MOVIES VALUES (504,’TERMINATOR GENESYS, 2015, ‘ENGLISH’, 102);
INSERT INTO MOVIES VALUES (505,’JAWS, 1975, ‘ENGLISH’, 106);
INSERT INTO MOVIES VALUES (506,’BRIDGE OF SPIES’,2015,’ENGLISH’, 106);
INSERT INTO MOVIES VALUES (507,’VERTIGO, 1943, ‘ENGLISH’, 105);
INSERT INTO MOVIES VALUES (508,’SHADOW OF A DOUBT, 1943, ‘ENGLISH’, 105);
1 501 HEROINE
1 502 HEROINE
3 503 COMEDIAN
4 504 GUEST
4 501 HERO
MOVID REVSTARS
501 4
502 2
503 5
504 4
505 3
506 2
507 2
508 4
Queries:
1. List the titles of all movies directed by ‘Hitchcock’.
SELECT MOVTITLE
FROM MOVIES
FROM DIRECTOR
WHERE DIRNAME='HITCHCOCK');
2. Find the movie names where one or more actors acted in two or more movies.
3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN
operation).
SELECT ACTNAME, MOVTITLE, MOVYEAR
OR
SELECT A.ACTNAME, A.ACTNAME, C.MOVTITLE, C.MOVYEAR FROM
MOVTITLE MAX(REVSTARS)
---------------------------------------------------------------------------------
BRIDGE OF SPIES 5
JAB HARRY MET SEJAL 4
JAWS 5
JOLLY LLB2 5
RAJAKUMARA 2
SHADOW OF A DOUBT 4
TERMINATOR GENESYS 4
VERTIGO 2
UPDATE RATING
SET REVSTARS=5
WHERE MOVID IN (SELECT MOVID
FROM MOVIES
WHERE DIRID IN (SELECT DIRID
FROM DIRECTOR
1 ROWS UPDATED
501 4
502 2
503 5
504 4
505 5
506 5
507 2
508 4
1. List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in each semester and in each
section.
4. Calculate the FinalIA (average of best two test marks) and update the corresponding
table for all students.
STUDENT
CREATE TABLE STUDENT
(USN VARCHAR(10) PRIMARY KEY,
SNAME VARCHAR(25),
ADDRESS VARCHAR(25),
PHONE VARCHAR(10),
GENDER CHAR(1));
Table created.
SEMSEC
CREATE TABLE SEMSEC
SSID VARCHAR(5) PRIMARY KEY,
SEM NUMBER(2),
SEC CHAR(1));
Table created.
CLASS
CREATE TABLE CLASS
(USN REFERENCES STUDENT(USN),
SSID REFERENCES SEMSEC(SSID));
Table created.
SUBJECT
CREATE TABLE SUBJECT
(SUBCODE VARCHAR(8) PRIMARY KEY,
TEST3 NUMBER(2),
FINALIA NUMBER(3));
Table created.
Values for tables:
STUDENT:
select * from student;
USN SNAME ADDRESS PHONE G
---------- ------------------------- ------------- ------------ - ---
1cg15cs001 Abhi tumkur 9875698410 M
1cg15cs002 amulya gubbi 8896557412 F
1cg16me063 chethan nittur 7894759522 M
1cg14ec055 raghavi sspuram 9485675521 F
1cg15ee065 sanjay bangalore 9538444404 M
SEMSEC:
select * from semsec;
SSID SEM S
----- --------- -
5A 5 A
3B 3 B
CLASS:
select * from class;
USN SSID
---------- -----
1cg15cs001 5A
1cg15cs002 5A
1cg16me063 3B
1cg14ec055 7A
1cg15ee065 3B
1cg15ee065 4c
1cg15cs002 4c
SUBJECT:
select * from subject;
SUBCODE TITLE SEM CREDITS
-------- -------------------- --------- ---------
15cs53 dbms 5 4
15cs33 ds 3 4
15cs34 co 3 4
15csl58 dba 52
10cs71 oomd 7 4
IAMARKS:
select * from iamarks;
USN SUBCODE SSID TEST1 TEST2 TEST3 FINALIA
---------- -------- ----- --------- --------- --------- ---------
DLOCATION (DNo,DLoc)
1. Make a list of all project numbers for projects that involve an employee whose last name
is ‘KUMAR’, either as a worker or as a manager of the department that controls the
project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10
percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ 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).
Schema Diagram
NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter
Department table to add foreign constraint MGRSSN using sql command.
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘GATECE01’,’MOHAN’,’KUMAR’,’BANGALORE’,’M’, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘GATCSE01’,’JAGAN’,’RAO’,’BANGALORE’,’M’, 500000);
Note: update entries of employee table to fill missing fields SUPERSSN and DNO
UPDATE EMPLOYEE
SETSUPERSSN=NULL, DNO=’3’
WHERE SSN=’GATECE01’;
UPDATE EMPLOYEE
SETSUPERSSN=’GATCSE02’, DNO=’5’
WHERE SSN=’GATCSE01’;
UPDATE EMPLOYEE
SETSUPERSSN=’GATCSE04’, DNO=’5’
WHERE SSN=’GATCSE03’;
UPDATE EMPLOYEE
SETDNO=’5’, SUPERSSN=’GATCSE05’
WHERE SSN=’GATCSE04’;
UPDATE EMPLOYEE
SETDNO=’5’, SUPERSSN=’GATCSE06’
WHERE SSN=’GATCSE05
UPDATE EMPLOYEE
SETDNO=’5’, SUPERSSN=NULL
WHERE SSN=’GATCSE06’;
UPDATE EMPLOYEE
SETDNO=’1’, SUPERSSN=’GATACC02’
WHERE SSN=’GATACC01’;
UPDATE EMPLOYEE
SETDNO=’1’, SUPERSSN=NULL
WHERE SSN=’GATACC02’;
UPDATE EMPLOYEE
SETDNO=’4’, SUPERSSN=NULL
WHERE SSN=’GATISE01’;
UPDATE EMPLOYEE
SETDNO=’2’, SUPERSSN=NULL
WHERE SSN=’GATIT01’;
Queries:
1. Make a list of all project numbers for projects that involve an employee whose last name is
‘KUMAR’, either as a worker or as a manager of the department that controls the project.
PNO
-----------
102
103
104
105
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10
percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ 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).
FNAME LNAME
------------------------------------
JAGAN RAO
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.
DNO COUNT(*)
---------------------------------------
5 3
Viva Questions
1. What is SQL?
Structured Query Language
2. What is database?
A database is a logically coherent collection of data with some inherent meaning, representing some
aspect of real world and which is designed, built and populated with data for a specific purpose.
3. What is DBMS?
It is a collection of programs that enables user to create and maintain a database. In other words it is
general-purpose software that provides the users with the processes of defining, constructing and
manipulating the database for various applications.
5. Advantages of DBMS?
o Redundancy is controlled.
o Data isolation.
o Data integrity.
Physical Data Independence: Modification in physical level should not affect the logical level.
Growth and restructuring of base tables is not reflected in views. Thus the view can insulate users
from the effects of restructuring and growth in the database. Hence accounts for logical data
independence.
be the relation which contains set tuples (t1, t2, t3, ...,tn). Each tuple
Every dependency in F has a single attribute for its right hand side.