Dbms Lab Journal 2024
Dbms Lab Journal 2024
Create the following tables with properly specifying Primary keys, foreign keys and solve
i. List the details of Students who are all studying in 2nd sem MCA.
ii. List the students who are not borrowed any books.
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.
1
DBMS LAB -22MCAL27
SCHEMA DIAGRAM
ER DIAGRAM
2
DBMS LAB -22MCAL27
3
DBMS LAB -22MCAL27
INSERT INTO BOOK VALUES ('B01', 'JAVA', 666, 'TATA MCGRAW', 1);
INSERT INTO BOOK VALUES ('B02', 'DBMC', 555, 'TATA MCGRAW', 1);
INSERT INTO BOOK VALUES ('B03', 'DBMS', 555, 'TATA MCGRAW', 2);
INSERT INTO BOOK VALUES ('B04', 'C++', 777, 'PH', 4);
INSERT INTO BOOK VALUES ('B05', 'CONSTRUCT', 444, 'PH', 5);
4
DBMS LAB -22MCAL27
5
DBMS LAB -22MCAL27
QUERY 1: LIST THE DETAILS OF STUDENTS WHO ARE ALL STUDYING IN 2ND SEM MCA.
SELECT *
FROM STUDENT S, BRANCH B
WHERE S. BRANCHID=B.BRANCHID AND
S.SEM = 2 AND
B.BRANCHNAME = 'MCA';
QUERY 2: LIST THE STUDENTS WHO ARE NOT BORROWED ANY BOOKS.
SELECT *
FROM STUDENT S
WHERE S.USN NOT IN (SELECT B.USN
FROM BORROW B);
6
DBMS LAB -22MCAL27
7
DBMS LAB -22MCAL27
QUERY 5: DISPLAY THE STUDENT DETAILS WHO BORROWED MORE THAN TWO BOOKS.
QUERY 6: DISPLAY THE STUDENT DETAILS WHO BORROWED BOOKS OF MORE THAN ONE AUTHOR.
8
DBMS LAB -22MCAL27
QUERY 8: LIST THE DETAILS OF STUDENTS WHO BORROWED THE BOOKS WHICH ARE ALL
PUBLISHED BY THE SAME PUBLISHER.
9
DBMS LAB -22MCAL27
2. STUDENT DATABASE
Consider the following schema:
STUDENT (USN, NAME, DATE_OF_BIRTH, BRANCH, MARK1, MARK2, MARK3, TOTAL, GPA)
SCHEMA DIAGRAM
STUDENT
ER DIAGRAM:
1
DBMS LAB -22MCAL27
2
DBMS LAB -22MCAL27
QUERY 1: UPDATE THE COLUMN TOTAL BY ADDING THE COLUMNS MARK1, MARK2, MARK3.
UPDATE STUDENTLAB
SET TOTAL = MARKS1 + MARKS2 + MARKS3;
UPDATE STUDENTLAB
SET GPA = (TOTAL/3.0);
3
DBMS LAB -22MCAL27
QUERY 3: FIND THE STUDENTS WHO BORN ON A PARTICULAR YEAR OF BIRTH FROM THE
DATE_OF_BIRTH COLUMN.
SELECT USN, NAME, DATE_OF_BIRTH
FROM STUDENTLAB
WHERE DATE_OF_BIRTH LIKE '%99';
QUERY 4: LIST THE STUDENTS WHO ARE STUDYING IN A PARTICULAR BRANCH OF STUDY.
SELECT USN, NAME, BRANCH
FROM STUDENTLAB
WHERE BRANCH = 'MCA';
4
DBMS LAB -22MCAL27
QUERY 6: FIND THE STUDENTS WHOSE NAME STARTS WITH THE ALPHABET “S”.
QUERY 7: FIND THE STUDENTS WHOSE NAME ENDS WITH THE ALPHABETS “AR”.
SELECT NAME
FROM STUDENTLAB
WHERE NAME ='%AR';
DELETE
FROM STUDENTLAB
WHERE USN = '1001';
5
DBMS LAB -22MCAL27
DESIGN AN ER-DIAGRAM FOR THE FOLLOWING SCENARIO, CONVERT THE SAME INTO A
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, pin code). 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.
I. Display the youngest player (in terms of age) name, team name, age in which he
II. List the details of the stadium where the maximum number of matches were played.
III. List the details of the player who is not a captain but got the man_of _match award at
least in two matches.
IV. Display the team details who won the maximum matches.
V. Display the team name where all its won matches played in the same stadium.
1
DBMS LAB -22MCAL27
SCHEMA DIAGRAM
ER DIAGRAM
2
DBMS LAB -22MCAL27
3
DBMS LAB -22MCAL27
4
DBMS LAB -22MCAL27
5
DBMS LAB -22MCAL27
6
DBMS LAB -22MCAL27
QUERY 1: DISPLAY THE YOUNGEST PLAYER (IN TERMS OF AGE) NAME, TEAM NAME, AGE IN
WHICH HE BELONGS OF THE TOURNAMENT.
SOLUTION:
QUERY 2: LIST THE DETAILS OF THE STADIUM WHERE THE MAXIMUM NUMBER OF
MATCHES WERE PLAYED.
SOLUTION:
SELECT *
FROM (SELECT M.SID, S.SNAME , COUNT(M.MID) AS NO_OF_MATCHES
FROM MATCHES M, STADIUM S
WHERE M.SID = S.SID
GROUP BY M.SID, S.SNAME
ORDER BY NO_OF_MATCHES DESC)
WHERE ROWNUM=1;
7
DBMS LAB -22MCAL27
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.
SOLUTION :
FROM MATCHES
GROUP BY MAN_OF_MATCH
QUERY 4: DISPLAY THE TEAM DETAILS WHO WON THE MAXIMUM MATCHES.
SOLUTION:
FROM MATCHES
GROUP BY WINNING_TEAM_ID
8
DBMS LAB -22MCAL27
QUERY 5: DISPLAY THE TEAM NAME WHERE ALL ITS WON MATCHES PLAYED IN THE SAME
STADIUM.
SOLUTION:
SELECT *
FROM TEAM
WHERE TID IN
(SELECT WINNING_TEAM_ID
FROM MATCHES
GROUP BY WINNING_TEAM_ID
HAVING COUNT(*)=(SELECT MAX(COUNT(*))
FROM MATCHES
GROUP BY SID));
9
DBMS LAB -22MCAL27
4.ELECTION DATABASE
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.Thereare 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:
i. List the details of the candidates who are contesting from more than one constituencies which
iii. 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
iv. Create a stored procedure to display the number_of_voters in the specified constituency.
1
DBMS LAB -22MCAL27
NAME VARCHAR(10),
STATE VARCHAR(10),
NO_OF_VOTERS INT );
PNAME VARCHAR(10),
SYMBOL VARCHAR(10) );
2
DBMS LAB -22MCAL27
NAME VARCHAR(10),
PNO VARCHAR(10),
AGE INT,
STATE VARCHAR(10),
NAME VARCHAR(10),
AGE VARCHAR(10),
ADDRESS VARCHAR(20),
3
DBMS LAB -22MCAL27
4
DBMS LAB -22MCAL27
QUERY 1: LIST THE DETAILS OF THE CANDIDATES WHO ARE CONTESTING FROM MORE THAN
ONE CONSTITUENCIES WHICH ARE BELONGS TO DIFFERENT STATES.
SELECT *
FROM CANDIDATE
GROUP BY CANDID
HAVING COUNT(DISTINCT(STATE))>1 );
GROUP BY STATE
FROM CONSTITUENCY
5
DBMS LAB -22MCAL27
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".
AS
BEGIN
IF AGE1>=18 THEN
ELSE
END IF;
END CHECKAGE;
1. EXECUTE CHECKAGE(12,18);
2. EXECUTE CHECKAGE(14,10);
6
DBMS LAB -22MCAL27
VOTER_COUNT NUMBER;
BEGIN
FROM CONSTITUENCY
END;
7
DBMS LAB -22MCAL27
BEGIN
UPDATE CONSTITUENCY
END;
8
DBMS LAB -22MCAL27
5. TOURISM
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
02.03.2021 updated 52/ 104 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 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.
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.
1
DBMS LAB -22MCAL27
SCHEMA DIAGRAM
ER DIAGRAM
2
DBMS LAB -22MCAL27
3
DBMS LAB -22MCAL27
4
DBMS LAB -22MCAL27
QUERY 1: LIST THE STATE NAME WHICH IS HAVING THE MAXIMUM NUMBER OF TOURIST
PLACES.
SELECT STATE
FROM PLACE
GROUP BY STATE
HAVING COUNT(STATE) = ( SELECT MAX(COUNT(STATE))
FROM PLACE
GROUP BY STATE);
5
DBMS LAB -22MCAL27
QUERY 2:LIST DETAILS OF TOURIST PLACE WHERE MAXIMUM NUMBER OF TOURISTS VISITED.
SELECT *
FROM PLACE
FROM VISIT
GROUP BY PID
FROM VISIT
GROUP BY PID));
QUERY 3: List the details of tourists visited all tourist places of the state “KARNATAKA”.
SELECT T.NAME,AGE, COUNTRY
FROM VISIT V, PLACE P, TOURIST T
WHERE V.PID = P.PID AND V.TID = T.TID AND STATE = 'KARNATAKA'
GROUP BY T.NAME, AGE, COUNTRY
HAVING COUNT(DISTINCT V.PID) = (
SELECT COUNT(*)
FROM PLACE
WHERE STATE = 'KARNATAKA' );
6
DBMS LAB -22MCAL27
QUERY 4: Display the details of the tourists visited at least one tourist place of the state, but
visited all states tourist places.
SELECT T.NAME, AGE, COUNTRY
FROM VISIT V, PLACE P, TOURIST T
WHERE V.PID = P.PID AND V.TID = T.TID
GROUP BY T.NAME, AGE, COUNTRY
HAVING COUNT(DISTINCT STATE) = (
SELECT COUNT(DISTINCT STATE)
FROM PLACE );
QUERY 5 : Display the details of the tourist place visited by the tourists of all country.