Dbms Lab Manual Harsha
Dbms Lab Manual Harsha
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)
i. List the details of Students who are all studying in 2nd sem MCA.
v.Display the student details who borrowed more than two books.
vi.Display the student details who borrowed books of more than one Author.
viii.List the details of students who borrowed the books which are all published by the
same publisher.
TABLE: BRANCH
Branchname varchar(10),
HOD varchar(10));
-----------------------------------------
-----------------------------------------
B04 BE PQR
------------------------------------------
TABLE: STUDENT
Name varchar(10),
Address varchar(50),
Sem number(1));
-------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
TABLE: BOOK
Bookname varchar(10),
Publisher varchar(10);
-------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
TABLE: AUTHOR
Authorname varchar(20),
Country varchar(10),
Age number(2));
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
TABLE:BORROW
Borrowdate date);
-------------------------------------------------------------
-------------------------------------------------------------
----------------------------------------------------------------
a) List the details of students who are all studying in 2nd sem MCA
----------------------------------------------------------
-----------------------------------------------------------
-------------------------------------------------------------
Select Name from STUDENT S where NOT EXISTS (select * from BORROW B where
B.USN=S.USN);
----------
Name
----------
Samanyu
Namitha
------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
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.
----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
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;
--------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
--------------
Bookname
--------------
UNIX
OOP
JAVA
DMS
DBMS
C++
----------------
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);
-------------------------------------------------------------------
--------------------------------------------------------------------
---------------------------------------------------------------------
i) Update the column total by adding the columns m1, m2, m3.
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.
vi) Find the students whose name starts with the alphabet “S”.
vii) Find the students whose name ends with the alphabets “AR”.
STUDENT (USN, name, date_of_birth, branch, m1, m2, m3, total, GPA)
----------------------------------------------------------------------------------------------------
Student
USN NAME DOB BRANCH MARKS TOTAL GPA
----------------------------------------------------------------------------------------------------
TABLE:STUDENT
name varchar(20),
DOB date,
branch varchar(10),
m1 number(3),
m2 number(3),
m3 number(3),
total number(3),
GPA number(4,2);
---------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
a) Update the column total by adding the columns mark1, mark2, mark3.
6 Rows Updated
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
----------------------------------------
GPA
----------------------------------------
9.92
8.94
7.22
6.84
8.35
6.14
----------------------------------------
6 row(s) updated.
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
c) Find the students who born on a particular year of birth from the date_of_birth column
-------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
Select gpa as MAX_GPA,name,branch from student where gpa in(select max(gpa) from student
group by branch);
------------------------------------------------
------------------------------------------------
-------------------------------------------------
f) Find the students whose name starts with the alphabet “S”.
-------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
MCA005 Sachin 10-SEP-80 MCA 70 80 88 238 8.35
-------------------------------------------------------------------------------------------------------------------
e) Find the students whose name ends with the alphabets “AR”.
---------------------------------------------------------------------------------------------------------------------
1 row(s) deleted.
******************************************************************************
3)Design an ER diagram for the following scenario, convert the same to a relational model
and then solve the following queries.
STADIUM(Stadiumid,Stadiumname,Address)
MATCH(Matchid,Team1,Team2,Date,Time,Stadium)
RESULT(Matchid,Teamwon,Manofthematch)
PHONENO(Playerid,Phone)
---------------------------------------------------------------------------------------------------------------------
TABLE:TEAM
Teamname varchar(10),
City varchar(10),
Coach varchar(10),
Insert intoTEAM(Teamid,Teamname,City,Coach,Captain)values('T001','RCB','Banglore','
Dravid','P001');
----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
TABLE:PLAYER
Name varchar(10),
Age number(2),
-------------------------------------------------------
-------------------------------------------------------
P006 UV 37 T002
------------------------------------------------------------
TABLE:STADIUM
Name varchar((10),
City varchar(10),
Area varchar(20),
PIN number(10));
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
TABLE:MATCH
Date date,
Time varchar(10),
-------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
TABLE:RESULT
-------------------------------------------------------
-------------------------------------------------------
-------------------------------------------------------
TABLE:PHONENO
PHNO number(10));
------------------------------
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.
----------------------------------------------------------
---------------------------------------------------------
------------------------------------------------------------
b) Display the youngest player name, team name, age in which he belongs of tournament from
each team.
-----------------------------------------
-----------------------------------------
Zaheer 27 RCB
UV 29 CSK
Irfan pathan 29 DD
Staurt Binni 32 MI
-------------------------------------------
c)List the details of stadium where the maximum number of matches were played.
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-------------------------------------------------------------------------------
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 TEAM where Teamid in (select Teamwon from RESULT GROUP BY Teamwon
having count(*)=(select MAX(count(*))from Result GROUP BY Teamwon));
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-------------------------------------------------------------------------
******************************************************************************
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.
Table:PLACE
Pname varchar(15),
State varchar(15),
Distance number(6),
History varchar(20));
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
Table:TOURIST
Tname varchar(15),
Age number(2),
Country varchar(20));
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
Table:VISIT
Date1 date,
primary key(tid,pid));
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
Table: EMAIL
EMAIL varchar(20));
----------------------------------------
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
---------------
Select * from place where pid in(select pid from VISIT GROUP BY PID having count(*)
=(select MAX(count(*))from visit group by pid))
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
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'));
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
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)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
e) Display the details of the tourist place visited by the tourist off all country
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
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.
Table: Constituency
CNAME varchar(10),
State varchar(10),
NO_OF_VOTERS number(10));
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
C002 Chennai TN 1
C003 Hydrabad AP 1
C005 Agra UP 1
------------------------------------------------------------------------------------------
Table: Voter
Vname varchar(10),
Age number(2),
HNO number(5),
City varchar(10),
State varchar(10),
Pincode number(10),
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
Table:Party
Pname varchar(10),
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-----------------------------------------------------------------------
Table:Candidate
Candname varchar(10),
Age number(2),
Phone number(10),
State varchar(10),
-------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
Table:Contest
---------------------------------------
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);
------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
Select State from Constituency GROUP BY State having count(*)=(select MAX(count(*)) from
Constituency GROUP BY State)
-----------
STATE
-----------
KAR
-----------
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'.
vname in varchar,
Age in number,
hno in number,
city in varchar,
State in varchar,
pincode in number,
CID in varchar)is
begin
else
commit;
end if;
end;
Execution:
Set Serveroutput on
call checkage('V008','Anu',17,309,'Hyderabad','AP',352719,'C003');
call checkage('V009','Aarushi',19,311,'Shimoga','Karnataka',577201,'C006');
call completed
call completed
After insert
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
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
Begin
end;
Execution:
Set serveroutput on
call votercount('banglore')
output:
procedure Created
call completed
Begin
update Constituency
CID=:NEW.CID;
end;
-------------------------------------------------------------------------------------
----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
After insert
---------------------------------------------------------------------------------
----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
*****************************************************************************