DBMS_Program 3-5
DBMS_Program 3-5
Design an ER-diagram for the following scenario, Convert the same into a relational model
and then solve the following queries.
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.
5. Display the team name where all its won matches played in the same stadium.
SCHEMA :
TEAM
TID TNAME COACH CAPID CITY
PLAYER
STADIUM
QUERY 1:) DISPLAY THE YOUNGEST PLAYER (IN TERMS OF AGE) NAME, TEAM NAME ,
AGE IN WHICH HE BELONGS OF THE TOURNAMENT.
SQL>
SELECT P.PNAME,T.TNAME,P.AGE FROM TEAM T,PLAYER P WHERE T.TID=P.TID AND AGE
IN (SELECT MIN(AGE) FROM PLAYER);
--------------------------------------------------------------------------------
QUERY 2: LIST THE DETAILS OF THE STADIUM WHERE THE MAXIMUM NUMBER OF
MATCHES WERE PLAYED.
SQL>
SELECT * FROM STADIUM WHERE SID IN(SELECT STADIUMID FROM MATCH GROUP BY
STADIUMID HAVING COUNT(STADIUMID)=(SELECT MAX(COUNT(STADIUMID))FROM
MATCH GROUP BY STADIUMID));
--------------------------------------------------------------------------------
QUERY 3: LIST THE DETAILS OF THE PLAYER WHO IS NOT A CAPTAIN BUT GOT THE
MAN_OF _MATCH AWARD AT LEAST IN TWO MATCHES.
SQL>
SELECT * FROM PLAYER WHERE PID IN(SELECT MOMID FROM MATCH GROUP BY MOMID
HAVING COUNT(MOMID) > 1 AND MOMID NOT IN(SELECT CAPID FROM TEAM));
--------------------------------------------------------------------------------
QUERY 4: DISPLAY THE TEAM DETAILS WHO WON THE MAXIMUM MATCHES.
SQL>
SELECT * FROM TEAM WHERE TID IN(SELECT WINNERID FROM MATCH GROUP BY
WINNERID HAVING COUNT(WINNERID)=(SELECT MAX(TOTAL) FROM (SELECT
COUNT(WINNERID) AS TOTAL FROM MATCH GROUP BY WINNERID)));
QUERY 5: DISPLAY THE TEAM NAME WHERE ALL ITS WON MATCHES PLAYED IN THE
SAME STADIUM.
SQL>
SELECT TNAME FROM TEAM WHERE TID IN(SELECT WINNERID FROM MATCH GROUP BY
WINNERID HAVING COUNT(DISTINCT STADIUMID)=1);
--------------------------------------------------------------------------------
DBMS _ 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 solve 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 constituencty
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
respective constituency in “CONSTITUENCY” table , AFTER inserting a
tuple into the “VOTERS” table
✓ SCHEMA:
CANDIDATE
CONSTITUENCY
CONTEST
--------------------------------------------------------------------------------
QUERY 2:) DISPLAY THE STATE NAME HAVING MAXIMUM NUMBER OF CONSTITUENCIES.
SQL>
SELECT STATE FROM CONSTITUENCY GROUP BY STATE HAVING COUNT(STATE)=(SELECT
MAX(COUNT(STATE)) FROM CONSTITUENCY GROUP BY STATE);
--------------------------------------------------------------------------------
QUERY 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”.
SQL>
CREATE OR REPLACE PROCEDURE INSERTDATA(ID VARCHAR2,NAME VARCHAR2,AGE
NUMBER,ADDR VARCHAR2,CNID VARCHAR2) AS BEGIN
IF AGE>=18 THEN INSERT INTO VOTER(VID,NAME,AGE,ADDRESS,CNID)
VALUES(ID,NAME,AGE,ADDR,CNID); ELSE
DBMS_OUTPUT.PUT_LINE('NOT AN ELIGIBLE VOTER'); END IF;
END;
EXECUTION 1:
BEGIN INSERTDATA('V03','KIRAN',17,'BALLARY','CN2'); END;
EXECUTION 2:
BEGIN INSERTDATA('V07','YUVI',34,'KANYAKUMARI','CNT3'); END;
--------------------------------------------------------------------------------
QUERY 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.
SQL>
CREATE OR REPLACE PROCEDURE GETNOOFVOTER(CONSTI_NAME IN VARCHAR2) IS
NO_VOTERS NUMBER; BEGIN SELECT NO_OF_VOTERS INTO NO_VOTERS FROM
CONSTITUENCY C WHERE C.NAME=CONSTI_NAME;
DBMS_OUTPUT.PUT_LINE(NO_VOTERS); END;
(BEFORE EXICUTION)
SELECT *FROM VOTER;
BEGIN
INSERTDATA('V06','RAKESH',28,'RAJANUKUNTE','CNT5'); END;
(AFTER EXICUTION)
SELECT *FROM VOTER;
SELECT *FROM CONSTITUENCY;
DBMS _ 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 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 isalso required to
record the visted_date in the database. A tourist can visit a Tourist place many times
at different dates. A Tourist place can be visited by many touristseither in the same
date or at different dates.
Queries:
1 List the state name which is having maximum number of ourist places.
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.
✓ SCHEMA:
TOURIST_PLACE
TID NAME STATE KILO_METER HISTORY
TOURIST
--------------------------------------------------------------------------------
QUERY2: LIST DETAILS OF TOURIST PLACE WHERE MAXIMUM NUMBER OF
TOURISTS VISITED.
SQL>
SELECT * FROM TOURIST_PLACE WHERE TPID IN(SELECT V.TPID FROM
TOURIST T,VISIT V WHERE T.TID=V.TID GROUP BY V.TPID HAVING
COUNT(V.TPID)IN (SELECT MAX(COUNT(TPID))FROM VISIT GROUP BY TPID));
--------------------------------------------------------------------------------
QUERY3:) LIST THE DETAILS OF TOURISTS VISITED ALL TOURIST PLACES OF
THE STATE “KARNATAKA”.
SQL>
SELECT * FROM TOURIST WHERE TID IN(SELECT TID FROM VISIT V JOIN
TOURIST_PLACE TP ON V.TPID=TP.TPID WHERE TP.STATE='KARNATAKA'
GROUP BY TID HAVING COUNT(STATE) IN (SELECT COUNT(STATE) FROM
TOURIST_PLACE WHERE STATE='KARNATAKA'));
--------------------------------------------------------------------------------
QUERY4:) DISPLAY THE DETAILS OF THE TOURISTS VISITED AT LEAST ONE
TOURIST PLACE OF THE STATE, BUT VISITED ALL STATES TOURIST PLACES.
SQL>
SELECT * FROM TOURIST WHERE TID IN(SELECT TID FROM VISIT GROUP BY
TID HAVING COUNT(TID)>=(SELECT COUNT(*) FROM(SELECT STATE FROM
TOURIST_PLACE GROUP BY STATE)));
--------------------------------------------------------------------------------
QUERY5:) DISPLAY THE DETAILS OF THE TOURIST PLACE VISITED BY THE
TOURISTS OF ALL COUNTRY.
SQL>
SELECT *FROM TOURIST_PLACE WHERE TPID IN(SELECT TPID FROM VISIT
JOIN TOURIST ON VISIT.TID=TOURIST.TID GROUP BY TPID HAVING
COUNT(DISTINCT COUNTRY)=(SELECT COUNT(DISTINCT COUNTRY) FROM
TOURIST));
-------------------------------------------------------------------------------