80% found this document useful (10 votes)
13K views

MCA Dbms Lab Manual Full

This document contains a laboratory manual for the Database Laboratory course at Visvesvaraya Technological University. It provides instructions for students to create database tables to model entities in a cricket tournament, including teams, players, stadiums, and matches. It also lists 8 queries for students to solve on the created database tables. The tables are to be created based on an ER diagram designed to model the given scenario.

Uploaded by

nageshmcackm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
80% found this document useful (10 votes)
13K views

MCA Dbms Lab Manual Full

This document contains a laboratory manual for the Database Laboratory course at Visvesvaraya Technological University. It provides instructions for students to create database tables to model entities in a cricket tournament, including teams, players, stadiums, and matches. It also lists 8 queries for students to solve on the created database tables. The tables are to be created based on an ER diagram designed to model the given scenario.

Uploaded by

nageshmcackm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

Belgaum, Karnataka

DATABASE LABORATORY
LABORATORY MANUAL
16MCA28
II SEMESTER - 2017

Prepared By:

Nagesh B S
Asst. Prof.
Dept. of MCA
RNSIT
Phone:9844032238

Department of MCA

ESTD: 2001
An Institution with a difference

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


Program 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,Borrowed_Date)
Queries:
1 List the details of Students who are all Studying in 2nd sem MCA.
2 List the students who are not borrowed any books.
3 Display the USN, Student name, Branch_name, Book_name, Author_name ,
Books_Borrowed_Date of 2nd sem MCA Students who borrowed books.
4 Display the number of books written by each Author.
5 Display the student details who borrowed more than two books.
6 Display the student details who borrowed books of more than one Author.
7 Display the Book names in descending order of their names.
8 List the details of students who borrowed the books which are all published by
the same Publisher.

BRANCH
Branchid Branchname HOD

STUDENT
USN Name Address Branchid sem

BOOK
Bookid Bookname Authorid Publisher Branchid

AUTHOR
Authorid Authorname Country age

BORROW
USN Bookid Borrowed_Date

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


create table branch
(branchid int primary key,
bname varchar(10),
hod varchar(10));

create table student


(usn varchar(10) primary key,
name varchar(10),
addr varchar(15),
branchid int references branch(branchid),
sem int);

create table book


(bookid int primary key,
bname varchar(10),
author

create table author


(authorid int primary key,
aname varchar(10),
country varchar(10),
age int);

create table book


(bookid int primary key,
bname varchar(10),
authorid int references author(authorid),
publisher varchar(10),
branchid int references branch(branchid));

create table borrow


(usn varchar(10) references student(usn),
bookid int references book(bookid),
borrowdate date);

SQL> select * from branch;

BRANCHID BNAME HOD


---------- ---------- ----------
1 mca npk
2 mba bojanna
3 cse gtr
4 ise sudhamani
5 electrical sumathi

SQL> select * from student;


USN NAME ADDR BRANCHID SEM
---------- ---------- --------------- ---------- ----------
1rn1 harish bangalore 1 2
1rn2 bharath mysore 2 3
1rn3 kiran delhi 3 6
1rn4 mahi chennai 4 7
1rn5 krishna hubli 5 4

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


SQL> select * from book;

BOOKID BNAME AUTHORID PUBLISHER BRANCHID


---------- ---------- ---------- ---------- ----------
1111 c prog 123 pearson 1
2222 dbms 124 mgrawhill 2
3333 oops 125 sapna 3
4444 unix 126 subhash 4
5555 cprog 127 pearson 5

SQL> select * from author;

AUTHORID ANAME COUNTRY AGE


---------- ---------- ---------- ----------
123 navathe india 55
124 ritche uk 44
125 RAMKRISHNA india 55
126 sumitabha india 38
127 dennis usa 66

SQL> select * from borrow;

USN BOOKID BORROWDAT


---------- ---------- ---------
1rn1 2222 10-JAN-00
1rn1 3333 05-MAR-16
1rn3 5555 01-JUN-10
1rn5 2222 19-MAY-00
1rn2 1111 22-FEB-15

Query 1.
select * from student where sem=2 and branchid in
(select branchid from branch where bname='mca')

USN NAME ADDR BRANCHID SEM


---------- ---------- --------------- ---------- ----------
1rn1 harish bangalore 1 2

Query 2.
select * from student where usn not in (select usn from borrow);

USN NAME ADDR BRANCHID SEM


---------- ---------- --------------- ---------- ----------
1rn4 mahi chennai 4 7

Query 3.
select student.usn ,student.name,branch.bname, book.bname, aname,
borrowdate from student , branch, book, author, borrow where
student.usn=borrow.usn and borrow.bookid=book.bookid and
book.authorid =author.authorid and student.branchid=branch.branchid
and student.sem=2 and branch.bname='mca';

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


USN NAME BNAME BNAME ANAME BORROWDAT
---------- ---------- ---------- ---------- ---------- ---------
1rn1 harish mca dbms ritche 10-JAN-00
1rn1 harish mca oops RAMKRISHNA 05-MAR-16

Query 4.
select count(*) , authorid from book group by authorid;

COUNT(*) AUTHORID
---------- ----------
1 123
1 125
1 124
1 126
1 127

Query 5.
select * from student where usn in ( select usn from borrow group
by usn having count(usn) >=2);

USN NAME ADDR BRANCHID SEM


---------- ---------- --------------- ---------- ----------
1rn1 harish bangalore 1 2

Query 6.
select * from student s where exists (select br.usn from borrow br
join book bk on br.bookid=bk.bookid where br.usn=s.usn group by usn
having count(distinct authorid)>1);

USN NAME ADDR BRANCHID SEM


---------- ---------- --------------- ---------- ----------
1rn1 harish bangalore 1 2

Query 7.
select bname from book order by bname desc;

BNAME
----------
unix
oops
dbms
cprog
c prog

Query 8.
select * from student s where exists (select usn , publisher from
borrow join book on borrow.bookid=book.bookid where s.usn=borrow.usn
group by usn having count(distinct publisher)=1);

USN NAME ADDR BRANCHID SEM


---------- ---------- --------------- ---------- ----------
1rn2 bharath mysore 2 3
1rn3 kiran delhi 3 6
1rn5 krishna hubli 5 4

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


Program 2
Design an ER-diagram for the following scenario, Convert the same into 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 Stadiums to conduct matches. Each stadium is
identified using Stadiumid, having a stadium_name,Address ( involves
city,area_name,pincode).A team can play many matches. Each match played between the two
teams in the scheduled date and time in the predefined Stadium. Each match is identified
uniquely by using Matchid. Each match won by any of the one team that also wants to record in
the database. For each match man_of_the match award given to a player.
Queries:
1 Display the youngest player (in terms of age) Name, Team name , age in which he belongs of
the tournament.
2 List the details of the stadium where the maximum number of matches were played.
3 List the details of the player who is not a captain but got the man_of _match award at least in
two matches.
4 Display the Team details who won the maximum matches.
5 Display the team name where all its won matches played in the same stadium.

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


create table team
( tid int primary key,
tname varchar(20),
coach varchar(20),
captain_pid int,
city varchar(20));

create table player


( pid int primary key,
pname varchar(2),
age int,
tid int references team(tid))

create table stadium


(sid int primary key,
sname varchar(20),
picode number(8),
city varchar(20),
area varchar(20));

create table match


(mid int primary key,
mdate date,
time varchar(6),
sid int references stadium(sid),
team1_id int references team(tid),
team2_id int references team(tid),
winning_team_id int references team(tid),

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


man_of_match int references player(pid))

create table player_phone


( pid int references player(pid),
phone int ,
primary key(pid,phone));

SQL> select * from team;


TID TNAME COACH CAPTAIN_PID CITY
---------- -------------------- -------------------- ----------- ------------------
123 rcb sunil 1 bangalore
124 csk laxman 3 chennai
125 royals singh 4 rajasthan
126 daredevils sehwag 2 delhi

SQL> select * from player;

PID PNAME AGE TID


---------- -------------------- ---------- ----------
1 sachin 33 123
2 dravid 32 124
3 dhoni 30 124
4 raina 30 125
5 kohli 23 126

SQL> select * from stadium;


SID SNAME PICODE CITY AREA
---------- -------------------- ---------- -------------------- -----------
111 chinnaswamy 56001 bangalore mg road
222 kotla 460009 delhi highway
333 international 38883 chennai tr nagar
444 ksca 560098 bangalore peenya
555 csca 567772 cochin beach road

SQL> select * from match;


MID MDATE TIME SID TEAM1_ID TEAM2_ID WINNING_TEAM_ID MAN_OF_MATCH
---------- --------- ------ ---------- ---------- ---------- --------------- ------------
1 10-JAN-17 10am 111 123 124 123 1
102 11-JAN-17 pm 222 124 126 126 5
103 12-JAN-17 11am 111 125 126 126 5
104 17-JAN-17 12pm 111 125 123 123 1

SQL> select * from player_phone;


PID PHONE
---------- ----------
1 998882928
2 877563733
2 988928822
3 877366383

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


Query 1 :
Select pname, tname, age from player p, team t where
p.tid=t.tid and age =(select min(age) from player);
PNAME TNAME AGE
-------------------- -------------------- ----------
kohli daredevils 23

Query 2:
select * from stadium where sid in
(select sid from match group by sid having count(sid) =
(select max(count(sid)) from match group by sid))

SID SNAME PICODE CITY AREA


---------- -------------------- ---------- -------------------- ------------
111 chinnaswamy 56001 bangalore mg road

Query 3:
select * from player where pid not in ( select captain_pid from
team) and pid in (select man_of_match from match group by
man_of_match having count(man_of_match)=2);
PID PNAME AGE TID
---------- -------------------- ---------- ----------
5 kohli 23 126

Query 4:
select * from team where tid in (select winning_team_id from
match group by winning_team_id having count(winning_team_id)=
(select max(count(winning_team_id))from match group by
winning_team_id))

TID TNAME COACH CAPTAIN_PID CITY


---------- -------------------- ------------------- s----------- ----
126 daredevils sehwag 2 delhi

Query 5
select tname from team where tid in (
select winning_team_id from match group
by(winning_team_id,sid)
having count(*) in (select count(winning_team_id)
from match group by winning_team_id))

TNAME
-------
rcb

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


Program 3
Consider the following Scenario and design an ER-Diagram, map the designed ER-diagram
into a Relational model. Consider an organization “ABC” having many employees. An employee
works for one department. Each employee identified by using Empid, having Name, address (
described as House_no, city, district, state, pin code) and more than one phone numbers. Department
identified by using Dno, having Dname, Dlocation. Each Department having a manager . Each
department having many employees. There are many Projects , each project is controlled by the
department. Each Project uniquely identified by Pno, having Project_name, Project_location. An
employee works on many Projects. Number of hours per week worked on each project by an
Employee also needs to be recorded in the database . A project is worked by many employees. Each
employee supervised by the supervisor. Employee having many dependents. Dependents having the
dependent_name, gender, age, address. Dependents are identified by Empid.
T1(Empid, Emp_Name,city, district, state, pin_code, phoneno, Dno,Dname,Dlocation,
Dept_mgr_id, Pno, Project_name, Project_location, Number_of_Hours,Supervisor_Empid,
Dependent_name, gender, address) ,Deduce the above Relation T1 into the 3NF and then solve the
following queries.
Queries:
1. Display the details of the employees who are working on both the projects having project_no 5 and
10.
2. Display the details of employees having atleast two dependents.
3. Display the project name on which more number of employees are working.
4. Retrieve the employees who do not have any dependents.
5. Display the Employee details whose total number of hours per week working on various projects is
maximum than all other employees.
6. create a view to display the number of employees working in each department.

dno dlocation

dname
SUPERVISES DEPARTMENT

1 N
ename 1 WORKSFOR
M
1
eid EMPLOYEE
1
1
1 MANAGES
address
CONTROL
1
HAS

name
N WORKSON 1
M
N
DEPENDENT N
gender PROJECT
plocation
age address
No_of_hours pno
pname
Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238
SQL> create table employee(eid int primary key,
ename varchar(10),
address varchar(10),
supeid int,
dno int);

SQL> alter table employeee add constraint fk_supeid foreign


key(supeid) references employeee(eid));

SQL> create table department(dno int primary key,


dname varchar(20),
dlocation varchar(10),
mgrid int references employeee(eid));

SQL> alter table employeee add constraint fk_dno foreign key(dno)


references department(dno));

SQL> create table project(pno int primary key,


pname varchar(20),
plocation varchar(20),
dno int references department(dno));

SQL> create table dependent(name varchar(20),


gender varchar(6),
age int,
addr varchar(20),
eid int references employeee(eid),
primary key(name,eid));

SQL> create table empproj(eid int references employeee(eid),


pno int references project(pno),
hpw int,
primary key(eid,pno));

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


SQL> create table empphno(eid int references employeee(eid),
phno int,
primary key(eid,phno));

SQL> desc employee;


Name Null? Type
----------------------------------------- -------- -----------------
EID NOT NULL NUMBER(38)
NAME VARCHAR2(30)
ADDRESS VARCHAR2(30)
SUPEID NUMBER(38)
DNO NUMBER(38)

SQL> desc department;


Name Null? Type
----------------------------------------- -------- -----------------
DNO NOT NULL NUMBER(38)
DNAME VARCHAR2(20)
DLOCATION VARCHAR2(10)
MGRID NUMBER(38)

SQL> desc project;


Name Null? Type
----------------------------------------- -------- -----------------
PNO NOT NULL NUMBER(38)
PNAME VARCHAR2(20)
PLOCATION VARCHAR2(20)
DNO NUMBER(38)

SQL> desc dependent;


Name Null? Type
----------------------------------------- -------- -----------------
NAME NOT NULL VARCHAR2(20)
GENDER VARCHAR2(6)
AGE NUMBER(38)
ADDR VARCHAR2(20)
EID NOT NULL NUMBER(38)

SQL> desc empproj;


Name Null? Type
----------------------------------------- -------- -----------------
EID NOT NULL NUMBER(38)
PNO NOT NULL NUMBER(38)
HPW NUMBER(38)

SQL> desc empphno;


Name Null? Type
----------------------------------------- -------- -----------------
EID NOT NULL NUMBER(38)
PHNO NOT NULL NUMBER(38)

SQL> select * from employee;


EID NAME ADDRESS SUPEID DNO
---------- ---------- ---------- ---------- -----------
1 priya bangalore 5 200
2 sindu davangere 1 400
3 teertha sirsi 2 300

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


4 spurthy chikmangalore 3 200
5 raghavi bangalore 4 500

SQL> select * from department;

DNO DNAME DLOCATION MGRID


---------- -------------------- ---------- ----------
100 mca blore 4
200 mba mlore 5
300 cse mumbai 2
400 mech delhi 3
500 ece chennai 1

SQL> select * from project;

PNO PNAME PLOCATION DNO


---------- -------------------- -------------------- ----------
111 student blore 100
222 library madurai 300
333 hotel chennai 100
444 railway delhi 500
555 airline ranchi 400
5 sp mysore 100
10 raji kolkata 200

SQL> select * from dependent;

NAME GENDER AGE ADDR EID


-------------------- ------ ---------- -------------------- ----------
priya f 20 mumbai 1
divya f 19 blore 2
priyanka f 18 madurai 3
sarvan m 24 delhi 3
jothi f 40 madurai 5
lakshmi f 23 udupi 1

SQL> select * from empproj;

EID PNO HPW


---------- ---------- ----------
1 111 5
3 222 4
2 333 7
4 111 10
5 444 20
1 5 4
1 10 8

SQL> select * from empphno;

EID PHNO
---------- ----------
3 9025678934
4 9807654323
5 8907654323

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


2 7896897654
1 9087654321

Query 1. select * from employee where eid in(select w1.eid from


empproj w1,empproj w2 where w1.pno=5 and w2.pno=10 and
w1.eid=w2.eid);

EID NAME ADDRESS SUPEID DNO


---------- ---------- ---------- -------- -----
1 priya bangalore 5 200

Query 2. select * from employee where eid in(select eid from


dependent group by eid having count(eid)>=2);

EID NAME ADDRESS SUPEID DNO


---------- -------- ---------- ------- ------
1 priya bangalore 5 200
3 teertha sirsi 2 300

Query 3. select pname from project where pno in(select pno from
empproj group by pno having count(pno)=(select max(count(pno)) from
empproj group by pno))

PNAME
--------------------
student

Query 4. select * from employee where eid not in (select eid from
dependent);

EID NAME ADDRESS SUPEID DNO


---------- -------- --------- --------- ----------
4 spurthy chikmangalore 3 200

Query 5. select * from employee where eid in(select eid from empproj
group by eid having sum(hpw)= 2 (select max(sum(hpw)) from empproj
group by eid));

EID NAME ADDRESS SUPEID DNO


---------- -------- ---------- -------- ------
5 raghavi bangalore 4 500

Query 6.
create view empcount(dno,no_of_emp) as select dno,count(dno) from
employeee group by dno;

SQL> select * from empcount;

DNO NO_OF_EMP
---------- ----------
200 2
300 1
400 1
500 1

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


Program 4
Design an ER-diagram for the following scenario, 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
tourist_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 emailids. A
tourist visits many Tourist places, it is also required to record the visted_date in the database. A
tourist can visit aTourist place many times at different dates. A Tourist place can be visited by
many tourists either inthe same date or at different dates.
Queries:
1 List the state name which is having maximum number of tourist places.
2 List details of Tourist place where maximum number of tourists visited.
3 List the details of tourists visited all tourist places of the state “KARNATAKA”.
4 Display the details of the tourists visited at least one tourist place of the state, but visited all
states tourist places.
5 Display the details of the tourist place visited by the tourists of all country.

AGE KILOMETERS
COUNTRY
HISTORY
STATE
VDATE

TOURIST M N TOURIST_PLACE
VISITS

TID TNAME
TPID TP_NAME

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


create table tourist_place
(tpid number primary key,
history varchar(20),
kilometers number(3)
,state varchar(20),
tpname varchar(20));

create table tourist(tid number primary key,


country varchar(20),
age number,
tname varchar(20));

create table visits


(tpid number(3) references tourist_place(tpid),
tid number references tourist(tid),
vdate date,
primary key(tpid,tid));

create table email


(tid number references tourist(tid),
email varchar(20),primary key(tid,email));

desc tourist_place;
Name Null? Type
----------------------------------------- -------- ---------------------
TPID NOT NULL NUMBER
HISTORY VARCHAR2(20)
KILOMETERS NUMBER
STATE VARCHAR2(20)
TPNAME VARCHAR2(20)

desc tourist;
Name Null? Type
----------------------------------------- -------- ---------------------
TID NOT NULL NUMBER
COUNTRY VARCHAR2(20)
AGE NUMBER
TNAME VARCHAR2(20)

desc visits;
Name Null? Type
----------------------------------------- -------- ---------------------
TPID NOT NULL NUMBER
TID NOT NULL NUMBER
VDATE DATE

desc email;
Name Null? Type
----------------------------------------- -------- ---------------------
TID NUMBER
EMAIL VARCHAR2(20)

SQL> insert into


tourist_place(tpid,history,kilometers,state,tpname)values('11','beauty','
160','karnataka','ooty');

1 row created.

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


SQL> select * from tourist_place;

TPID HISTORY KILOMETERS STATE TPNAME


---------- -------------------- ---------- ------------- ---------
11 beauty 160 karnataka ooty
12 monuments 270 kerala beluru
13 beach 360 tamilnadu marina

SQL> insert into


tourist(tid,country,age,tname)values('22','india','34','prakash');

1 row created.

SQL> select * from tourist;

TID COUNTRY AGE TNAME


---------- -------------------- ---------- --------------------
22 india 34 prakash
23 orissa 28 bhanu
24 india 30 nagesh

SQL> insert into visits values('&tpid','&tid','&vdate');


Enter value for tpid: 12
Enter value for tid: 23
Enter value for vdate: 13-nov-2014
old 1: insert into visits values('&tpid','&tid','&vdate')
new 1: insert into visits values('12','23','13-nov-2014')

1 row created.

SQL> select * from visits;

TPID TID VDATE


---- ---------- ---------
12 23 13-NOV-14
11 24 24-JUN-13
13 22 25-SEP-11
11 23 23-FEB-10
13 23 12-JAN-10
14 24 10-JAN-17

SQL> insert into email values('&tid','&email');


Enter value for tid: 23
Enter value for email: [email protected]
old 1: insert into email values('&tid','&email')
new 1: insert into email values('23','[email protected]')

1 row created.

SQL> select * from email;


TID EMAIL
---------- --------------------
23 [email protected]
22 [email protected]
24 [email protected]

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


Query 1:
select state from tourist_place group by state having
count(state)=(select max(count(state)) from tourist_place group by
state);

STATE
--------------------
karnataka

query 2:
select * from tourist_place where tpid in (select tpid from visits group
by tpid having count(tpid)= (select max(count(tpid)) from visits group by
tpid));

TPID HISTORY KILOMETERS STATE TPNAME


--------- -------------------- ---------- -------------------- ----------
11 beauty 160 karnataka ooty
13 beach 360 tamilnadu marina

query 3:
select * from tourist t where t.tid in
(select tid from visits join tourist_place on
visits.tpid=tourist_place.tpid where state='karnataka'
group by tid having count(state) in (select count(state ) from
tourist_place where state='karnataka') );

TID COUNTRY AGE TNAME


------ -------------------- ---------- --------------------
24 india 30 nagesh

query 4:
select * from tourist t where t.tid in (select tid from visits join
tourist_place on visits.tpid=tourist_place.tpid
group by tid having count(distinct state)
in (select count(distinct state ) from tourist_place) );

TID COUNTRY AGE TNAME


----- -------------------- ---------- --------------------
23 orissa 28 bhanu

query 5:
select * from tourist_place where tpid in (
select tpid from visits join tourist on visits.tid=tourist.tid
group by tpid having count(distinct country)=
(select count(distinct country) from tourist));

TPID HISTORY KILOMETERS STATE TPNAME


----- -------------------- ---------- -------------------- --------------
11 beauty 160 karnataka ooty
13 beach 360 tamilnadu marina

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


Program 5
Design an ER-diagram for the following scenario, Convert the same into a relational model,
normalize Relations into a suitable Normal form and then solve the following queries.
A country wants to conduct an election for the parliament. A country having many constituencies.
Each constituency is identified uniquely 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 (involves Houseno, city, state, pincode). 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 candidate can contest from many constituencies under a same party. A
party can have many candidates 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.
Queries:
1 List the details of the candidates who are contesting from more than one constituencies which are
belongs to different states.
2 Display the state name having maximum number of constituencies.
3 Create a stored procedure to insert the tuple into the voter table by checking the voter age.
If voter’s age is at least 18 years old, then insert the tuple into the voter else display the
“Not an eligible voter msg” .
4 Create a stored procedure to display the number_of_voters in the specified constituency. Where the
constituency name is passed as an argument to the stored procedure.
5 Create a TRIGGER to UPDATE the count of “ Number_of_voters” of the respectiv
constituency in “CONSTITUENCY” table , AFTER inserting a tuple into the “VOTERS” table.

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238
create table constituency
(cons_id number(20) primary key,
csname varchar(20),
csstate varchar(20),
no_of_voters number(10));

create table party


(pid number(20) primary key,
pname varchar(20),
psymbol varchar(10));

create table candidates


(cand_id number(12) primary key,
phone_no number(10),
age number(2),
state varchar(20),
name varchar(20),
pid int references party(pid));

create table contest


(cons_id number(20) references constituency(cons_id),
cand_id number(12) references candidates(cand_id)
primary key(cons_id,cand_id);

create table voter


(vid number(20) primary key,
vname varchar(20),
vage number(5),
vaddr varchar(20),
cons_id number(20) references constituency(cons_id),
cand_id number(12) references candidates(cand_id));

select * from constituency;

CONS_ID CSNAME CSSTATE NO_OF_VOTERS


-------- -------------------- -------------------- ------------
111 rajajinagar karnataka 4
222 ramnagar kerala 1

select * from party;

PID PNAME PSYMBOL


---- -------------------- ----------
876 bjp lotus
877 congress hand

select * from candidates;

CAND_ID PHONE_NO AGE STATE NAME PID


-------- ---------- ---------- -------------------- --------- ----------
121 9538904626 23 kerala raksha 876
122 9740777502 24 karnataka veena 877

select * from contest;


CONS_ID CAND_ID
--------- ----------
111 122

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


222 121
222 122

select * from voter;

VID VNAME VAGE VADDR CONS_ID CAND_ID


----- -------------------- ---------- ------------- ---------- ----------
345 prashanth 21 kanakpura 222 122
346 prakash 23 ramnagar 111 121
348 nagesh 30 mandya 111 121
349 nagesh 30 mandya 111 121

Query 1:
select * from candidates where cand_id in (select cand_id from contest
join constituency on contest.cons_id=constituency.cons_id
group by cand_id having count(distinct(csstate))>1);

CAND_ID PHONE_NO AGE STATE NAME PID


------- ---------- ---------- ----------- -------------------- --------
122 9740777502 24 karnataka veena 877

Query 2:
select csstate from constituency group by csstate having count(csstate)
in (select max(count(csstate)) from constituency group by csstate);

CSSTATE
--------------------
karnataka

query 3:

create or replace procedure agechecking ( id in number,age in


number)
as
BEGIN
if age>18 then
insert into voter(vid,vage) values (id,age);
else
dbms_output.put_line('age should be high');
end if;
end agechecking;
/

Procedure created.

SQL> set serveroutput on;


SQL> exec agechecking (25,21);

PL/SQL procedure successfully completed. // row inserted

SQL> exec agechecking (20,15);


age should be high //Message displayed as age is less than or equal to 18

PL/SQL procedure successfully completed.

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238


query 4:
create or replace procedure display_count
(
const_id number
)
as
vid constituency.cons_id % type;
begin
select no_of_voters into vid from constituency where cons_id = const_id
and rownum = 1;
dbms_output.put_line ( 'total voters are: ' || vid);
end;
/

Procedure created.
SQL> select * from constituency;

CONS_ID CSNAME CSSTATE NO_OF_VOTERS


---------- -------------------- -------------------- ------------
111 rajajinagar karnataka 2
222 ramnagar kerala 1

SQL> exec display_count(111);


total voters are: 2

Query 5:
create or replace trigger count
after insert on voter
for each row
begin
update constituency
set no_of_voters = no_of_voters + 1
where cons_id=:new.cons_id;
end count;
/
Trigger created.

SQL> set serveroutput on;


SQL> select * from constituency;

CONS_ID CSNAME CSSTATE NO_OF_VOTERS


---------- -------------------- -------------------- ------------
111 rajajinagar karnataka 2
222 ramnagar kerala 1

SQL> insert into voter values(348,'nagesh',30,'mandya',111,121);

1 row created.

After insertion into voter table , the constituency table is


automatically updated.

SQL> select * from constituency;

CONS_ID CSNAME CSSTATE NO_OF_VOTERS


---------- -------------------- -------------------- ------------
111 rajajinagar karnataka 3
222 ramnagar kerala 1

Nagesh B S, Asst. Prof., Dept. of MCA, RNSIT, Bengaluru phone:9844032238

You might also like