0% found this document useful (0 votes)
48 views42 pages

Dbms Lab Manual Harsha

The document outlines a DBMS laboratory assignment involving the creation of tables for a college database, including BRANCH, STUDENT, BOOK, AUTHOR, and BORROW, with specified primary and foreign keys. It includes SQL queries to retrieve various data such as students in a specific semester, students who have not borrowed books, and details of borrowed books. Additionally, it provides a second part with a schema for a STUDENT table that includes operations for updating total marks and calculating GPA.

Uploaded by

nadig.shashank07
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)
48 views42 pages

Dbms Lab Manual Harsha

The document outlines a DBMS laboratory assignment involving the creation of tables for a college database, including BRANCH, STUDENT, BOOK, AUTHOR, and BORROW, with specified primary and foreign keys. It includes SQL queries to retrieve various data such as students in a specific semester, students who have not borrowed books, and details of borrowed books. Additionally, it provides a second part with a schema for a STUDENT table that includes operations for updating total marks and calculating GPA.

Uploaded by

nadig.shashank07
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/ 42

DBMS Laboratory 22MCAL27

1) Create the following tables with properly specifying Primary keys, foreign keys and
solve the following queries.

BRANCH(Branchid,Branchname,HOD)

STUDENT(USN,Name,Address,Branchid,Sem)

BOOK(Bookid,Bookname,Authorid,Publisher,Branchid)

AUTHOR(Authorid,Authorname,Country,Age)

BORROW(USN,Bookid,Borrowdate)

Execute the following Queries:

i. List the details of Students who are all studying in 2nd sem MCA.

ii.List the students who are not borrowed any books.

iii. Display the USN, Student name, Branch_name, Book_name, Author_name,


Books_Borrowed_Date of 2nd sem MCA Students who borrowed books.

iv. Display the number of books written by each Author.

v.Display the student details who borrowed more than two books.

vi.Display the student details who borrowed books of more than one Author.

vii.Display the Book names in descending order of their names.

viii.List the details of students who borrowed the books which are all published by the
same publisher.

JNN College of Engineering Page | 1


DBMS Laboratory 22MCAL27

JNN College of Engineering Page | 2


DBMS Laboratory 22MCAL27

TABLE: BRANCH

Create table BRANCH

(Branchid varchar(10)Primary key,

Branchname varchar(10),

HOD varchar(10));

Insert into BRANCH(Branchid,Branchname,HOD)values('B01','MCA','ADO');

-----------------------------------------

Branchid Branchname HOD

-----------------------------------------

B01 MCA ADO

B02 MBA ABC

B03 MTech XYZ

B04 BE PQR

BO5 BCom MNO

------------------------------------------

TABLE: STUDENT

Create table STUDENT

(USN varchar(10)Primary key,

Name varchar(10),

Address varchar(50),

Branchid varchar(10)references Branch,

Sem number(1));

JNN College of Engineering Page | 3


DBMS Laboratory 22MCAL27

Insert into STUDENT(USN,Name,Address,Branchid,Sem) values('4JN17MCA11','Jishi','#9


,kallalli','B01',2);

-------------------------------------------------------------------------------------------------

USN Name Address Branchid Sem

--------------------------------------------------------------------------------------------------

4JN17MCA11 Jishi #9,kallalli B01 2

4JN17MCA37 Suraksha #20,Vinobanagar B01 2

4JN17MCA08 Ananya #11,Hudco B01 1

4JN17MBA10 Namitha #5,Gopala B02 2

4JN17MBA01 Samanyu #1,Bhadravathi B02 1

---------------------------------------------------------------------------------------------------

TABLE: BOOK

Create table BOOK

(Bookid varchar(10)Primary key,

Bookname varchar(10),

Authorid varchar(10)references Author,

Publisher varchar(10);

Branchid varchar(10)references Branch);

Insert into BOOK(Bookid,Bookname,Authorid,Publisher,Branchid)values('BK01','DBMS','


A01','KJF','B01');

JNN College of Engineering Page | 4


DBMS Laboratory 22MCAL27

-------------------------------------------------------------------------

Bookid Bookname Authorid Publisher Branchid

--------------------------------------------------------------------------

BK01 DBMS A01 KJF B01

BK02 OOP A05 UVW B01

BK03 DMS A03 KJF B01

BK04 UNIX A04 OPQ B01

BK05 C++ A02 KJF B01

--------------------------------------------------------------------------

TABLE: AUTHOR

Create table AUTHOR

(Authorid varchar(10)Primary key,

Authorname varchar(20),

Country varchar(10),

Age number(2));

Insert into AUTHOR(Authorid,Authorname,Country,Age)values('A01','Elmasri Navathe','India',44);

-----------------------------------------------------------------------------

Authorid Authorname Country Age

-----------------------------------------------------------------------------

A01 Elmasri Navathe India 44

A02 Balguru Swami India 50

A03 Girimaldi India 40

A04 Sumitabadas India 36

A05 K R Venugopal India 54

A06 Dennis Richie India 60

JNN College of Engineering Page | 5


DBMS Laboratory 22MCAL27

TABLE:BORROW

Create table BORROW

(USN varchar(10)references STUDENT,

Bookid varchar(10)references BOOK,

Borrowdate date);

Insert into BORROW(USN,,Bookid,Borrowdate)values('4JN17MCA11','BK01','2018-04-03');

Insert into BORROW(USN,,Bookid,Borrowdate)values('4JN17MCA11','BK01','03-APR-18');

-------------------------------------------------------------

USN Bookid Borrowdate

-------------------------------------------------------------

4JN17MCA11 BK01 2018-04-03

4JN17MCA11 BK03 2018-03-04

4JN17MCA37 BK02 2018-01-11

4JN17MCA08 BK04 2018-02-05

4JN17MCA37 BK01 2018-06-14

4JN17MCA11 BK05 2018-05-11

----------------------------------------------------------------

a) List the details of students who are all studying in 2nd sem MCA

Select USN,Name,Address from STUDENT S,BRANCH B where S.Branchid =B.Branchid and


Sem=2 and Branchname='MCA';

JNN College of Engineering Page | 6


DBMS Laboratory 22MCAL27

----------------------------------------------------------

USN Name Address

-----------------------------------------------------------

4JN17MCA11 Jishi #9,kallalli

4JN17MCA37 Suraksha #11,vinobanagar

-------------------------------------------------------------

b) List the students who are not borrowed any books

Select Name from STUDENT S where NOT EXISTS (select * from BORROW B where
B.USN=S.USN);

----------

Name

----------

Samanyu

Namitha

------------------------------------------------------------------------------------------------------------------

c) Display the USN,student name,branch name,book name,author name,books borrowed


date of 2nd sem MCA students.

Select S.USN,Name,Branchname,Bookname,Authorname,Borrowdate from BRANCH B,


STUDENT S,BOOK BK,AUTHOR A,BORROW BR where S.Branchid=B.Branchid and
BR.USN=S.USN and BK.Bookid=BR.Bookid and BK.Authorid=A.Authorid and Sem=2 and
Branchname='MCA';
------------------------------------------------------------------------------------------------------------------

USN Name Branchname Bookname Authorname Borrowdate

-------------------------------------------------------------------------------------------------------------------

4JN17MCA11 Jishi MCA DBMS Elmasri Navathe 03-MAR-18

4JN17MCA11 Jishi MCA DMS Giri maldi 06-APR-18

4JN17MCA37 Suraksha MCA OOP K R Venugopal 01-SEP-18

JNN College of Engineering Page | 7


DBMS Laboratory 22MCAL27

4JN17MCA37 Suraksha MCA DBMS Elmasri Navathe 06-APR-18

4JN17MCA11 Jishi MCA C Dennis Richie 15-MAY-18

---------------------------------------------------------------------------------------------------------------------

d) Display the number of books written by each author.

select Authorname,Count(*) from AUTHOR A,BOOK BK where A.Authorid=BK.Authorid

GROUP BY Authorname;

-----------------------------------

Authorname Count(*)

-----------------------------------

BalguruSwami 1

Dennis Richie 1

Elmasri Navathe 1

Girimaldi 1

K R Venugopal 1

Sumitabadas 1

-------------------------------------

e) Display the students details who borrowed more than two books.

Select * from STUDENT S where(select Count(*) from BORROW BR where


BR.USN=S.USN)>2;

----------------------------------------------------------------------------------------

USN Name Address Branchid Sem

-----------------------------------------------------------------------------------------

4JN17MCA11 Jishi #9,kallalli B01 2

-----------------------------------------------------------------------------------------

JNN College of Engineering Page | 8


DBMS Laboratory 22MCAL27

f) Display the student details who borrowed books of more than one author.

Select * from STUDENT S where (select count(DISTINCT A.Authorid) from BOOK BK,
AUTHOR A,BORROW BR where BK.Authorid=A.Authorid and BR.Bookid=BK.Bookid and
BR.USN=S.USN)>1;

--------------------------------------------------------------------------------------

USN Name Address Branchid Sem

---------------------------------------------------------------------------------------

4JN17MCA11 Jishi #9,kallalli B01 2

4JN17MCA37 Suraksha #11,vinobanagar B01 2

----------------------------------------------------------------------------------------

g) Display the book names in descending order of their names.

Select Bookname from BOOK ORDER BY Bookname DESC;

--------------

Bookname

--------------

UNIX

OOP

JAVA

DMS

DBMS

C++

----------------

JNN College of Engineering Page | 9


DBMS Laboratory 22MCAL27

h) List the details of students who borrowed the books which are all published by the same
publisher.

Select *from STUDENT S where(select count(DISTINCT Publisher) from BOOK BK, BORROW
BR where BR.Bookid=BK.Bookid and BR.USN=S.USN)=1 and USN in(select USN from
BORROW GROUP BY USN HAVING COUNT(*)>1);

-------------------------------------------------------------------

USN Name Address Branchid Sem

--------------------------------------------------------------------

4JN17MCA11 Jishi #9,kallalli B01 2

---------------------------------------------------------------------

JNN College of Engineering Page | 10


DBMS Laboratory 22MCAL27

2) Consider the following schema:

STUDENT (USN, name, date_of_birth, branch, mark1(m1), mark2(m2), mark3(m3), total,


GPA)

Execute the following queries:

i) Update the column total by adding the columns m1, m2, m3.

ii) Find the GPA score of all the students.

iii) Find the students who born on a particular year of birth from the date_of_birth column.

iv) List the students who are studying in a particular branch of study.

v) Find the maximum GPA score of the student branch-wise.

vi) Find the students whose name starts with the alphabet “S”.

vii) Find the students whose name ends with the alphabets “AR”.

viii) Delete the student details whose USN is given as 1001.

STUDENT (USN, name, date_of_birth, branch, m1, m2, m3, total, GPA)

----------------------------------------------------------------------------------------------------

JNN College of Engineering Page | 11


DBMS Laboratory 22MCAL27

Student
USN NAME DOB BRANCH MARKS TOTAL GPA

----------------------------------------------------------------------------------------------------

TABLE:STUDENT

Create table STUDENT

(USN varchar(10)Primary key,

name varchar(20),

DOB date,

branch varchar(10),

m1 number(3),

m2 number(3),

m3 number(3),

total number(3),

GPA number(4,2);

Insert into STUDENT(USN,name,DOB,branch,m1,m2,m3)values('MCA001','Anand','21-may-


1980','MCA',90,95,98);

Insert into STUDENT(USN,name,DOB,branch,m1,m2,m3)values('MCA002','Vijay','28-oct-


1980','MCA',80,85,90);

Insert into STUDENT(USN,name,DOB,branch,marks1,marks2,marks3)values('MCA003','


Harish','02-Aug-1978','MBA',72,65,69);

Insert into STUDENT(USN,name,DOB,branch,m1,m2,m3)values('MCA004','Girish','14-mar-


1980','MBA',60,65,70);

Insert into STUDENT(USN,name,DOB,branch,m1,m2,m3)values('MCA005','Sachin','10-sep-


1980','MCA',70,80,88);

Insert into STUDENT(USN,name,DOB,branch,m1,m2,m3)values('MCA006','Kumar','28-may-


1980','MCA',55,58,62);

JNN College of Engineering Page | 12


DBMS Laboratory 22MCAL27

---------------------------------------------------------------------------------------------------------------------

USN name DOB branch m1 m2 m3

--------------------------------------------------------------------------------------------------------------------

MCA001 Anand 21-may-1980 MCA 90 95 98

MCA002 Vijay 28-oct-1980 MCA 80 85 90

MCA003 Harish 02-Aug-1978 MBA 72 65 69

MCA004 Girish 14-mar-1980 MBA 60 65 70

MCA005 Sachin 10-sep-1980 MCA 70 80 88

MCA006 Kumar 28-may-1980 MCA 55 58 62

--------------------------------------------------------------------------------------------------------------------

a) Update the column total by adding the columns mark1, mark2, mark3.

Update STUDENT set total=marks1+marks2+marks3;

6 Rows Updated

Select * from STUDENT;

---------------------------------------------------------------------------------------------------------------

USN NAME DOB BRANCH M1 M2 M3 TOTAL GPA

---------------------------------------------------------------------------------------------------------------

MCA001 Anand 21-MAY-80 MCA 90 95 98 283 -

MCA002 Vijay 28-OCT-80 MCA 80 85 90 255 -

MCA003 Harish 02-AUG-78 MBA 72 65 69 206 -

MCA004 Girish 14-MAR-80 MBA 60 65 70 195 -

MCA005 Sachin 10-SEP-80 MCA 70 80 88 238 -

MCA006 Kumar 28-MAY-80 MCA 55 58 62 175 -

---------------------------------------------------------------------------------------------------------------

JNN College of Engineering Page | 13


DBMS Laboratory 22MCAL27

b) Find the GPA score of all the students.

Select TRUNC(((marks1+marks2+marks3)/3)/9.5,2) GPA FROM STUDENT

----------------------------------------

GPA

----------------------------------------

9.92

8.94

7.22

6.84

8.35

6.14

----------------------------------------

UPDATE STUDENT SET GPA=TRUNC(((marks1+marks2+marks3)/3)/9.5,2)

6 row(s) updated.

Select * from STUDENT;

-----------------------------------------------------------------------------------------------------------

USN NAME DOB BRANCH M1 M2 M3 TOTALGPA

-----------------------------------------------------------------------------------------------------------

MCA001 Anand 21-MAY-80 MCA 90 95 98 283 9.92

MCA002 Vijay 28-OCT-80 MCA 80 85 90 255 8.94

MCA003 Harish 02-AUG-78 MBA 72 65 69 206 7.22

MCA004 Girish 14-MAR-80 MBA 60 65 70 195 6.84

MCA005 Sachin 10-SEP-80 MCA 70 80 88 238 8.35

MCA006 Kumar 28-MAY-80 MCA 55 58 62 175 6.14

-------------------------------------------------------------------------------------------------------------

JNN College of Engineering Page | 14


DBMS Laboratory 22MCAL27

c) Find the students who born on a particular year of birth from the date_of_birth column

Select * from Student where DOB like '_______78';

-------------------------------------------------------------------------------------------------------------------

USN NAME DOB BRANCH M1 M2 M3 TOTAL GPA

------------------------------------------------------------------------------------------------------------------

MCA003 Harish 02-AUG-78 MBA 72 65 69 206 7.22

------------------------------------------------------------------------------------------------------------------

d) List the students who are studying in a particular branch of study.

Select * from STUDENT Where BRACH='MCA';

-----------------------------------------------------------------------------------------------------------------

USN NAME DOB BRANCH M1 M2 M3 TOTAL GPA

------------------------------------------------------------------------------------------------------------------

MCA001 Anand 21-MAY-80 MCA 90 95 98 283 9.92

MCA002 Vijay 28-OCT-80 MCA 80 85 90 255 8.94

MCA005 Sachin 10-SEP-80 MCA 70 80 88 238 8.35

MCA006 Kumar 28-MAY-80 MCA 55 58 62 175 6.14

---------------------------------------------------------------------------------------------------------------------

e) Find the maximum GPA score of the student branch-wise.

Select gpa as MAX_GPA,name,branch from student where gpa in(select max(gpa) from student
group by branch);

JNN College of Engineering Page | 15


DBMS Laboratory 22MCAL27

------------------------------------------------

MAX_GPA NAME BRANCH

------------------------------------------------

9.92 Anand MCA

7.22 Harish MBA

-------------------------------------------------

f) Find the students whose name starts with the alphabet “S”.

Select * from STUDENT where name like 'S%';

-------------------------------------------------------------------------------------------------------------------

USN NAME DOB BRANCH M1 M2 M3 TOTAL GPA

-------------------------------------------------------------------------------------------------------------------
MCA005 Sachin 10-SEP-80 MCA 70 80 88 238 8.35

-------------------------------------------------------------------------------------------------------------------

e) Find the students whose name ends with the alphabets “AR”.

Select * from STUDENT where name like '%ar';

USN NAME DOB BRANCH M1 M2 M3 TOTAL GPA

---------------------------------------------------------------------------------------------------------------------

MCA006 Kumar 28-MAY-80 MCA 55 58 62 175 6.14

f) Delete the student details whose USN is given as MCA006.

Delete from STUDENT where usn='MCA006';

1 row(s) deleted.

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

JNN College of Engineering Page | 16


DBMS Laboratory 22MCAL27

3)Design an ER diagram for the following scenario, convert the same to a relational model
and then solve the following queries.

Consider a cricket tournament 'ABC CUP' organized by an organization. In the tournament


there are many teams are contesting each having a Teamid, Team-name, City, a coach. Each
team is uniquely identified by using Teamid. A team can have many players and a captain.
Each player is uniquely identified by Playerid, having a name, and multiple phone numbers,
age. A player represents only one team. There are many stdiums to conduct matches. Each
is identified using Stadiumid, having a Stadium name, Address.A team can play many
matches. Each match is identified uniquely by using Matchid. Each match won by any of the
team that also wants to record in the database. For each match man of the match award is
given to a player.

TEAM(Teamid, Teamname, City, Coach, Captain)

PLAYER(Playerid, Name, Age, Teamid)

STADIUM(Stadiumid,Stadiumname,Address)

MATCH(Matchid,Team1,Team2,Date,Time,Stadium)

RESULT(Matchid,Teamwon,Manofthematch)

PHONENO(Playerid,Phone)

---------------------------------------------------------------------------------------------------------------------

JNN College of Engineering Page | 17


DBMS Laboratory 22MCAL27

TABLE:TEAM

Create table TEAM

(Teamid varchar(10)Primary key,

Teamname varchar(10),

City varchar(10),

Coach varchar(10),

Captain varchar(10)references PLAYER);

Insert intoTEAM(Teamid,Teamname,City,Coach,Captain)values('T001','RCB','Banglore','
Dravid','P001');

JNN College of Engineering Page | 18


DBMS Laboratory 22MCAL27

----------------------------------------------------------------------------------

Teamid Teamname City Coach Captain

-----------------------------------------------------------------------------------

T001 RCB Banglore Dravid P001

T002 CSK Chennai Vettori P002

T003 KKR Kolkata Ravishastri P003

T004 DD Delhi Gavaskar P004

T005 MI Mumbai Jayavardane P005

-------------------------------------------------------------------------------------

TABLE:PLAYER

Create table PLAYER

(Playerid varchar(10)Primary key,

Name varchar(10),

Age number(2),

Teamid varchar(10)references TEAM);

Insert into PLAYER(Playerid,Name,Age,Teamid)values('P001','Kohli',34,'T001');

-------------------------------------------------------

Playerid Name Age Teamid

-------------------------------------------------------

P001 Kohli 34 T001

P002 Dhoni 36 T002

P003 Uthappa 32 T003

P004 Gambhir 33 T004

P005 R.Sharma 30 T005

JNN College of Engineering Page | 19


DBMS Laboratory 22MCAL27

P006 UV 37 T002

P007 Zaheer 38 T001

P008 Abhishek nair 28 T003

P009 Irfan pathan 31 T004

P010 Staurt binni 29 T005

------------------------------------------------------------

TABLE:STADIUM

Create table STADIUM

(Stadiumid varchar(10)Primary key,

Name varchar((10),

City varchar(10),

Area varchar(20),

PIN number(10));

Insert into STADIUM(Stadiumid,Name,City)values('S001','Firozshah','Delhi');

------------------------------------------------------------------------------------------

Stadiumid Name City Area PIN

------------------------------------------------------------------------------------------

S001 Firozshah Delhi lal chowk 509332

S002 Chinnaswami Banglore MG Road 503213

S003 Wankade Mumbai churchgate 537891

S004 Chidambaram Chennai chepauk 534123

S005 Eden garden Kolkata Ashoknagar 564731

-----------------------------------------------------------------------------------------------

JNN College of Engineering Page | 20


DBMS Laboratory 22MCAL27

TABLE:MATCH

Create table MATCH

(Matchid varchar(10)Primary key,

Team1 varchar(10)references TEAM,

Team2 varchar(10)references TEAM,

Date date,

Time varchar(10),

Stadium varchar(10)references STADIUM);

Insert into MATCH(Matchid,Team1,Team2,Date,Time,Stadium)values('M001','T001','T002','03-


APR-18','9:30','S001');

-------------------------------------------------------------------------------------------------------------

Matchid Team1 Team2 Date Time Stadium

--------------------------------------------------------------------------------------------------------------

M001 T001 T002 03-APR-18 9:30 S001

M002 T003 T004 05-APR-18 2:30 S002

M003 T004 T005 07-APR-18 2:30 S002

M004 T001 T003 08-APR-18 9:30 S003

M005 T002 T005 09-APR-18 9:30 S004

M006 T003 T005 11-APR-18 2:30 S005

M007 T001 T004 12-APR-18 9:30 S002

M008 T004 T002 14-APR-18 2:30 S002

---------------------------------------------------------------------------------------------------------------------

JNN College of Engineering Page | 21


DBMS Laboratory 22MCAL27

TABLE:RESULT

Create table RESULT

(Matchid varchar(10)references MATCH,

Teamwon varchar(10)references TEAM,

Manofthematch varchar(10)references PLAYER));

Insert into RESULT(Matchid, Teamwon, Manofthematch)values('M001','T001','P001');

-------------------------------------------------------

Matchid Teamwon Manofthematch

-------------------------------------------------------

M001 T001 P001

M002 T004 P009

M003 T004 P009

M004 T003 P008

M005 T005 P010

M006 T003 P003

M007 T004 P004

M008 T004 P009

-------------------------------------------------------

TABLE:PHONENO

Create table PONENO

(Playerid varchar(10)references PLAYER,

PHNO number(10));

JNN College of Engineering Page | 22


DBMS Laboratory 22MCAL27

Insert into PHONENO(Playerid,PHNO)values('P001',9876543210);

------------------------------

Playerid PHNO

------------------------------

P001 9876543210

P002 9874632112

P003 9876450912

P004 8756423134

P005 8097903845

P006 7659335623

P007 8089097303

P008 9808976098

P009 6080703980

P010 7098765431

------------------------------------

a) Display the youngest player name,team name,age in which they belongs of the tournament.

Select Name,Age,Teamname from PLAYER P,TEAM T where T.Teamid=P.Teamid and


Age=(select MIN(Age) from PLAYER);

----------------------------------------------------------

Name Age Teamname

---------------------------------------------------------

Abhishek nair 28 RCB

------------------------------------------------------------

b) Display the youngest player name, team name, age in which he belongs of tournament from
each team.

JNN College of Engineering Page | 23


DBMS Laboratory 22MCAL27

Select Name,Age,Teamname from PLAYER P,TEAM T where T.Teamid=P.Teamid and Age=


(select MIN(Age) from PLAYER where Teamid=T.Teamid);

-----------------------------------------

Name Age Teamname

-----------------------------------------

Zaheer 27 RCB

UV 29 CSK

Abhishek nair 33 KKR

Irfan pathan 29 DD

Staurt Binni 32 MI

-------------------------------------------

c)List the details of stadium where the maximum number of matches were played.

Select * from STADIUM where Stadiumid=(select Stadium from MATCH GROUP BY


STADIUM having count(*)=(select MAX(Count(*))from MATCH GROUP BY STADIUM));

------------------------------------------------------------------------------

Stadiumid Stadiumname City Area PIN

------------------------------------------------------------------------------

S002 Chinnaswamy Banglore Rajajinagar 643217

-------------------------------------------------------------------------------

d)List the details of the player who is not a captain but got the man of the match award atleast in
2 matches.

Select * from PLAYER where Playerid=(select Manofthematch from RESULT R,TEAM T


where R.Teamwon=T.Teamid and Captain<>Manofthematch GROUP BY Manofthematch
having COUNT(*)>2);

JNN College of Engineering Page | 24


DBMS Laboratory 22MCAL27

-------------------------------------------------------

Playerid Name Age Teamid

-------------------------------------------------------

P009 Irfan pathan 29 T009

-------------------------------------------------------

e) Display the team details who won maximum matches.

Select * from TEAM where Teamid in (select Teamwon from RESULT GROUP BY Teamwon
having count(*)=(select MAX(count(*))from Result GROUP BY Teamwon));

-------------------------------------------------------------------------

Teamid Teamname City Coach Captain

-------------------------------------------------------------------------

T004 DD Delhi Gavaskar P004

-------------------------------------------------------------------------

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

JNN College of Engineering Page | 25


DBMS Laboratory 22MCAL27

4.Design an ER diagram for the following senario Convert the same into a relational model,
normalize relations into a suitable Normal form and then solve the following queries A
country can have many Tourist places. Each Tourist place is identified by using
touriest_place_id having a name, belongs to a state, Number of kilometers away from the
capital city of that state, history. There are many tourists visits tourist places every year.
Each tourist is identified uniquely by using Tourist_id, having a Name, age, Country and
multiple email ids. A tourist visits many Tourist palces, it is also required to record the
visited_date in the database. A tourist can visit a Tourist place many times at different dates.
A Tourist place can be visited by many tourists either in the same date or at different dates.

JNN College of Engineering Page | 26


DBMS Laboratory 22MCAL27

Table:PLACE

create table PLACE

(PID varchar(10)Primary key,

Pname varchar(15),

State varchar(15),

Distance number(6),

History varchar(20));

Insert into PLACE (PID,Pname,State,Distance,History)values('P001','Jogfalls','Karnataka


',256,'Sharavthidam');

--------------------------------------------------------------------------------------------------------------

PID PNAME STATE DISTANCE HISTORY

--------------------------------------------------------------------------------------------------------------

P001 Jogfalls Karnataka 256 Sharavathi dam

P002 Palace Karnataka 435 Historic

P003 Golgumbuz Karnataka 572 Echo

P004 Thar Desert Rajasthan 315 Natures Wonder

P005 Marina Beach Tamilnadu 296 Natures Wonder

P006 Tirupathi A.P 446 Devine

P007 Munar Kerala 606 Back Water

P008 Ajantha caves Maharashtra 796 Echo

P009 Tajmahal Delhi 896 Shahjahan

P010 Red Fort Delhi 999 Mughal

--------------------------------------------------------------------------------------------------------------

JNN College of Engineering Page | 27


DBMS Laboratory 22MCAL27

Table:TOURIST

create table TOURIST

(TID varchar(10)Primary key,

Tname varchar(15),

Age number(2),

Country varchar(20));

Insert into TOURIST(TID,Tname,Age,Country)values('T001','OH Hane',35,'South korea')

------------------------------------------------------------------------------------------------------------

TID TNAME AGE COUNTRY

------------------------------------------------------------------------------------------------------------

T001 OH Hane 35 South korea

T002 Back Seng JO 24 South korea

T003 Bung Jung Go 31 China

T004 Steve 45 Australia

T005 Tanushree 22 India

----------------------------------------------------------------------------------------------------------

Table:VISIT

create table VISIT

(TID varchr(10)references TOURIST,

PID varchar(10) references PLACE,

Date1 date,

primary key(tid,pid));

JNN College of Engineering Page | 28


DBMS Laboratory 22MCAL27

----------------------------------------------------------------------------------------

TID PID Date

----------------------------------------------------------------------------------------

T001 P001 23JAN18

T002 P009 25JAN18

T003 P009 23JAN18

T004 P009 03FEB18

T005 P009 02FEB18

T003 P004 21FEB18

T005 P001 29JAN18

T005 P004 26JAN18

T005 P005 09FEB18

T005 P006 09FEB18

T005 P007 08FEB18

T005 P008 16FEB18

T005 P010 11MAR18

T001 P002 18FEB18

T001 P003 07JUL18

-----------------------------------------------------------------------------------------------

Table: EMAIL

create table EMAIL

(TID varchar(10)references TOURIST,

EMAIL varchar(20));

Insert into EMAIL(TID,EMAIL)values('T001','[email protected]');

JNN College of Engineering Page | 29


DBMS Laboratory 22MCAL27

----------------------------------------

TID EMAIL

----------------------------------------

T001 [email protected]

T002 [email protected]

T003 [email protected]

T004 [email protected]

T005 [email protected]

-----------------------------------------

a) List the state name which is having maximum numbers of touries places

Select state from PLACE group by State having count(*)=(select MAX(count(*))from Place
GROUP BY state);

---------------

STATE

---------------

Karnataka

---------------

b) List details of Tourist places where maximum number of tourists visited

Select * from place where pid in(select pid from VISIT GROUP BY PID having count(*)
=(select MAX(count(*))from visit group by pid))

-------------------------------------------------------------------------------------------------------

PID PNAME STATE DISTANCE HISTORY

-------------------------------------------------------------------------------------------------------

P009 Tajmahal Delhi 567 ShaJahan

-------------------------------------------------------------------------------------------------------

JNN College of Engineering Page | 30


DBMS Laboratory 22MCAL27

c) List the details of tourists visited all tourist places of the state 'Karnataka'

Select * from TOURIST where TID in(select TID from VISIT V,PLACE P where V.PID=P.PID
and state='Karnataka' GROUP BY V.TID having count(distinct pname)=(select count(*) from
place where state='Karnataka'));

--------------------------------------------------------------------------

TID TNAME AGE Country

--------------------------------------------------------------------------

T002 BaekSngJo 24 South korea

--------------------------------------------------------------------------

d) Display the details of touriests visited atlist one tourist place of state,but visited all states
tourist places

Select * from tourist t where (select count(distinct state) from visit v, place p where v.pid=p.pid
and t.tid=v.tid)=(select count(distinct state) from place)

--------------------------------------------------------------------------------

TID Iname Age Country

--------------------------------------------------------------------------------

T005 Tanushree 22 India

--------------------------------------------------------------------------------

e) Display the details of the tourist place visited by the tourist off all country

Select * from PLACE P where(select count(distinct COUNTRY)from VISIT V,TOURIST T


where V.PID=P.PID and T.TID=V.TID)=(select count(distnct COUNTRY)from TOURIST)

------------------------------------------------------------------------------------

PID Pname State Distance History

------------------------------------------------------------------------------------

P009 Tajmahal Delhi 567 Shahajahan

------------------------------------------------------------------------------------

JNN College of Engineering Page | 31


DBMS Laboratory 22MCAL27

5.Design an ER diagram for the following scenario, Convert the same into a relational model,
normalize relations into suitable normal from and then solve the queries:

A country wants to conduct an election for parliment. A country having many constituencies.
Each constituency is uniquely identified by Constituency id, having the Name, belongs to a
state, Number_of_voters. A constituency can have many voters. Each voter is uniquely
identified by using Voter_id, having the Name, age, address. Each voter belongs to only one
constituency. There are many candidates contesting in the election. Each candidates are
uniquely identified by using candidate_id, having Name, phone_no, age, state. A candidate
belongs to only one party. There are many parties. Each party is uniquely identified by using
Party_id, having Party_name, Party_symbol. A condidate can contest from many
contituencies under a same party. A party can have many condidates contesting from
different constituencies. No constituency having the candidates from the same party. A
constituency can have many contesting candidates belongs to different parties. Each voter
votes only one candidate of his/her constituency.

JNN College of Engineering Page | 32


DBMS Laboratory 22MCAL27

Table: Constituency

create table Constituency(CID varchar(10)primary key,

CNAME varchar(10),

State varchar(10),

NO_OF_VOTERS number(10));

Insert into Constituency(CID,CNAME,State,NO_OF_VOTERS)values('C001','banglore','


KAR',2);

Insert into Constituency(CID,CNAME,State,NO_OF_VOTERS)values('C002','chennai',' TN',1);

Insert into Constituency(CID,CNAME,State,NO_OF_VOTERS) values('C003','Hyderabad','


AP',1);

Insert into Constituency(CID,CNAME,State,NO_OF_VOTERS)values('C004','Kochi','Kerala


',1);

JNN College of Engineering Page | 33


DBMS Laboratory 22MCAL27

Insert into Constituency(CID,CNAME,State,NO_OF_VOTERS)values('C005','Agra',' UP',1);

Insert into Constituency(CID,CNAME,State,NO_OF_VOTERS) values('C006','Shimoga','


KAR',1);

-----------------------------------------------------------------------------------------

CID CNAME State NO_OF_VOTERS

-----------------------------------------------------------------------------------------

C001 Banglore KAR 2

C002 Chennai TN 1

C003 Hydrabad AP 1

C004 Kochi Kerala 1

C005 Agra UP 1

C006 Shimoga KAR 1

------------------------------------------------------------------------------------------

Table: Voter

create table Voter(VID varchar(10)primary key,

Vname varchar(10),

Age number(2),

HNO number(5),

City varchar(10),

State varchar(10),

Pincode number(10),

CID varchar(10)references Constituency);

Insert into Voter(VID,Vname,Age,HNO,City,State,Pincode,CID)values('V001','Pragathi',24,35


1,'BANGALORE','KAR',677204,'C001');

JNN College of Engineering Page | 34


DBMS Laboratory 22MCAL27

Insert into Voter(VID,Vname,Age,HNO,City,State,Pincode,CID)values('V002','Anish',31,


492,'Kochi','Kerala',732519,'C004');

Insert into Voter(VID,Vname,Age,HNO,City,State,Pincode,CID)values('V003','Unnathi',19,


104,'BANGALORE','KAR',678002,'C001');

Insert into Voter(VID,Vname,Age,HNO,City,State,Pincode,CID)values('V004','Pranjal',42,


55,'Chennai','TN',549326,'C002');

Insert into Voter(VID,Vname,Age,HNO,City,State,Pincode,CID)values('V005','Rishab',18,


61,'Agra','UP',457218,'C005');

Insert into Voter(VID,Vname,Age,HNO,City,State,Pincode,CID)values('V006','Sanmathi',48,


701,'Shimoga','KAR',577201,'C006');

Insert into Voter(VID,Vname,Age,HNO,City,State,Pincode,CID)values('V007','Stalin',27,


309,'Hyderabad','AP',352719,'C003');

---------------------------------------------------------------------------------------------------------------------

VID Vname Age HNO City State Pincode CID

---------------------------------------------------------------------------------------------------------------------

V001 Pragathi 24 351 BANGALORE KAR 677201 C001

V002 Anish 31 492 Kochi Kerala 732519 C004

V003 Unnathi 19 104 BANGALORE KAR 678002 C001

V004 Pranjal 42 55 Chennai TN 549326 C002

V005 Rishab 18 61 Agra UP 457218 C005

V006 Sanmathi 48 701 Shimoga KAR 577201 C006

V007 Stalin 27 309 Hydrabad AP 352719 C003

---------------------------------------------------------------------------------------------------------------------

Table:Party

create table Party(PID varchar(10)primary key,

Pname varchar(10),

symbol varchar (10));

JNN College of Engineering Page | 35


DBMS Laboratory 22MCAL27

Insert into Party(PID,Pname,symbole)values('P001','BJP','Lotus');

Insert into Party(PID,Pname,symbole)values('P002','CONGRESS','Hand');

Insert into Party(PID,Pname,symbole)values('P003','JDS','Grass');

Insert into Party(PID,Pname,symbole)values('P004','AIADMK','Cycle');

-------------------------------------------------------------------------

PID Pname symbole

-------------------------------------------------------------------------

P001 BJP Lotus

P002 CONGRESS Hand

P003 JDS Grass

P004 AIADMK Cycle

-----------------------------------------------------------------------

Table:Candidate

create table Candidate(Candid varchar(10)primary key,

Candname varchar(10),

Age number(2),

Phone number(10),

State varchar(10),

PID varchar(10)references Party);

Insert into Candidate(Candid,Candname,Age,Phone,State,PID)values('CD001','Kumar',59,


8011425965,'TN','P004');

Insert into Candidate(Candid,Candname,Age,Phone,State,PID)values('CD002','Eswar',62,


2222222222,'KAR','P001');

Insert into Candidate(Candid,Candname,Age,Phone,State,PID)values('CD003','ROHINI',36,


3333333333,'KAR','P001');

JNN College of Engineering Page | 36


DBMS Laboratory 22MCAL27

Insert into Candidate(Candid,Candname,Age,Phone,State,PID)values('CD004','VINAY',47,


4444444444,'UP','P002');

Insert into Candidate(Candid,Candname,Age,Phone,State,PID)values('CD005','PRANATHI',51,


5555555555,'Kerala','P003');

-------------------------------------------------------------------------------------------------------------------

CDNDID CDNDNAME AGE PHONE STATE PID

-------------------------------------------------------------------------------------------------------------------

CD001 KUMAR 59 8011425965 TN P004

CD002 ESHWAR 62 9080978976 KAR P001

CD003 ROHINI 36 6789098765 KAR P001

CD004 VINAY 47 8978909754 UP P002

CD005 PRANATHI 51 9093412345 Kerala P003

------------------------------------------------------------------------------------------------------------------

Table:Contest

create table Contest(CANDID varchar(10)references Candidate,

CID varchar(10)references Constituency);

Insert into Contest(CANDID,CID)values('CD002','C001');

Insert into Contest(CANDID,CID)values('CD002','C006');

Insert into Contest(CANDID,CID)values('CD002','C002');

Insert into Contest(CANDID,CID)values('CD003','C006');

Insert into Contest(CANDID,CID)values('CD005','C004');

JNN College of Engineering Page | 37


DBMS Laboratory 22MCAL27

---------------------------------------

CANDID CID

--------------------------------------

CD002 C001

CD002 C006

CD002 C002

CD003 C006

CD005 C004

-----------------------------------------

a) List the details of candidates who are Consisting from more than one constituencies which
belongs to different state

Select * from Candidate where CANDID in (select CANDID from Contest Ct,Constituency Co
WHERE Co.CID=Ct.CID GROUP BY CANDID having count(distinct State)>1);

------------------------------------------------------------------------------------------------------------------

CANDID CDNDNAME AGE PHONE STATE PID

-------------------------------------------------------------------------------------------------------------------

CD002 ESHWAR 62 9080978976 KAR P001

-------------------------------------------------------------------------------------------------------------------

b) Display the state name having maximum number of Constituencies.

Select State from Constituency GROUP BY State having count(*)=(select MAX(count(*)) from
Constituency GROUP BY State)

-----------

STATE

-----------

KAR

-----------

JNN College of Engineering Page | 38


DBMS Laboratory 22MCAL27

c) Crate a stored produre to insert the tuple into the voter table by checking the voter age if voter
age is atleast 18 years old,then insert the tuple into the voter else display the 'Not an eligible
voter msg'.

Create or replace procedure checkage(VID in varchar,

vname in varchar,

Age in number,

hno in number,

city in varchar,

State in varchar,

pincode in number,

CID in varchar)is

begin

If Age < 18 then

dbms_output.put_line('Age is less than 18 Record can not be inserted');

else

Insert into voter values(VID,Vname,Age,HNO,city,State,Pincode,CID);

commit;

dbms_output.put_line('Record inserted Successfully');

end if;

end;

Execution:

Set Serveroutput on

call checkage('V008','Anu',17,309,'Hyderabad','AP',352719,'C003');

JNN College of Engineering Page | 39


DBMS Laboratory 22MCAL27

call checkage('V009','Aarushi',19,311,'Shimoga','Karnataka',577201,'C006');

Age is less than 18.Record can't be inserted

call completed

Record inserted Successfully

call completed

After insert

---------------------------------------------------------------------------------------------------------------------

VID Vname Age HNO City State Pincode CID

---------------------------------------------------------------------------------------------------------------------

V009 Aarushi 19 311 Shimoga KAR 577201 C006

---------------------------------------------------------------------------------------------------------------------

d) Create a stored procedure to dispaly the number of voters in the specified Constituency.where
the constituency is passed as an argument to the stored procedure

Create or replace procedure votercount(cons in varchar)as cnt number(10);

Begin

select no_of_voters into cnt from constituency where Cname=Cons;

dbms_output.put_line(' no_of_voters in '||cons||' constituency ' || cnt);

end;

JNN College of Engineering Page | 40


DBMS Laboratory 22MCAL27

Execution:

Set serveroutput on

call votercount('banglore')

output:

procedure Created

no_of_voters in Banglore Constituency 2

call completed

e) Create a Trigger to update the count of 'number_of_voter' of the rexpective constituency in


'CONSTITUENCY' tables,After inserting a tuple into the voter tabe

Create or replace TRIGGER updatevotercnt

After insert on voter

for each row

Begin

update Constituency

Set no_of_voters=no_of_voters+1 where

CID=:NEW.CID;

end;

Insert into voter values('V009','Manya',20,555,'Badravathi','karnataka',5776328,'C006');

JNN College of Engineering Page | 41


DBMS Laboratory 22MCAL27

-------------------------------------------------------------------------------------

CID CNAME State NO_OF_VOTERS

----------------------------------------------------------------------------------

C006 Shimoga KAR 1

---------------------------------------------------------------------------------

After insert

---------------------------------------------------------------------------------

CID CNAME State NO_OF_VOTERS

----------------------------------------------------------------------------------

C006 Shimoga KAR 2

-----------------------------------------------------------------------------------

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

JNN College of Engineering Page | 42

You might also like