DBMS Lab Manual - 21CSL55

Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

DBMS Laboratory with mini Project 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.

Dept. of CSE,RIT,Hassan Page 1


DBMS Laboratory with mini Project 21CSL55
Solution:
Entity-Relationship Diagram

Dept.of CSE,RIT,Hassan Page 2


DBMS Laboratory with mini Project 21CSL55
Schema Diagram

Dept.of CSE,RIT,Hassan Page 3


DBMS Laboratory with mini Project 21CSL55

Table Creation

CREATE TABLE BOOK (


BOOK_ID INT (10) PRIMARY KEY,
TITLE VARCHAR (20),
PUB_YEAR VARCHAR (20),
PUBLISHER_NAME VARCHAR (20),
FOREIGN KEY (PUBLISHER_NAME) REFERENCES PUBLISHER (NAME) ON DELETE
CASCADE);

CREATE TABLE BOOK_AUTHORS (


AUTHOR_NAME VARCHAR (20),
BOOK_ID INT (10),
PRIMARY KEY (BOOK_ID, AUTHOR_NAME),
FOREIGN KEY (BOOK_ID) REFERENCES BOOK (BOOK_ID) ON DELETE CASCADE);

CREATE TABLE PUBLISHER (


NAME VARCHAR (20) PRIMARY KEY,
PHONE BIGINT (20),
ADDRESS VARCHAR (100));

CREATE TABLE BOOK_COPIES (


NO_OF_COPIES INT (5),
BOOK_ID INT (10),
PROGRAMME_ID INT (10),
PRIMARY KEY (BOOK_ID,PROGRAMME_ID),
FOREIGN KEY (BOOK_ID) REFERENCES BOOK (BOOK_ID) ON DELETE CASCADE,
FOREIGN KEY (PROGRAMME_ID) REFERENCES LIBRARY_PROGRAMME
(PROGRAMME_ID) ON DELETE CASCADE);

CREATE TABLE BOOK_LENDING (


DATE_OUT DATE,
DUE_DATE DATE,
BOOK_ID INT (10),
PROGRAMME_ID INT (10),
CARD_NO INT (10),
PRIMARY KEY (BOOK_ID,PROGRAMME_ID, CARD_NO),
FOREIGN KEY (BOOK_ID) REFERENCES BOOK (BOOK_ID) ON DELETE CASCADE,
FOREIGN KEY (PROGRAMME_ID) REFERENCES
LIBRARY_PROGRAMME(PROGRAMME_ID) ON DELETE CASCADE,
FOREIGN KEY (CARD_NO) REFERENCES CARD (CARD_NO) ON DELETE CASCADE);

CREATE TABLE CARD (


CARD_NO INT (10) PRIMARY KEY);

Dept.of CSE,RIT,Hassan Page 4


DBMS Laboratory with mini Project 21CSL55
CREATE TABLE LIBRARY_PROGRAMME (
PROGRAMME_ID INT (10) PRIMARY KEY,
PROGRAMME_NAME VARCHAR (50),
ADDRESS VARCHAR (100));

Table Descriptions

DESC BOOK
;

DESC BOOK_AUTHORS;

DESC PUBLISHER;

DESC BOOK_COPIES

Dept.of CSE,RIT,Hassan Page 5


DBMS Laboratory with mini Project 21CSL55
DESC BOOK_LENDING;

DESC CARD;

DESC LIBRARY_PROGRAMME

Dept.of CSE,RIT,Hassan Page 6


DBMS Laboratory with mini Project 21CSL55
Insertion of Values to Tables

INSERT INTO BOOK VALUES (1,'DBMS','JAN-2017', 'MCGRAW-HILL');


INSERT INTO BOOK VALUES (2,'ADBMS','JUN-2016','MCGRAW-HILL');
INSERT INTO BOOK VALUES (3, 'CD','SEP-2016','PEARSON');
INSERT INTO BOOK VALUES (4,' ALGORITHMS ','SEP-2015',' MIT');
INSERT INTO BOOK VALUES (5,'OS','MAY-2016','PEARSON');

INSERT INTO BOOK_AUTHORS VALUES ('NAVATHE', 1);


INSERT INTO BOOK_AUTHORS VALUES ('NAVATHE', 2);
INSERT INTO BOOK_AUTHORS VALUES ('ULLMAN',3);
INSERT INTO BOOK_AUTHORS VALUES ('CHARLES', 4);
INSERT INTO BOOK_AUTHORS VALUES('GALVIN', 5);

INSERT INTO PUBLISHER VALUES ('MCGRAW-HILL', 9989076587,'BANGALORE');


INSERT INTO PUBLISHER VALUES ('PEARSON', 9889076565,'NEWDELHI');
INSERT INTO PUBLISHER VALUES ('PRENTICE HALL', 7455679345,'HYEDRABAD');
INSERT INTO PUBLISHER VALUES ('WILEY', 8970862340,'CHENNAI');
INSERT INTO PUBLISHER VALUES ('MIT',7756120238,'BANGALORE');

INSERT INTO BOOK_COPIES VALUES (10, 1, 10);


INSERT INTO BOOK_COPIES VALUES (5, 1, 11);
INSERT INTO BOOK_COPIES VALUES (2, 2, 12);
INSERT INTO BOOK_COPIES VALUES (5, 2, 13);
INSERT INTO BOOK_COPIES VALUES (7, 3, 14);
INSERT INTO BOOK_COPIES VALUES (1, 5, 10);
INSERT INTO BOOK_COPIES VALUES (3, 4, 11);

INSERT INTO BOOK_LENDING VALUES ('2017-01-01','2017-06-01', 1, 10, 101);


INSERT INTO BOOK_LENDING VALUES ('2017-01-11 ','2017-03-11', 3, 14, 101);
INSERT INTO BOOK_LENDING VALUES ('2017-02-21','2017-04-21', 2, 13, 101);
INSERT INTO BOOK_LENDING VALUES ('2017-03-15 ','2017-07-15', 4, 11, 101);
INSERT INTO BOOK_LENDING VALUES ('2017-04-12','2017-05-12', 1, 11, 104);

INSERT INTO CARD VALUES (100);


INSERT INTO CARD VALUES (101);
INSERT INTO CARD VALUES (102);
INSERT INTO CARD VALUES (103);
INSERT INTO CARD VALUES (104);

INSERT INTO LIBRARY_PROGRAMME VALUES (10,'VIJAY NAGAR','MYSURU');


INSERT INTO LIBRARY_PROGRAMME VALUES (11,'VIDYANAGAR','HUBLI'); ;
INSERT INTO LIBRARY_PROGRAMME VALUES(12,'KUVEMPUNAGAR','MYSURU');
INSERT INTO LIBRARY_PROGRAMME VALUE(13,'RAJAJINAGAR','BANGALORE');
INSERT INTO LIBRARY_PROGRAMME VALUES (14,'MANIPAL','UDUPI');

Dept.of CSE,RIT,Hassan Page 7


DBMS Laboratory with mini Project 21CSL55
SELECT * FROM BOOK;

BOOK_ID TITLE PUB_YEAR PUBLISHER_NAME


1 DBMS Jan-2017 MCGRAW-HILL
2 ADBMS Jun-2017 MCGRAW-HILL
3 CD Sep-2016 PEARSON
4 ALGORITHMS Sep-2015 MIT
5 OS May-2016 PEARSON

SELECT * FROM BOOK_AUTHORS;

AUTHOR_NAME BOOK_ID
NAVATHE 1
NAVATHE 2
ULLMAN 3
CHARLES 4
GALVIN 5

SELECT * FROM PUBLISHER;

NAME PHONE ADDRESS


MCGRAW-HILL 9989076587 BANGALORE
MIT 7756120238 BANGALORE
PEARSON 9889076565 NEWDELHI
PRENTICE HALL 7455679345 HYEDRABAD
WILEY 8970862340 CHENNAI

SELECT * FROM BOOK_COPIES;

NO_OF_COPIES BOOK_ID PROGRAMME_ID


10 1 10
5 1 11
2 2 12
5 2 13
7 3 14
1 5 10
3 4 11

Dept.of CSE,RIT,Hassan Page 8


DBMS Laboratory with mini Project 21CSL55

SELECT * FROM BOOK_LENDING;

DATEOUT DUEDATE BOOKID PROGRAMME_ID CARDNO


2017-01-01 2017-06-01 1 10
2017-01-11 2017-03-11 3 4 101
2017-02-21 2017-04-21 2 13 101
2017-03-15 2017-07-15 4 11 101
2017-04-12 2017-05-12 1 11 104

SELECT * FROM CARD;

CARDNO
101
102
103
104
105

SELECT * FROM LIBRARY_PROGRAMME;

PROGRAMME_ID PROGRAMME_NAME ADDRESS


10 VIJAY NAGAR MYSURU
11 VIDYANAGAR HUBLI
12 KUVEMPUNAGAR MYSURU
13 RAJAJINAGAR BANGALORE
14 MANIPAL UDUPI

Dept.of CSE,RIT,Hassan Page 9


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;

BOOK_ TITLE PUBLISHER_ AUTHOR_ NO_ PROGRAMME


ID NAME NAME OF_COPIES _ID
1 DBMS MCGRAW-HILL NAVATHE 10 10

1 DBMS MCGRAW-HILL NAVATHE 5 11

2 ADBMS MCGRAW-HILL NAVATHE 2 12

2 ADBMS MCGRAW-HILL NAVATHE 5 13

3 CD PEARSON ULLMAN 7 14

4 ALGORITHMS MIT CHARLES 1 11

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;

Dept.of CSE,RIT,Hassan 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.

DELETE FROM BOOK WHERE BOOK_ID=3;

4. Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.

CREATE VIEW VW_PUBLICATION AS SELECT PUB_YEAR FROM BOOK;

SELECT * FROM VW_PUBLICATION

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;

Dept.of CSE,RIT,Hassan 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.

Dept.of CSE,RIT,Hassan 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)

Write SQL queries to


1. average.
2. Find the name and numbers of all salesmen who had more than one customer.
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.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.

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

Dept.of CSE,RIT,Hassan Page 13


DBMS Laboratory with mini Project 21CSL55
Schema Diagram

Table Creation

CREATE TABLE SALESMAN (


SALESMAN_ID INT (4) PRIMARY KEY,
NAME VARCHAR (20),
CITY VARCHAR (20),
COMMISSION VARCHAR (20));

CREATE TABLE CUSTOMER (


CUSTOMER_ID INT (5) PRIMARY KEY,
CUST_NAME VARCHAR (20),
CITY VARCHAR (20), GRADE INT (4),
SALESMAN_ID INT (6),
FOREIGN KEY (SALESMAN_ID) REFERENCES SALESMAN (SALESMAN_ID) ON DELETE
SET NULL);

CREATE TABLE ORDERS (


ORD_NO INT (5) PRIMARY KEY,
PURCHASE_AMT DECIMAL (10, 2),
ORD_DATE DATE,
CUSTOMER_ID INT (4),
SALESMAN_ID INT (4),
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER (CUSTOMER_ID) ON DELETE
CASCADE,
FOREIGN KEY (SALESMAN_ID) REFERENCES SALESMAN (SALESMAN_ID) ON DELETE
CASCADE);

Dept.of CSE,RIT,Hassan Page 14


DBMS Laboratory with mini Project 21CSL55
Table Descriptions

DESC SALESMAN;

DESC CUSTOMER;

DESC ORDERS;

Insertion of Value

INSERT INTO SALESMAN VALUES(101,'RICHARD','LOS ANGELES','18%');


INSERT INTO SALESMAN VALUES(103,'GEORGE','NEWYORK','32%');
INSERT INTO SALESMAN VALUES(110,'CHARLES','BANGALORE','54%');
INSERT INTO SALESMAN VALUES(122,'ROWLING','PHILADELPHIA','46%');
INSERT INTO SALESMAN VALUES(126,'KURT','CHICAGO','52%');
INSERT INTO SALESMAN VALUES(132,'EDWIN','PHOENIX','41%');

INSERT INTO CUSTOMER VALUES(501,'SMITH','LOS ANGELES',10,103);


INSERT INTO CUSTOMER VALUES(510,'BROWN','ATLANTA',14,122);
INSERT INTO CUSTOMER VALUES(522,'LEWIS','BANGALORE',10,132);
INSERT INTO CUSTOMER VALUES(534,'PHILIPS','BOSTON',17,103);
INSERT INTO CUSTOMER VALUES(543,'EDWARD','BANGALORE',14,110);

Dept.of CSE,RIT,Hassan Page 15


DBMS Laboratory with mini Project 21CSL55
INSERT INTO CUSTOMER VALUES(550,'PARKER','ATLANTA',19,126);

Dept.of CSE,RIT,Hassan Page 16


DBMS Laboratory with mini Project 21CSL55

INSERT INTO ORDERS VALUES(1,1000, '2017-05-04',501,103);


INSERT INTO ORDERS VALUES(2,4000,'2017-01- ,522,132);
INSERT INTO ORDERS VALUES(3,2500, '2017-02-24',550,126);
INSERT INTO ORDERS VALUES(5,6000,'2017-04-13',522,103);
INSERT INTO ORDERS VALUES(6,7000, '2017-03-09',550,126);
INSERT INTO ORDERS VALUES (7,3400,'2017-01-20',501,122);

SELECT * FROM SALESMAN;

SELECT * FROM CUSTOMER;

SELECT * FROM ORDERS;

Dept.of CSE,RIT,Hassan Page 17


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

SELECT GRADE,COUNT(DISTINCT CUSTOMER_ID)


FROM CUSTOMER GROUP BY GRADE
HA NG GRADE >( E ECT VG(GRADE) FR CUSTOM
WHE E CITY=

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.)

SELECT S.SALESMAN_ID,NAME,CUST_NAME,COMMISSION FROM SALESMAN


S,CUSTOMER C
WHERE S.CITY = C.CITY
UNION
SELECT SALESMAN_ID, NAME, 'NO MATCH',COMMISSION FROM SALESMAN
WHERE NOT CITY = ANY (SELECT CITY
FROM CUSTOMER) ORDER BY 2 DESC;

Dept.of CSE,RIT,Hassan Page 18


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

SELECT * FROM VW_ELITSALESMAN

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:

DELETE FROM SALESMAN WHERE SALESMAN_ID=101;

Dept.of CSE,RIT,Hassan Page 19


DBMS Laboratory with mini Project 21CSL55

Program Outcomes:
The students are able to
Create, Update and query on the database.

Implement, analyze and evaluate the project developed for an application.

Dept.of CSE,RIT,Hassan Page 20


DBMS Laboratory with mini Project 21CSL55

C. Consider the schema for Movie Database:


ACTOR (Act_id, Act_Name, Act_Gender)
DIRECTOR (Dir_id, Dir_Name, Dir_Phone)
MOVIES (Mov_id, Mov_Title, Mov_Year, Mov_Lang, Dir_id)
MOVIE_CAST (Act_id, Mov_id, Role)
RATING (Mov_id, Rev_Stars) Write SQL queries to

1. List the titles of all movies directed by


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).
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
movietitle.
5. 5.

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

Dept.of CSE,RIT,Hassan Page 21


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

CREATE TABLE DIRECTOR (


DIR_ID INT (5) PRIMARY KEY,
DIR_NAME VARCHAR (20),
DIR_PHONE BIGINT);

CREATE TABLE MOVIES


(MOV_ID INT (4) PRIMARY KEY,
MOV_TITLE VARCHAR (50),
MOV_YEAR INT (4),
MOV_LANG VARCHAR (20),
DIR_ID INT (5),
FOREIGN KEY (DIR_ID) REFERENCES DIRECTOR(DIR_ID));

CREATE TABLE MOVIES_CAST (


ACT_ID INT (5),
MOV_ID INT (5),
ROLE VARCHAR (20),
PRIMARY KEY (ACT_ID, MOV_ID),
FOREIGN KEY (ACT_ID) REFERENCES ACTOR (ACT_ID),
FOREIGN KEY (MOV_ID) REFERENCES MOVIES (MOV_ID));

Dept.of CSE,RIT,Hassan Page 22


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;

Dept.of CSE,RIT,Hassan Page 23


DBMS Laboratory with mini Project 21CSL55
DESC RATING;

Insertion of Values to Tables


INSERT INTO ACTOR VALUES (
INSERT INTO ACTOR VALUES (
INSERT INTO ACTOR VALUES (
INSERT INTO ACTOR VALUES (

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

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 IN

INSERT INTO MOVIES_CAST VALUES (1, 501,'HEROINE');


INSERT INTO MOVIES_CAST VALUES (1, 502,'HEROINE');
INSERT INTO MOVIES_CAST VALUES (3, 503,'COMEDIAN');
INSERT INTO MOVIES_CAST VALUES (4, 504,'GUEST');
INSERT INTO MOVIES_CAST VALUES (4, 501,'HERO');

INSERT INTO RATING VALUES (501, 4);


INSERT INTO RATING VALUES (502, 2);
INSERT INTO RATING VALUES (503, 5);
INSERT INTO RATING VALUES (504, 4);
INSERT INTO RATING VALUES (505, 3);
INSERT INTO RATING VALUES (506, 2);

Dept.of CSE,RIT,Hassan Page 24


DBMS Laboratory with mini Project 21CSL55
SELECT * FROM ACTOR;

ACT_ID ACT_NAME ACT


1 MADHURI DIXIT F
2 AAMIR KHAN M
3 JUHI CHAWLA F
4 SRIDEVI F

SELECT * FROM DIRECTOR;

DIR_ID DIR_NAME DIR_PHONE


100 SUBHASH KAPOOR 56340015
102 ALAN TAYLOR 719600310
103 SANTHOSH ANANDDRAM 99346111
104 IMTIAZ ALI 85399209
105 HITCHCOCK 7766138911
106 STEVEN SPIELBERG 9966138934

SELECT * FROM MOVIES;

MOV_ID MOV_TITLE MOV_YEAR MOV_LANG DIR_ID


501 JAB HARRY MET SEJAL 2017 HINDI 104
502 RAJAKUMARA 2017 KANNADA 103
503 JOLLY LLB 2 2013 HINDI 100
504 TERMINATOR GENESYS 2015 ENGLISH 102
505 JAWS 1975 ENGLISH 106
506 BRIDGE OF SPIES 2015 ENGLISH 106
507 VERTIGO 1958 ENGLISH 105
508 SHADOW OF A DOUBT 1943 ENGLISH 105

Dept.of CSE,RIT,Hassan Page 25


DBMS Laboratory with mini Project 21CSL55

SELECT * FROM MOVIE_CAST;

MOV_ID MOV_TITLE MOV_YEAR MOV_LANG DIR_ID


501 JAB HARRY MET SEJAL 2017 HINDI 104
502 RAJAKUMARA 2017 KANNADA 103
503 JOLLY LLB 2 2013 HINDI 100
504 TERMINATOR GENESYS 2015 ENGLISH 102
505 JAWS 1975 ENGLISH 106
506 BRIDGE OF SPIES 2015 ENGLISH 106
507 VERTIGO 1958 ENGLISH 105
508 SHADOW OF A DOUBT 1943 ENGLISH 105

SELECT * FROM RATING;

MOV_ID REV_STARS
501 4
502 2
503 5
504 4
505 3
506 2
507 2
508 4

Dept.of CSE,RIT,Hassan Page 26


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;

Dept.of CSE,RIT,Hassan Page 27


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.

Dept.of CSE,RIT,Hassan Page 28


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)

Write SQL queries to


1. List all the student section.
2. Compute the total number of male and female students in each semester and in each
section.
3. subjects.
4. Calculate the FinalIA (average of best two test marks) and update the corresponding
table for all students.
5. Categorize students based on the following criterion:
If FinalIA = 17 to 20 then CAT =

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

Dept.of CSE,RIT,Hassan Page 29


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

CREATE TABLE SEMSEC (


SSID VARCHAR (5) PRIMARY KEY,
SEM INT (5),
SEC CHAR (1));

CREATE TABLE CLASS (


USN VARCHAR (10),
SSID VARCHAR (5),
PRIMARY KEY (USN, SSID),
FOREIGN KEY (USN) REFERENCES STUDENT (USN),
FOREIGN KEY (SSID) REFERENCES SEMSEC (SSID));

CREATE TABLE SUBJECT(


SUBCODE VARCHAR(10)
PRIMARY KEY,
TITLE VARCHAR(20),
SEM INT,
CREDITS INT);

Dept.of CSE,RIT,Hassan Page 30


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;

Dept.of CSE,RIT,Hassan Page 31


DBMS Laboratory with mini Project 21CSL55
DESC SUBJECT;

DESC IAMARKS;

Insertion of values to tables

INSERT INTO STUDENT VALUES ('4AD13CS020','AKSHAY','BELAGAVI', 8877881122,'M');


INSERT INTO STUDENT VALUES ('4AD13CS062','SANDHYA','BENGALURU',
7722829912,'F');
INSERT INTO STUDENT VALUES ('4AD13CS091','TEESHA','BENGALURU', 7712312312,'F');
INSERT INTO STUDENT VALUES ('4AD13CS066','SUPRIYA','MANGALURU',
8877881122,'F');
INSERT INTO STUDENT VALUES ('4AD14CS010','ABHAY','BENGALURU', 9900211201,'M');
INSERT INTO STUDENT VALUES ('4AD14CS032','BHASKAR','BENGALURU',
9923211099,'M');
INSERT INTO STUDENT VALUES ('4AD14CS025','ASMI','BENGALURU', 7894737377,'F');
INSERT INTO STUDENT VALUES ('4AD15CS011','AJAY','TUMKUR', 9845091341,'M');
INSERT INTO STUDENT VALUES ('4AD15CS029','CHITRA','DAVANGERE', 7696772121,'F');
INSERT INTO STUDENT VALUES ('4AD15CS045','JEEVA','BELLARY', 9944850121,'M');
INSERT INTO STUDENT VALUES ('4AD15CS091','SANTOSH','MANGALURU',
8812332201,'M')
INSERT INTO STUDENT VALUES ('4AD16CS045','ISMAIL','KABURGI', 9900232201,'M');
INSERT INTO STUDENT VALUES ('4AD16CS088','SAMEERA','SHIMOGA', 9905542212,'F');
INSERT INTO STUDENT VALUES ('4AD16CS122','VINAYAKA','CHIKAMAGALUR',
8800880011,'M');

Dept.of CSE,RIT,Hassan Page 32


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

INSERT INTO CLASS VALUES ('4AD13CS020','CSE8A');


INSERT INTO CLASS VALUES ('4AD13CS062','CSE8A');
INSERT INTO CLASS VALUES ('4AD13CS066','CSE8B');
INSERT INTO CLASS VALUES ('4AD13CS091','CSE8C');
INSERT INTO CLASS VALUES ('4AD14CS010','CSE7A');
INSERT INTO CLASS VALUES ('4AD14CS025','CSE7A');
INSERT INTO CLASS VALUES ('4AD14CS032','CSE7A');
INSERT INTO CLASS VALUES ('4AD15CS011','CSE4A');
INSERT INTO CLASS VALUES ('4AD15CS029','CSE4A');
INSERT INTO CLASS VALUES ('4AD15CS045','CSE4B');
INSERT INTO CLASS VALUES ('4AD15CS091','CSE4C');
INSERT INTO CLASS VALUES ('4AD16CS045','CSE3A');
INSERT INTO CLASS VALUES ('4AD16CS088','CSE3B');
INSERT INTO CLASS VALUES ('4AD16CS122','CSE3C');

INSERT INTO SUBJECT VALUES ('10CS81','ACA', 8, 4);


INSERT INTO SUBJECT VALUES ('10CS82','SSM', 8, 4);
INSERT INTO SUBJECT VALUES ('10CS83','NM', 8, 4);
INSERT INTO SUBJECT VALUES ('10CS84','CC', 8, 4);
INSERT INTO SUBJECT VALUES ('10CS85','PW', 8, 4);

Dept.of CSE,RIT,Hassan Page 33


DBMS Laboratory with mini Project 21CSL55
INSERT INTO SUBJECT VALUES ('10CS71','OOAD', 7, 4);

Dept.of CSE,RIT,Hassan Page 34


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

INSERT INTO IAMARKS VALUES ('4AD13CS091','10CS81','CSE8C', 15, 16, 18,0);


INSERT INTO IAMARKS VALUES ('4AD13CS091','10CS82','CSE8C', 12, 19, 14,0);
INSERT INTO IAMARKS VALUES ('4AD13CS091','10CS83','CSE8C', 19, 15, 20,0);
INSERT INTO IAMARKS VALUES ('4AD13CS091','10CS84','CSE8C', 20, 16, 19,0);
INSERT INTO IAMARKS VALUES ('4AD13CS091','10CS85','CSE8C', 15, 15, 12,0);

SELECT * FROM STUDENT;

S LECT * FROM

Dept.of CSE,RIT,Hassan Page 35


DBMS Laboratory with mini Project 21CSL55
SELECT * FROM SEMSEC;

SELECT * FROM CLASS;

Dept.of CSE,RIT,Hassan Page 36


DBMS Laboratory with mini Project 21CSL55

SELECT * FROM SUBJECT;

SELECT * FROM IAMARKS;

Dept.of CSE,RIT,Hassan Page 37


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

SELECT * FROM VW_STUDENT_TEST

Dept.of CSE,RIT,Hassan Page 38


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:

SELECT * FROM IAMARKS;

UPDATE IAMARKS
SET FINALIA=GREATEST(TEST1+TEST2,TEST2+TEST3,TEST1+TEST3)/2;

After executing above SQL statement, IAMARKS table contents are:

5. Categorize students based on the following criterion:


If FinalIA = 17 to 20 then CAT =

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;

Dept.of CSE,RIT,Hassan Page 39


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.

Dept.of CSE,RIT,Hassan Page 40


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

Dept.of CSE,RIT,Hassan Page 41


DBMS Laboratory with mini Project 21CSL55
Schema Diagram

Table Creation

CREATE TABLE DEPARTMENT (


DNO VARCHAR (20) PRIMARY KEY,
DNAME VARCHAR (20),
MGRSTARTDATE DATE,
MGRSSN VARCHAR (20));

CREATE TABLE EMPLOYEE (


SSN VARCHAR (20) PRIMARY KEY,
FNAME VARCHAR (20),
LNAME VARCHAR (20),
ADDRESS VARCHAR (100),
SEX CHAR (1),
SALARY INT (10),
SUPERSSN VARCHAR (20),
DNO VARCHAR (20),
FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE (SSN),
FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNO));

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

Dept.of CSE,RIT,Hassan Page 42


DBMS Laboratory with mini Project 21CSL55
EMPLOYEE(SSN);

Dept.of CSE,RIT,Hassan Page 43


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

CREATE TABLE PROJECT (


PNO INT (10) PRIMARY KEY,
PNAME VARCHAR (20),
PLOCATION VARCHAR (20),
DNO VARCHAR (20),
FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNO));

CREATE TABLE WORKS_ON (


HOURS INT (4),
SSN VARCHAR (20),
PNO INT (10),
PRIMARY KEY (SSN, PNO),
FOREIGN KEY (SSN) REFERENCES EMPLOYEE (SSN),
FOREIGN KEY (PNO) REFERENCES PROJECT (PNO));

Table Descriptions
DESC EMPLOYEE;

DESC DEPARTMENT;

Dept.of CSE,RIT,Hassan Page 44


DBMS Laboratory with mini Project 21CSL55
DESC DLOCATION;

DESC PROJECT;

DESC PROJECT;

DESC WORKS_ON;

Insertion of values to tables


INSERT INTO EMPLOYEE VALUES ('ATMEECE01','JOHN','SCOTT','BANGALORE','M',
450000,NULL,NULL);
INSERT INTO EMPLOYEE VALUES ('ATMECSE01','JAMES','SMITH','BANGALORE','M',
500000,NULL,NULL);
INSERT INTO EMPLOYEE VALUES ('ATMECSE02','HEARN','BAKER','BANGALORE','M',
700000,NULL,NULL);
INSERT INTO EMPLOYEE VALUES ('ATMECSE03','EDWARD','SCOTT','MYSORE','M',
500000,NULL,NULL);
INSERT INTO EMPLOYEE VALUES ('ATMECSE04','PAVAN','HEGDE','MANGALORE','M',
650000,NULL,NULL);
INSERT INTO EMPLOYEE VALUES ('ATMECSE05','GIRISH','MALYA','MYSORE','M',
450000,NULL,NULL);
INSERT INTO EMPLOYEE VALUES ('ATMECSE06','NEHA','SN','BANGALORE','F',
800000,NULL,NULL);
INSERT INTO EMPLOYEE VALUES ('ATMEACC01','AHANA','K','MANGALORE','F',
350000,NULL,NULL);
INSERT INTO EMPLOYEE VALUES
('ATMEACC02','SANTHOSH','KUMAR','MANGALORE','M', 300000,NULL,NULL);
INSERT INTO EMPLOYEE VALUES ('ATMEISE01','VEENA','M','MYSORE','F',

Dept.of CSE,RIT,Hassan Page 45


DBMS Laboratory with mini Project 21CSL55
600000,NULL,NULL);

Dept.of CSE,RIT,Hassan Page 46


DBMS Laboratory with mini Project 21CSL55
INSERT INTO EMPLOYEE VALUES ('ATMEIT01','NAGESH','HR','BANGALORE','M',
500000,NULL,NULL);

INSERT INTO DEPARTMENT VALUES ('1','ACCOUNTS','2001-01-01','ATMEACC02');


INSERT INTO DEPARTMENT VALUES ('2','IT','2016-08-01','ATMEIT01');
INSERT INTO DEPARTMENT VALUES ('3','ECE','2008-6-01','ATMEECE01');
INSERT INTO DEPARTMENT VALUES ('4','ISE','2015-06-01','ATMEISE01');
INSERT INTO DEPARTMENT VALUES ('5','CSE','2002-06-01','ATMECSE05');

Note: update entries of employee table to fill missing fields SUPERSSN and DNO
UPDATE EMPLOYEE SET SUPE

UPDATE EMPLOYEE

INSERT INTO DLOCATION VALUES ('BANGALORE', '1');


INSERT INTO DLOCATION VALUES ('BANGALORE', '2');
INSERT INTO DLOCATION VALUES ('BANGALORE', '3');
INSERT INTO DLOCATION VALUES ('MANGALORE', '4');
INSERT INTO DLOCATION VALUES ('MANGALORE', '5');

INSERT INTO PROJECT VALUES (100,'IOT','BANGALORE','5');


INSERT INTO PROJECT VALUES (101,'CLOUD','BANGALORE','5');
INSERT INTO PROJECT VALUES (102,'BIGDATA','BANGALORE','5');
INSERT INTO PROJECT VALUES (103,'SENSORS','BANGALORE','3');
INSERT INTO PROJECT VALUES (104,'BANK MANAGEMENT','BANGALORE','1');
INSERT INTO PROJECT VALUES (105,'SALARY MANAGEMENT','BANGALORE','1');

Dept.of CSE,RIT,Hassan Page 47


DBMS Laboratory with mini Project 21CSL55
INSERT INTO PROJECT VALUES (106,'OPENSTACK','BANGALORE','4');
INSERT INTO PROJECT VALUES (107,'SMART CITY','BANGALORE','2');

INSERT INTO WORKS_ON VALUES (4, 'ATMECSE01', 100);


INSERT INTO WORKS_ON VALUES (6, 'ATMECSE01', 101);
INSERT INTO WORKS_ON VALUES (8, 'ATMECSE01', 102);
INSERT INTO WORKS_ON VALUES (10, 'ATMECSE02', 100);

INSERT INTO WORKS_ON VALUES (4, 'ATMECSE05', 101);


INSERT INTO WORKS_ON VALUES (5, 'ATMECSE06', 102);
INSERT INTO WORKS_ON VALUES (6, 'ATMECSE03', 102);
INSERT INTO WORKS_ON VALUES (7, 'ATMEECE01', 103);
INSERT INTO WORKS_ON VALUES (5, 'ATMEACC01', 104);
INSERT INTO WORKS_ON VALUES (6, 'ATMEACC02', 105);
INSERT INTO WORKS_ON VALUES (4, 'ATMEISE01', 106);
INSERT INTO WORKS_ON VALUES (10, 'ATMEIT01', 107);

SELECT * FROM EMPLOYEE;

SELECT * FROM DEPARTMENT ;

Dept.of CSE,RIT,Hassan Page 48


DBMS Laboratory with mini Project 21CSL55
SELECT * FROM DLOCATION ;

SELECT * FROM PROJECT ;

SELECT * FROM WORKS_ON

Dept.of CSE,RIT,Hassan Page 49


DBMS Laboratory with mini Project 21CSL55
Queries:

1. Make a list of all project numbers for projects that involve an employee whose last name

(SELECT DISTINCT P.PNO FROM PROJECT P, DEPARTMENT D, EMPLOYEE E

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

Dept.of CSE,RIT,Hassan Page 50


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.

Dept.of CSE,RIT,Hassan Page 51


DBMS Laboratory with mini Project 21CSL55
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.
4. What is a Database system?
The database and DBMS software together is called as Database system.
5. Advantages of DBMS?
Redundancy is controlled.
Unauthorized access is restricted.
Providing multiple user interfaces.
Enforcing integrity constraints.
Providing backup and recovery.
6. Disadvantage in File Processing System?
Data redundancy & inconsistency.
Difficult in accessing data.
Data isolation.
Data integrity.
Concurrent access is not possible.
Security Problems.
7. Define the "integrity rules"
There are two Integrity rules.
Entity Integrity: States that key cannot have NULL
be either a NULL value or should

Dept.of CSE,RIT,Hassan Page 52


DBMS Laboratory with mini Project 21CSL55
be Primary Key value of other relation.

Dept.of CSE,RIT,Hassan Page 53


DBMS Laboratory with mini Project 21CSL55
8. What is a view? How it is related to data independence?
A view may be thought of as a virtual table, that is, a table that does not really exist in its
own right but is instead derived from one or more underlying base table. In other words,
there is no stored file that direct represents the view instead a definition of view is stored in
data dictionary. 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.
9. What is Data Model?
A collection of conceptual tools for describing data, data relationships, data semantics and
constraints.
10. What is E-R model?
This data model is based on real world that consists of basic objects called entities and of
relationship among these objects. Entities are described in a database by a set of attributes.
11. What is Object Oriented model?
This model is based on collection of objects. An object contains values stored in instance
variables within the object. An object also contains bodies of code that operate on the object.
These bodies of code are called methods. Objects that contain same types of values and the
same methods are grouped together into classes.
12. What is an Entity?
It is an 'object' in the real world with an independent existence.
13. What is an Entity type?
It is a collection (set) of entities that have same attributes.
14. What is an attribute?
It is a particular property, which describes the entity.
15. What is degree of a Relation?
It is the number of attribute of its relation schema.
16. What is Relationship?
It is an association among two or more entities.
17. What is DDL (Data Definition Language)?
A data base schema is specified by a set of definitions expressed by a special language
called DDL.
18. What is DML (Data Manipulation Language)?

Dept.of CSE,RIT,Hassan Page 54


DBMS Laboratory with mini Project 21CSL55
This language that enable user to access or manipulate data as organized by appropriate

Dept.of CSE,RIT,Hassan Page 55


DBMS Laboratory with mini Project 21CSL55
data model.

19. What is normalization?


It is a process of analyzing the given relation schemas based on their Functional
Dependencies (FDs) and primary key to achieve the properties
Minimizing redundancy
Minimizing insertion, deletion and update anomalies.
20. What is 1 NF (Normal Form)?
The domain of attribute must include only atomic (simple, indivisible) values.
21. What is 2NF?
A relation schema R is in 2NF if it is in 1NF and every non-prime attribute A in R is fully
functionally dependent on primary key.
22. What is 3NF?
A relation schema R is in 3NF if it is in 2NF and for every FD X A either of the following is
true
X is a Super-key of R.
A is a prime attribute of R.
In other words, if every non prime attribute is non-transitively dependent on primary key.
23. What is BCNF (Boyce-Codd Normal Form)?
A relation schema R is in BCNF if it is in 3NF and satisfies additional constraints that for
every FD X A, X must be a candidate key.
24. What is 4NF?
A relation schema R is said to be in 4NF if for every Multivalued dependency X Y that
holds over R, one of following is true
X is subset or equal to (or) XY = R.
X is a super key.
25. What is 5NF?
A Relation schema R is said to be 5NF if for every join dependency {R1, R2, ...,Rn} that
holds R, one the following is true
Ri = R for some i.
The join dependency is implied by the set of FD, over R in which the left side is key
of R.

Dept.of CSE,RIT,Hassan Page 56


DBMS Laboratory with mini Project 21CSL55
26. What are partial, alternate,, artificial, compound and natural key?
Partial Key:
It is a set of attributes that can uniquely identify weak entities and that are related to same
owner entity. It is sometime called as Discriminator.
Alternate Key:
All Candidate Keys excluding the Primary Key are known as Alternate Keys.
Artificial Key:
If no obvious key, either standalone or compound is available, then the last resort is to
simply create a key, by assigning a unique number to each record or occurrence. Then this is
known as developing an artificial key.
Compound Key:
If no single data element uniquely identifies occurrences within a construct, then combining
multiple elements to create a unique identifier for the construct is known as creating a
compound key.
Natural Key:
When one of the data elements stored within a construct is utilized as the primary key, then
it is called the natural key.
27. What is meant by query optimization?
The phase that identifies an efficient execution plan for evaluating a query that has the least
estimated cost is referred to as query optimization.
28. What do you mean by atomicity and aggregation?
Atomicity:
Either all actions are carried out or none are. Users should not have to worry about the effect
of incomplete transactions. DBMS ensures this by undoing the actions of incomplete
transactions.
Aggregation:
A concept which is used to model a relationship between a collection of entities and
relationships. It is used when we need to express a relationship among relationships.
29. What is a checkpoint and when does it occur?
A Checkpoint is like a snapshot of the DBMS state. By taking checkpoints, the DBMS can
reduce the amount of work to be done during restart in the event of subsequent crashes.

30. What do you mean by flat file database?

Dept.of CSE,RIT,Hassan Page 57


DBMS Laboratory with mini Project 21CSL55
It is a database issssssssn which there are no programs or user access languages. It has no cross-file

Dept.of CSE,RIT,Hassan Page 58


DBMS Laboratory with mini Project 21CSL55
capabilities but is user-friendly and provides user-interface management.
31. Brief theory of Network, Hierarchical schemas and their properties
Network schema uses a graph data structure to organize records example for such a database
management system is CTCG while a hierarchical schema uses a tree data structure example
for such a system is IMS.
32. What is a query?
A query with respect to DBMS relates to user commands that are used to interact with a data
base. The query language can be classified into data definition language and data
manipulation language.
33. What do you mean by Correlated subquery?
Subqueries, or nested queries, are used to bring back a set of rows to be used by the parent
query. Depending on how the subquery is written, it can be executed once for the parent
query or it can be executed once for each row returned by the parent query. If the subquery
is executed for each row of the parent, this is called a correlated subquery.
A correlated subquery can be easily identified if it contains any references to the parent
subquery columns in its WHERE clause. Columns from the subquery cannot be referenced
anywhere else in the parent query. The following example demonstrates a non-correlated
subquery.
' IN (Select ODATE From ORDER Where
CUST.CNUM = ORDER.CNUM)
34. What are the primitive operations common to all record management systems?
Addition, deletion and modification
35. How do you communicate with an RDBMS?
You communicate with an RDBMS using Structured Query Language (SQL)
36. Define SQL and state the differences between SQL and other conventional
programming Languages
SQL is a nonprocedural language that is designed specifically for data access operations on
normalized relational database structures. The primary difference between SQL and other
conventional programming languages is that SQL statements specify what data operations
should be performed rather than how to perform them.
37. What is database Trigger?
A database trigger is a PL/SQL block that can defined to automatically execute for insert,
update, and delete statements against a table. The trigger can e defined to execute once for
the entire statement or once for every row that is inserted, updated, or deleted.

Dept.of CSE,RIT,Hassan Page 59


DBMS Laboratory with mini Project 21CSL55
38. What are stored-procedures? And what are the advantages of using them.
Stored procedures are database objects that perform a user defined operation. A stored
procedure can have a set of compound SQL statements. A stored procedure executes the
SQL commands and return the result to the client. Stored procedures are used to reduce
network traffic.
39. Which is the subset of SQL commands used to manipulate Database structures,
including tables?
Data Definition Language (DDL)
40. What operator performs pattern matching?
LIKE operator
41. What operator tests column for the absence of data?
IS NULL operator
42. What are the wildcards used for pattern matching?
For single character substitution and % for multi-character substitutio
43. What are the difference between TRUNCATE and DELETE commands?

TRUNCATE DELETE

TRUNCATE is a DDL command DELETE is a DML command


TRUNCATE operation cannot be DELETE operation can be rolled back
rolled back
TRUNCATE does not invoke trigger DELETE does invoke trigger
TRUNCATE resets auto_increment DELETE does not resets
value to 0 auto_increment value to 0

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

Dept.of CSE,RIT,Hassan Page 60


DBMS Laboratory with mini Project 21CSL55
Example Tables:
CREATE TABLE Customer
(
customer_id INT (6) PRIMARY KEY,
cname VARCHAR (100),
caddress VARCHAR (100)
);

CREATE TABLE Order


(
order_id INT (6) PRIMARY KEY,
products VARCHAR (100),
payment DECIMAL(10,2),
customer_id INT (6) ,
FOREIGN KEY (customer_id ) REFERENCES Customer(customer_id) ON DELETE CASCADE
);
Customer is the master table and Order is the child table, where 'customer_id is primary key in
customer table and customer_id is the foreign key in Order table and represents the customer who
placed the order. When a row of Customer is deleted, any Order row matching the deleted
Customer's customer_id will also be deleted.

47. What is the use of Floor()?


The FLOOR() function returns the largest integer value that is smaller than or equal to a number.
EXAMPLE;
SELECT FLOOR(25.75);
OUTPUT
25
48. What is the use of Truncate()?
The TRUNCATE() function truncates a number to the specified number of decimal places.
EXAMPLE;
SELECT TRUNCATE(135.375, 2);
OUTPUT
135.37
49. What is the use of CEILING?
Return the smallest integer value that is greater than or equal to 25.75:
EXAMPLE;
SELECT CEILING(25.75)
OUTPUT

Dept.of CSE,RIT,Hassan Page 61


DBMS Laboratory with mini Project 21CSL55
26

Dept.of CSE,RIT,Hassan Page 62


DBMS Laboratory with mini Project 21CSL55
50. What you mean by SQL UNIQUE Constraint?
The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a
column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint.
However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.
51. How to add and drop UNIQUE Constraint in table in mysql?
ALTER TABLE contacts ADD CONSTRAINT UNC_name_email UNIQUE(name,email)
ALTER TABLE contacts DROP INDEX UNC_name_email;
52. What is the Group by Clause?
The GROUP BY clause is a SQL command that is used to group rows that have the same
values.
The GROUP BY clause is used in the SELECT statement .Optionally it is used in conjunction
with aggregate functions to produce summary reports from the database.That's what it
does, summarizing data from the database.
The queries that contain the GROUP BY clause are called grouped queries and only return
single row for every grouped item.
Example:SELECT COUNT(CustomerID), Country FROM Customers
GROUP BY Country
53. What is use of having clause in mysql
The HAVING clause is used in the SELECT statement to specify filter conditions for a group of
rows or aggregates.
The HAVING clause is often used with the GROUP BY clause to filter groups based on a
specified condition. If the GROUP BY clause is omitted, the HAVING clause behaves like
the WHERE clause.
Notice that the HAVING clause applies a filter condition to each group of rows, while
the WHERE clause applies the filter condition to each individual row.
Example:SELECT COUNT(CustomerID), Country FROM Customers
GROUP BY Country HAVING COUNT(CustomerID) > 5;
54. What is distinct clause in SQL?
When querying data from a table, you may get duplicate rows. In order to remove these duplicate
rows, you use the DISTINCT clause in the SELECT statement.
Example:SELECT DISTINCT columns FROM table_name WHERE where_conditions;

Dept.of CSE,RIT,Hassan Page 63


DBMS Laboratory with mini Project 21CSL55
55. What is a union?
Unions combine the results from multiple SELECT queries into a consolidated result set.
The only requirements for this to work is that the number of columns should be the same from
all the SELECT queries which needs to be combined
56. What is use of MySQL Aggregate Functions?
The data that you need is not always directly stored in the tables. However, you can get it by
performing the calculations of the stored data when you select it.
By definition, an aggregate function performs a calculation on a set of values and returns a
single value.
MySQL provides many aggregate functions that include AVG, COUNT, SUM, MIN, MAX, etc.
An aggregate function ignores NULL values when it performs calculation except for
the COUNT function.
Often, aggregate functions are accompanied by the GROUP BY clause of the SELECT
statement
Below are some of aggregate functions used in sql query
AVG function
The AVG function calculates the average value of a set of values. It ignores NULL values in the
calculation.
COUNT function
The COUNT function returns the number of the rows in a table. For example, you can use below
query .below query return number of employees in Employee table
SELECT COUNT (Empname) FROM Employee
SUM function
The SUM function returns the sum of a set of values. The SUM function ignores NULL values.
If no matching row found, the SUM function returns a NULL value.
The SUM function to get the sum of salary of employees in the Employee table
SELECT SUM(Salary) FROM Employee
MAX function
The MAX function returns the maximum value in a set of values.
Below query gets maximum salary of table Employee
SELECT MAX(Salary) FROM Employee
MIN function
The MIN function returns the minimum value in a set of values.
Below query returns minimum salary of table Employee
SELECT MIN(Salary) FROM Employee

Dept.of CSE,RIT,Hassan Page 64


DBMS Laboratory with mini Project 21CSL55
Additional Queries
CREATE command
CREATE TABLE Employee
(
Empno int(4) primary key,
Empname varchar(50),
job varchar(40),
Hiredate date,
Salary decimal(10,2),
Deptno int(7),
Age int(10)
);
DESC command
DESC Employee;

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-

Dept.of CSE,RIT,Hassan Page 65

You might also like