0% found this document useful (0 votes)
404 views24 pages

DBMS Lab (18IS507) Manual With Solutions-1

This database management system lab course aims to teach students how to design ER and schema diagrams, program SQL queries using DDL and DML, and develop database applications using stored procedures, triggers and cursors. The course content includes creating tables, inserting data, updating records, and writing complex SQL queries on an example employee database and insurance database.

Uploaded by

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

DBMS Lab (18IS507) Manual With Solutions-1

This database management system lab course aims to teach students how to design ER and schema diagrams, program SQL queries using DDL and DML, and develop database applications using stored procedures, triggers and cursors. The course content includes creating tables, inserting data, updating records, and writing complex SQL queries on an example employee database and insurance database.

Uploaded by

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

DATABASE MANAGEMENT SYSTEM LAB

Sub Code: 18IS507


Credits : 01
Hrs. /Week: 0+0+2+0 Total Hours : 36

COURSE LEARNING OBJECTIVES


This Course will enable students to

1. Learn how to design ER and schema diagrams for the given database
problems and understand the mapping structure of entity relationship to
tables.
2. Program SQL queries using Data Definition and Data Manipulation Languages
3. Program SQL queries through a variety of database problems.
4. Understand the concept of stored procedures, triggers and cursors in
developing database applications.
5. Make use of complex and advanced query concepts in the developments of
real time application using the database.
Course Content:
PART A:

1. Consider the schema for Employee Database:


EMPLOYEE(SSN, FName, LName, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT(DNo, DName, MgrSSN, MgrStartDate)
DLOCATION(DNo,DLoc)
PROJECT(PNo, PName, PLocation, DNo)
WORKS_ON(SSN, PNo, Hours)
DEPENDENT(ESSN, Name, Sex, Bdate, Relationship)

SCHEMA DIAGRAM:
CREATE STATEMENTS:

EMPLOYEE table:
CREATE table employee
(
SSN varchar(10) primary key,
FName varchar(20) NOT NULL,
LName varchar(20),
Address varchar(20),
Gender CHAR(1) check (Gender in ('F','M')),
Salary decimal(10,2),
SuperSSN varchar(10) references Employee(SSN),
DNO int
);

DEPARTMENT table:
create table Department
(
DNUM int primary key check (DNUM > 0 and DNUM <= 20),
DName varchar(30) UNIQUE NOT NULL,
MGRSSN varchar(10) references Employee(SSN),
MGRStartDtae Date
);

DEPT_LOCATION table:
Create table DLocation
(
DNO int references Department(DNUM),
DLoc Varchar(30),
primary key (DNO, DLoc)
);

PROJECT table:
Create table Project
(
PNO int primary key,
PName varchar(30),
PLocation varchar(30),
DNO int references Department(DNUM)
);

WORKS_ON table:
Create table Works_on
(
ESSN varchar(10) references Employee(SSN),
PNO int references Project(PNO),
hours int,
primary key(ESSN, PNO)
);

ALTER STATEMENTS:
EMPLOYEE table:
alter table Employee add constraint FK_Emp_Dep FOREIGN KEY(DNO) references
Department(DNUM)

INSERT STATEMENTS:
EMPLOYEE table:
insert into Employee values('1111111111', 'Aishwarya', 'S', 'Tamil Nadu', 'F',
100000, NULL, NULL)
insert into Employee values('1111111112', 'Bhoomika', 'Bhat', 'Bangalore', 'F',
200000, NULL, NULL)
insert into Employee values('1111111113', 'Suhas', 'S', 'Andra Pradesh', 'M',
900000, NULL, NULL)
insert into Employee values('1111111116', 'Nidhi', 'Prabhu', 'Thirthahalli', 'F',
250000, NULL, NULL)
insert into Employee values('1111111114', 'Deeksha', 'D', 'Udupi', 'F', 800000,
NULL, NULL)
insert into Employee values('1111111115', 'Adithi', 'M', 'Manipal', 'F', 500000,
NULL, NULL)

DEPARTMENT table:
insert into Department values(1, 'Information Science', '1111111115', '07-10-2020')
insert into Department values(2, 'Electroncis and Communication', '1111111112',
'09-05-2015')
insert into Department values(3, 'Electrical and Electronics', '1111111111', '09-
09-2014')
insert into Department values(4,'Biotech','1111111114','12-15-1990')

DEPT_LOCATION table:
insert into DLocation values(1, 'Udupi')
insert into DLocation values(2, 'Bangalore')
insert into DLocation values(3, 'Mangalore')
insert into DLocation values(4, 'Manipal')

PROJECT table:
insert into Project values(20, 'P1', 'Mysore', 1)
insert into Project values(21, 'P2', 'Udupi', 3)
insert into Project values(22, 'P3', 'Hubli', 2)
insert into Project values(23, 'P4', 'Surathkal', 2)
insert into Project values(24, 'P5', 'Bangalore', 3)

WORKS_ON table:
insert into Works_on values('1111111111', '20', 7)
insert into Works_on values('1111111112', '21', 5)
insert into Works_on values('1111111113', '22', 6)
insert into Works_on values('1111111114', '23', 8)
insert into Works_on values('1111111115', '24', 8)
insert into Works_on values('1111111115', '20', 8)
insert into Works_on values('1111111116', '21', 5)
insert into Works_on values('1111111113', '14', 7)

UPDATE STATEMENTS:
EMPLOYEE table:
update Employee set SuperSSN='1111111115' where SSN='1111111114'
update Employee set SuperSSN='1111111114' where SSN='1111111111'
update Employee set SuperSSN='1111111114' where SSN='1111111112'
update Employee set SuperSSN='1111111114' where SSN='1111111113'
update Employee set SuperSSN='1111111114' where SSN='1111111116'

update Employee set DNO=1 where SSN='1111111111'


update Employee set DNO=2 where SSN='1111111112'
update Employee set DNO=3 where SSN='1111111113'
update Employee set DNO=1 where SSN='1111111114'
update Employee set DNO=2 where SSN='1111111115'
update Employee set DNO=1 where SSN='1111111116'

Update Employee set LName='Codd' where SSN=1111111115


Update Project set PName='Smart City' where PNO=24
Update Department set DName='Research' where DNUM='2'

SELECT * FROM <TABEL_NAME> for all tables:


Select * from Employee
Select * from Department

select * from DLocation

Select * from project

Select * from Works_on

Write SQL queries to


1. Make a list of all project numbers for projects that involve an employee whose last
name is ‘Codd’, either as a worker or as a manager of the department that controls
the project.
select distinct PNO from Works_on where ESSN=
(select SSN from employee where LName='Codd')
union
select distinct PNO from Project as p, Department as d
where p.DNO=d.DNUM AND d.MGRSSN=(select SSN from employee where
LName = 'Codd')
2. Show the resulting salaries if every employee working on the ‘Smart City’ project
is given a 10 percent raise.

Update Project set PName='Smart City' where PNO=24

select SSN, FName, LName, 1.1*Salary as Updated_salary


from employee where SSN in --cannot give = here as 2 ppl are working
(select ESSN from Works_on where PNO =
(select PNO from project where PName='Smart city'))

3. Find the sum of the salaries of all employees of the ‘Information Science’
department, as well as the maximum salary, the minimum salary, and the average
salary in this department.
Update Department set DName='Research' where DNUM='2'

select SUM(e.salary) as Total_Salary, MAX(e.salary) as Max_Salary


, MIN(e.salary) as Min_Salary, AVG(e.salary) as Avg_Salary
from employee e , Department d
where d.DName='Research' and e.DNO=d.DNUM

4. Retrieve the name of each employee who works on all the projects controlled by
department number 5.
select e.FName,e.LName
from Department d,employee e,Works_on w,Project p
where d.DNUM=e.DNO and e.SSN=w.ESSN and w.PNO=p.PNO and
p.DNO=3
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.
10,00,000.
select DNO, count(*) as Num_of_emps
from employee
where Salary > 100000 and DNO in
(select DNO
from employee group by DNO
having count(*)>2)
group by DNO

2. Consider the Insurance database given below.


PERSON (driver – id #: String, name: string, address: string)
CAR (regno: string, model: string, year: int)
ACCIDENT (report-number: int, accd-date: date, location: string)
OWNS (driver-id #: string, regno: string)
PARTICIPATED (driver-id: string, Regno: string, report-number: int, damage
amount: int)
SCHEMA DIAGRAM:

CREATE STATEMENTS:

PERSON table:
CREATE TABLE PERSON
(
D_ID VARCHAR(10) PRIMARY KEY,
D_NAME VARCHAR(25) NOT NULL,
ADDR VARCHAR(30)
);

CAR table:
CREATE TABLE CAR
(
REG_NO VARCHAR(10) PRIMARY KEY,
MODEL VARCHAR(10) NOT NULL,
YEAR DATE
);

ACCIDENT table:
CREATE TABLE ACCIDENT
(
REPORT_NUM INT PRIMARY KEY,
ACC_DATE DATE,
ACC_LOC VARCHAR(30)
);

OWNS table:
CREATE TABLE OWNS
(
D_ID VARCHAR(10) REFERENCES PERSON(D_ID),
REG_NO VARCHAR(10) REFERENCES CAR(REG_NO),
PRIMARY KEY(D_ID,REG_NO)
);

PARTICIPATED table:
CREATE TABLE PARTICIPATED
(
D_ID VARCHAR(10) REFERENCES PERSON(D_ID),
REG_NO VARCHAR(10) REFERENCES CAR(REG_NO),
REPORT_NUM INT REFERENCES ACCIDENT(REPORT_NUM),
DAM_AMOUNT INT,
PRIMARY KEY (D_ID,REG_NO,REPORT_NUM)
);

INSERT STATEMENTS:
AND
SELECT * FROM <TABEL_NAME> for all tables:

PERSON table:
INSERT INTO PERSON VALUES('00001','AAAA','UDUPI')
INSERT INTO PERSON VALUES('00002','BBBB','MANGALORE')
INSERT INTO PERSON VALUES('00003','CCCC','BANGALORE')
INSERT INTO PERSON VALUES('00004','EEEE','MANGALORE')
INSERT INTO PERSON VALUES('00005','FFFF','BANGALORE')
INSERT INTO PERSON VALUES('00006','GGGG','MANGALORE')

SELECT * FROM PERSON

CAR table:
INSERT INTO CAR VALUES('101','A','2000-03-28')
INSERT INTO CAR VALUES('102','B','2000-04-28')
INSERT INTO CAR VALUES('103','C','2000-05-28')
INSERT INTO CAR VALUES('104','D','2000-06-28')
INSERT INTO CAR VALUES('105','E','2000-07-28')
INSERT INTO CAR VALUES('106','F','2000-08-28')

SELECT * FROM CAR

ACCIDENT table:
INSERT INTO ACCIDENT VALUES(1,'2002-04-25','UDUPI')
INSERT INTO ACCIDENT VALUES(2,'2003-05-05','BANGALORE')
INSERT INTO ACCIDENT VALUES(3,'2005-06-26','MANGALORE')
INSERT INTO ACCIDENT VALUES(4,'2002-07-15','UDUPI')
INSERT INTO ACCIDENT VALUES(5,'2008-08-03','MANGALORE')

SELECT * FROM ACCIDENT


OWNS table:
INSERT INTO OWNS VALUES('00001','101')
INSERT INTO OWNS VALUES('00002','102')
INSERT INTO OWNS VALUES('00003','103')
INSERT INTO OWNS VALUES('00004','104')
INSERT INTO OWNS VALUES('00005','105')
INSERT INTO OWNS VALUES('00001','106')

UPDATE OWNS SET D_ID='00006' WHERE REG_NO='106'

SELECT * FROM OWNS

PARTICIPATED table:
INSERT INTO PARTICIPATED VALUES('00001','101',1,2000)
INSERT INTO PARTICIPATED VALUES('00002','102',2,2000)
INSERT INTO PARTICIPATED VALUES('00003','103',3,2000)
INSERT INTO PARTICIPATED VALUES('00004','104',4,2000)
INSERT INTO PARTICIPATED VALUES('00005','105',5,2000)
INSERT INTO PARTICIPATED VALUES('00006','106',5,2000)

UPDATE PARTICIPATED SET DAM_AMOUNT=5000 WHERE REG_NO='106'


UPDATE PARTICIPATED SET DAM_AMOUNT=6000 WHERE REG_NO='104'
UPDATE PARTICIPATED SET DAM_AMOUNT=3000 WHERE REG_NO='103'
UPDATE PARTICIPATED SET DAM_AMOUNT=4000 WHERE REG_NO='102'

SELECT * FROM PARTICIPATED

Write SQL queries to


1. List the names of people who owned cars that were involved in accidents in
2008.
SELECT P.D_NAME
FROM PERSON P,PARTICIPATED P1,ACCIDENT A
WHERE P.D_ID =P1.D_ID AND P1.REPORT_NUM=A.REPORT_NUM
AND ACC_DATE LIKE '2008-__-__'
2. Find the name of owner and his car that has maximum number of accidents in
2008
SELECT P.D_NAME,C.MODEL
FROM PERSON P,CAR C,ACCIDENT A,PARTICIPATED P1
WHERE P.D_ID=P1.D_ID AND C.REG_NO=P1.REG_NO AND
A.REPORT_NUM=P1.REPORT_NUM AND A.ACC_DATE LIKE '2008-__-
__'
GROUP BY P.D_NAME,C.MODEL
HAVING COUNT(*)>= ALL(SELECT COUNT(*)
FROM PERSON P2,CAR C1,ACCIDENT A1,PARTICIPATED P4
WHERE P2.D_ID=P4.D_ID AND C1.REG_NO=P4.REG_NO AND
A1.REPORT_NUM=P4.REPORT_NUM AND A1.ACC_DATE LIKE '2008-
__-__'
GROUP BY P2.D_NAME,C1.MODEL)
3. List the name of owners who own atleast two TOYOTA cars.
SELECT P.D_NAME
FROM PERSON P, CAR C,OWNS O
WHERE P.D_ID=O.D_ID AND C.REG_NO=O.REG_NO AND
C.MODEL='A'
GROUP BY P.D_NAME
HAVING COUNT(*)>=1

UPDATE CAR SET MODEL='A' WHERE REG_NO=102


4. List the name of owner who owns maximum TOYOTA cars.
INSERT INTO CAR VALUES ('107','A','2000-02-28')

INSERT INTO OWNS VALUES('00001','107')

SELECT P.D_NAME,COUNT(*) AS NUM_OF_CARS


FROM PERSON P,CAR C,OWNS O
WHERE P.D_ID=O.D_ID AND O.REG_NO=C.REG_NO AND
C.MODEL='A'
GROUP BY P.D_NAME
HAVING COUNT(*) >= ALL
(SELECT COUNT(*) AS NUM_OF_CARS
FROM PERSON P1,CAR C1,OWNS O1
WHERE P1.D_ID=O1.D_ID AND O1.REG_NO=C1.REG_NO AND
C1.MODEL='A'
GROUP BY P1.D_NAME)

5. Find the name of owner who owns cars having minimum damage amount for
accidents in 2008
SELECT P.D_NAME,SUM(P1.DAM_AMOUNT)
FROM PERSON P,ACCIDENT A,PARTICIPATED P1
WHERE P.D_ID=P1.D_ID AND A.REPORT_NUM=P1.REPORT_NUM
AND A.ACC_DATE LIKE '2008-__-__'
GROUP BY P.D_NAME
HAVING SUM(P1.DAM_AMOUNT) <= ALL
(SELECT SUM(P4.DAM_AMOUNT)
FROM PERSON P2,ACCIDENT A1,PARTICIPATED P4

3. Consider the following database for a banking enterprise:


BRANCH (branch-name: string, branch-city: string, assets: real)
ACCOUNT (accno: int, branch-name: string, balance: real)
DEPOSITOR (customer-name: string, accno: int)
CUSTOMER (customer-name: string, customer-street: string, customer-city: string)
LOAN (loan-number: int, branch-name: string, amount: real)
BORROWER (customer-name: string, loan-number: int)
SCHEMA DIAGRAM:

Write SQL queries to


1. Find all the customers who have at least two accounts at the Main branch. (*)
2. Find all the customers who have an account at all the branches located in a specific
city (*)
3. Find all the customers who have accounts in at least 2 branches located in a specific
city.
4. Find all the customers who have borrowed loan from at least one branch that is
located in a specific city.
5. Find the branch name that has maximum number of customers in a specific city

CREATE STATEMENTS:
BRANCH TABLE:
create table Branch(
bname varchar(15),
bcity varchar(15),
assets real,
primary key(bname)
);

ACCOUNT TABLE:
create table Account(
accno int,
bname varchar(15) default 'Manipal',
balance real,
primary key(accno),
foreign key(bname) references Branch(bname) on delete cascade on update cascade
);

CUSTOMER TABLE:
create table Customer
(
cname varchar(20),
cstreet varchar(25),
ccity varchar(20),
primary key(cname),
);

LOAN TABLE:
create table Loan
(loan_no int,
bname varchar(15),
amount real,
primary key(loan_no),
foreign key(bname)references Branch(bname) on delete cascade on update cascade
);

BORROWER TABLE:
create table Borrower
(cname varchar(20),
loan_no int,
primary key(cname,loan_no),
foreign key(cname)references Customer(cname) on delete cascade on update cascade,
foreign key(loan_no)references Loan(loan_no) on delete cascade on update cascade,
unique(loan_no)
);

DEPOSITOR TABLE:
create table Depositor
(
cname varchar(20),
accno int,
primary key(cname,accno),
foreign key(accno)references Account(accno) on delete cascade on update cascade,
foreign key(cname)references Customer(cname) on delete cascade on update cascade,
unique(accno)
);
INSERT STATEMENTS:
BRANCH TABLE:
insert into Branch values('SBI_Udupi','Udupi','30000')
insert into Branch values('SBI_Mangalore','Mangalore','20000')
insert into Branch values('SBI_Manipal','Udupi','40000')
insert into Branch values('SBI_Karkala','Karkala','30000')
insert into Branch values('SBI_Bangalore','Bangalore','20000')
insert into Branch values('SBI_Sasthana','Udupi','40000')
insert into Branch values('SBI_Surathkal','Mangalore','40000')

ACCOUNT TABLE:
insert into Account values('123456781','SBI_Udupi','300000')
insert into Account values('123456782','SBI_Bangalore','310000')
insert into Account values('123456783','SBI_Sasthana','200000')
insert into Account values('123456784','SBI_Mangalore','500000')
insert into Account values('123456785','SBI_Surathkal','360000')
insert into Account values('123456786','SBI_Udupi','870000')
insert into Account values('123456787','SBI_Bangalore','650000')
insert into Account values('123456788','SBI_Manipal','40000')
insert into Account values('123456789','SBI_Mangalore','230000')
insert into Account values('123456770','SBI_Karkala','350000')
insert into Account values('123456771','SBI_Manipal','130000')
insert into Account values('123456772','SBI_Bangalore','350000')
insert into Account values('123456773','SBI_Bangalore','360000')
insert into Account values('123456774','SBI_Mangalore','700000')
insert into Account values('123456775','SBI_Udupi','800000')
insert into Account values('123456791','SBI_Bangalore','360000')
insert into Account values('123456792','SBI_Bangalore','380000')
insert into Account values('123456793','SBI_Sasthana','400000')
insert into Account values('123456794','SBI_Surathkal','600000')
insert into Account values('123456795','SBI_Udupi','360000')

CUSTOMER TABLE:
insert into Customer values('Nidhi','8th cross','Udupi');
insert into Customer values('Maneesha','4th cross','Mangalore');
insert into Customer values('Mallik','2nd cross','Karkala');
insert into Customer values('Nisha','5th cross','Bangalore');
insert into Customer values('Sanjana','13th cross','Mangalore');
insert into Customer values('Rahul','7th cross','Udupi');

DEPOSITOR TABLE:
insert into Depositor values('Rahul','123456781')
insert into Depositor values('Mallik','123456785')
insert into Depositor values('Nidhi','123456784')
insert into Depositor values('Sanjana','123456786')
insert into Depositor values('Nidhi','123456783')
insert into Depositor values('Maneesha','123456782')
insert into Depositor values('Sanjana','123456787')
insert into Depositor values('Mallik','123456788')
insert into Depositor values('Nidhi','123456789')
insert into Depositor values('Sanjana','123456771')
insert into Depositor values('Mallik','123456773')
insert into Depositor values('Maneesha','123456772')
insert into Depositor values('Rahul','123456770')
insert into Depositor values('Mallik','123456774')
insert into Depositor values('Nidhi','123456791')
insert into Depositor values('Sanjana','123456775')
insert into Depositor values('Nisha','123456792')
insert into Depositor values('Maneesha','123456793')
insert into Depositor values('Mallik','123456794')
insert into Depositor values('Maneesha','123456795')

LOAN TABLE:
insert into Loan values(1,'SBI_Karkala','12000');
insert into Loan values(2,'SBI_Udupi','15000');
insert into Loan values(3,'SBI_Manipal','20000');
insert into Loan values(4,'SBI_Bangalore','30000');
insert into Loan values(5,'SBI_Mangalore','13000');
insert into Loan values(6,'SBI_Surathkal','13000');
insert into Loan values(7,'SBI_Sasthana','13000');
insert into Loan values(8,'SBI_Manipal','25000');
insert into Loan values(9,'SBI_Bangalore','35000');
insert into Loan values(10,'SBI_Udupi','65000');
insert into Loan values(11,'SBI_Mangalore','33000');
insert into Loan values(12,'SBI_Karkala','62000');
insert into Loan values(13,'SBI_Udupi','35000');
insert into Loan values(14,'SBI_Manipal','17000');

BORROWER TABLE:
insert into Borrower values('Nidhi',9);
insert into Borrower values('Mallik',6);
insert into Borrower values('Maneesha',2);
insert into Borrower values('Nisha',4);
insert into Borrower values('Sanjana',12);
insert into Borrower values('Rahul',3);
insert into Borrower values('Nisha',14);
insert into Borrower values('Nidhi',5);
insert into Borrower values('Mallik',7);
insert into Borrower values('Maneesha',8);
insert into Borrower values('Sanjana',11);
insert into Borrower values('Rahul',13);
insert into Borrower values('Nisha',10);
insert into Borrower values('Sanjana',1);

SELECT * FROM <TABEL_NAME> for all tables:


select * from Branch

select * from Account

select * from Customer

select * from Depositor


select * from Loan

select * from Borrower

1. Find all the customers who have at least two accounts at the Main branch. (*)

select d.cname,count(*) as No_of_accounts


from Account a, Depositor d
where a.accno=d.accno and a.bname='SBI_Udupi'
group by d.cname
having count(*)>1

2. Find all the customers who have an account at all the branches located in a specific
city (*)

select *
from Customer C
where NOT EXISTS (
select B.bname
from Branch B
where B.bcity='Bangalore'

except

select distinct A.bname


from Depositor D, Account A, Branch B
where D.accno=A.accno AND A.bname=B.bname AND C.ccity='Bangalore'
AND D.cname=C.cname);

3. Find all the customers who have accounts in at least 2 branches located in a specific
city.

select d.cname, count(distinct a.bname) number_of_branches


from Account a, Depositor d, Branch b
where a.accno=d.accno and a.bname=b.bname and b.bcity='Udupi'
group by d.cname
having count(distinct a.bname)>=2

4. 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)
VIEWER(Viewer_ID, Name, Age, Gender)
RATINGS(Viewer_ID, Mov_id , Stars)

SCHEMA DIAGRAM:
CREATE STATEMENTS:

ACTOR table:
CREATE TABLE ACTOR
(
ACT_ID INT PRIMARY KEY,
ACT_NAME varchar(30),
ACT_GENDER CHAR(1) CHECK (ACT_GENDER IN('M','F'))
);

DIRECTOR table:
CREATE TABLE DIRECTOR
(
DIR_ID INT PRIMARY KEY,
DIR_NAME VARCHAR(25),
dir_phone INT
);

MOVIES table:
CREATE TABLE MOVIES
(
MOV_ID INT PRIMARY KEY,
MOV_TITLE VARCHAR(50),
MOV_YEAR INT,
MOV_LANG VARCHAR(20),
DIR_ID INT REFERENCES DIRECTOR(DIR_ID),
UNIQUE(MOV_TITLE)
);

MOVIE_CAST table:
CREATE TABLE MOVIE_CAST(
ACT_ID INT REFERENCES ACTOR(ACT_ID),
MOV_ID INT REFERENCES MOVIES(MOV_ID),
ROLE VARCHAR(20),
PRIMARY KEY(ACT_ID,MOV_ID)
);

VIEWER table:
CREATE TABLE VIEWER(
VIEW_ID INT PRIMARY KEY,
VIEW_NAME VARCHAR(25),
AGE INT,
VIEW_GENDER char(1) CHECK (VIEW_GENDER IN('F','M'))
);

RATINGS table:
CREATE TABLE RATINGS(
VIEW_ID INT REFERENCES VIEWER(VIEW_ID),
MOV_ID INT REFERENCES MOVIES(MOV_ID),
STARS INT CHECK(STARS>=0 AND STARS<=5)
PRIMARY KEY(VIEW_ID,MOV_ID),
);

INSERT STATEMENTS:
AND
SELECT * FROM <TABEL_NAME> for all tables:
ACTOR table:
INSERT INTO ACTOR VALUES (101,'SHAHID KAPOOR','M')
INSERT INTO ACTOR VALUES (102,'ANUSHKA SHARMA','F')
INSERT INTO ACTOR VALUES (103,'RANBIR KAPOOR','M')
INSERT INTO ACTOR VALUES (104,'ALIA BHAT','F')
INSERT INTO ACTOR VALUES (105,'PARINITHI CHOPRA','F')
INSERT INTO ACTOR VALUES (106,'VARUN DHAVAN','M')

SELECT *FROM ACTOR

DIRECTOR table:
INSERT INTO DIRECTOR VALUES (1001,'AAAA',93876259)
INSERT INTO DIRECTOR VALUES (1002,'BBBB',83761947)
INSERT INTO DIRECTOR VALUES (1003,'CCCC',85679026)
INSERT INTO DIRECTOR VALUES (1004,'DDDD',97254689)
INSERT INTO DIRECTOR VALUES (1005,'EEEE',84732648)

SELECT *FROM DIRECTOR

MOVIES table:
INSERT INTO MOVIES VALUES (1,'JAB WE MET',2007,'HINDI',1001)
INSERT INTO MOVIES VALUES (2,'PK',2014,'ENGLISH',1004)
INSERT INTO MOVIES VALUES (3,'YEH JAWANI HAI DIWANI',2013,'KANNADA',1005)
INSERT INTO MOVIES VALUES (4,'STUDENT OF THE YEAR',2012,'HINDI',1003)
INSERT INTO MOVIES VALUES (5,'ISHAQZAADE',2012,'HINDI',1001)
INSERT INTO MOVIES VALUES (6,'ABCD2',2015,'ENGLISH',1002)
INSERT INTO MOVIES VALUES (7,'KABIR SINGH',2018,'HINDI',1001)

SELECT *FROM MOVIES

MOVIE_CAST table:
INSERT INTO MOVIE_CAST VALUES (101,1,'HERO')
INSERT INTO MOVIE_CAST VALUES (102,2,'HEROINE')
INSERT INTO MOVIE_CAST VALUES (103,3,'HERO')
INSERT INTO MOVIE_CAST VALUES (104,4,'HEROINE')
INSERT INTO MOVIE_CAST VALUES (105,5,'HEROINE')
INSERT INTO MOVIE_CAST VALUES (106,6,'HERO')
INSERT INTO MOVIE_CAST VALUES (101,7,'HERO')

SELECT *FROM MOVIE_CAST


VIEWER table:
INSERT INTO VIEWER VALUES (1,'ZZZZ',22,'M')
INSERT INTO VIEWER VALUES (2,'YYYY',24,'F')
INSERT INTO VIEWER VALUES (3,'XXXX',24,'M')
INSERT INTO VIEWER VALUES (4,'WWWW',38,'M')
INSERT INTO VIEWER VALUES (5,'NNNN',20,'F')
INSERT INTO VIEWER VALUES (6,'SSSS',20,'M')
INSERT INTO VIEWER VALUES (7,'UUUU',20,'F')
INSERT INTO VIEWER VALUES (8,'VVVV',20,'M')

SELECT *FROM VIEWER

RATINGS table:
INSERT INTO RATINGS VALUES (1,3,5)
INSERT INTO RATINGS VALUES (1,5,4)
INSERT INTO RATINGS VALUES (2,6,5)
INSERT INTO RATINGS VALUES (3,4,4)
INSERT INTO RATINGS VALUES (3,5,4)
INSERT INTO RATINGS VALUES (4,2,4)
INSERT INTO RATINGS VALUES (5,1,5)
INSERT INTO RATINGS VALUES (6,1,5)

SELECT *FROM RATINGS

Write SQL queries to


1. List all actors who acted in a movie before 2000 and also in a movie after 2017.
SELECT A.ACT_NAME
FROM ACTOR A,MOVIE_CAST MC,MOVIES M
WHERE A.ACT_ID=MC.ACT_ID AND MC.MOV_ID=M.MOV_ID AND
MOV_YEAR<2012
INTERSECT
SELECT A.ACT_NAME
FROM ACTOR A,MOVIE_CAST MC,MOVIES M
WHERE A.ACT_ID=MC.ACT_ID AND MC.MOV_ID=M.MOV_ID AND
MOV_YEAR>2017
2. 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.
UPDATE RATINGS SET STARS=5
WHERE MOV_ID IN(SELECT M.MOV_ID
FROM MOVIES M,DIRECTOR D
WHERE M.DIR_ID=D.DIR_ID AND
D.DIR_NAME='BBBB')

SELECT D.DIR_NAME,R.STARS
FROM RATINGS R,MOVIES M,DIRECTOR D
WHERE STARS = 5 AND R.MOV_ID=M.MOV_ID AND
D.DIR_ID=M.DIR_ID AND D.DIR_NAME='BBBB'
3. Update rating of all movies directed by ‘Subhash Ghai’ to 5.
SELECT M.MOV_TITLE,MAX(R.STARS) AS RATINGS
FROM MOVIES M,RATINGS R
WHERE M.MOV_ID=R.MOV_ID AND R.STARS>1
GROUP BY M.MOV_TITLE

5. Consider the schema for StudentDatabase:


STUDENT(USN, Name, DOB, Gender, Address,Dept_Id)
DEPARTMENT (Dept_ID, Dept_Name)
COURSE(Course_Id, Course_Title, Credits,Dept_Id)
GRADE_REPORT(USN, Course_ID, MSE1,MSE2,Task,SEE, Total)
CLASS(Sem, Sec , USN)

SCHEMA DIAGRAM:
CREATE STATEMENTS:

STUDENT table:
create table Student
(
usn varchar(10) primary key,
s_name varchar(15) not null,
dept_id int references Department(dept_id),
dob date,
gender char(1)
)

DEPARTMENT table:
create table Department
(
dept_id int primary key,
dept_name varchar(20) not null
)

COURSE table:
create table Course
(
course_id varchar(10) primary key,
course_title varchar(20) not null,
credits int,
dept_id int references Department(dept_id)
)

GRADE_REPORT table:
create table Grade_report
(
usn varchar(10) references Student(usn),
course_id varchar(10) references Course(course_id),
mse1 int,
mse2 int,
task int,
see int,
total int,
primary key (USN,course_id)
)

CLASS table:
create table Class
(
usn varchar(10) references Student(usn),
sem int not null,
sec char(1) not null,
primary key(usn,sem,sec)
)

INSERT STATEMENTS:
AND
SELECT * FROM <TABEL_NAME> for all tables:
STUDENT table:
insert into Student values('123456789','aaaaa',3,'2000-12-12','f')
insert into Student values('123456788','bbbbb',2,'2000-11-12','f')
insert into Student values('123456787','ccccc',4,'2000-10-12','m')
insert into Student values('123456786','ddddd',1,'2000-12-11','m')
insert into Student values('123456785','eeeee',3,'2000-12-10','f')
insert into Student values('123456784','fffff',2,'2000-9-12','m')
insert into Student values('123456783','ggggg',3,'2000-12-12','f')
insert into Student values('123456782','hhhhh',2,'2000-12-7','m')

select * from Student


DEPARTMENT table:
insert into Department values (1,'ECE')
insert into Department values (2,'CSE')
insert into Department values (3,'ISE')
insert into Department values (4,'BT')

select * from Department

COURSE table:
insert into Course values ('ISE101','DS',4,1)
insert into Course values ('ECE101','DSD',3,1)
insert into Course values ('CSE101','DC',2,1)
insert into Course values ('ECE102','MPI',3,1)
insert into Course values ('ISE102','DSM',2,1)
insert into Course values ('CSE102','DAA',4,1)
insert into Course values ('BT101','MB',3,1)
insert into Course values ('BT102','MT',3,1)

select * from Course

GRADE_REPORT table:
insert into Grade_report values('123456789','ISE101',20,19,7,47,93)
insert into Grade_report values('123456789','ISE102',20,19,7,48,94)

insert into Grade_report values('123456788','CSE101',20,19,9,47,95)


insert into Grade_report values('123456788','CSE102',20,19,7,47,93)

insert into Grade_report values('123456787','BT101',20,20,8,48,96)


insert into Grade_report values('123456787','BT102',20,18,8,46,92)

insert into Grade_report values('123456786','ECE101',20,18,7,48,93)


insert into Grade_report values('123456786','ECE102',19,19,7,45,90)

insert into Grade_report values('123456785','ISE101',20,19,7,37,83)


insert into Grade_report values('123456785','ISE102',20,19,7,38,84)

insert into Grade_report values('123456784','CSE101',20,19,9,47,95)


insert into Grade_report values('123456784','CSE102',20,19,7,47,93)
insert into Grade_report values('123456783','ISE101',20,20,8,48,96)
insert into Grade_report values('123456783','ISE102',20,18,8,46,92)

insert into Grade_report values('123456782','CSE101',20,18,7,48,93)


insert into Grade_report values('123456782','CSE102',19,19,7,45,90)

select * from Grade_report

CLASS table:
insert into Class values('123456789',5,'A')
insert into Class values('123456788',3,'B')
insert into Class values('123456787',7,'A')
insert into Class values('123456786',3,'A')
insert into Class values('123456785',5,'B')
insert into Class values('123456784',3,'B')
insert into Class values('123456783',5,'B')
insert into Class values('123456782',7,'B')

select * from Class

Write SQL queries to


1. List all the student details studying in fourth semester ‘B’ section.
select * from Student

SELECT S.USN, S.s_name, S.DOB, S.GENDER


FROM CLASS C, STUDENT S
WHERE C.USN=S.USN AND C.SEM=5 AND C.SEC='B'

2. Compute the total number of male and female students in each semester and in each
section.
SELECT C.SEM,C.SEC, COUNT (CASE WHEN S.GENDER='F' THEN 1
END) AS FEMALE_STUDENTS, COUNT (CASE WHEN GENDER='M'
THEN 1 END) AS MALE_STUDENTS
FROM STUDENT S, CLASS C
WHERE S.USN=C.USN
GROUP BY C.SEM, C.SEC
3. Create a view of MSE1 marks of student USN ‘4NM17IS001’ in all subjects.
CREATE VIEW MSE1__MARKS AS
SELECT C.COURSE_ID, C.COURSE_TITLE, G.MSE1
FROM COURSE C, GRADE_REPORT G
WHERE C.COURSE_ID=G.COURSE_ID AND G.USN='123456782'

SELECT * FROM MSE1__MARKS

PART B

Create a database application using any programming language as the front end
and any database software as a backend. For any problem selected, write the ER
Diagram, apply ER-mapping rules, normalize the relations, and follow the
application development process. Make sure that the application developed
should have a minimum of five tables, at least one trigger and one stored
procedure. Indicative areas include; health care, education, industry, transport,
library.

COURSE OUTCOMES

At the end of the course the student will be able to

Sl. No. Course Outcome (CO) PI Bloom’s


Taxonomy
Level (BTL)

C508.1
Make use of ER diagrams concepts to design a L3
1.3.1,1.4.1,2.4
database for a given real world scenarios. .4,4.2.1
C508.2 Make use of Schema diagrams concepts to design a 1.3.1,1.4.1,2.4
L3
database for a given real world scenarios. .4,4.2.1
C508.3 Analyze abstract problems and apply a combination of 1.3.1,1.4.1,2.4
hardware and software to address problems. .4,4.1.3,4.2.1,
L4
Implement database creation using Data Definition 5.1.2
Language(DDL) concepts.
C508.4 Apply the DML(Data Manipulation Langauge) 1.3.1,1.4.1,2.4
Concepts to query the Database .4,4.1.3,4.2.1, L3
5.1.2
C508.5 1.3.1,1.4.1,2.4
Apply the concepts of complex queries in database L3
.4,4.1.3,4.2.1,
environment 5.1.2

Mapping of POs & COs:


POs /
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
Cos
C508.1 1 2 2 1 1
C508.2 1 2 2 1 1
C508.3 2 2 2 2 1 1 3
C508.4 2 2 2 2 1 1
C508.5 2 2 2 2 1 1 2
(L = 1: 30%-49%, M = 2: 50%-69%, H = 3: >70%)

TEXTBOOK:
1. Database Systems Models, Languages, Design and Application Programming, Ramez
Elmasri and Shamkant B. Navathe, 7th Edition, 2017, Pearson.

REFERENCE BOOKS:
1. Database System Concepts- Silberschatz, Korth and Sudharshan, Sixth Edition, Mc-
GrawHill, 2010

E-RESOURCES:
1. https://swayam.gov.in/nd1_noc19_cs46/preview , Database Management System,
Swayam.
2. https://www.coursera.org/learn/intro-sql, Introduction to Structured Query Language
(SQL), coursera.
3. https://www.coursera.org/projects/introduction-to-relational-database-and-sql,
Introduction to Relational Database and SQL.

Note: In the examination, each student will pick one full question from 5 database
scenarios.

********************

You might also like