Dbms Lab Manual II Cse II Sem
Dbms Lab Manual II Cse II Sem
2021-2022(R18)
1|Page
CS407PC: DATABASE MANAGEMENT SYSTEMS LAB
LIST OF EXPERIMENTS:
1. Concept design with E-R Model
2. Relational Model
3. Normalization
4. Practicing DDL commands
5. Practicing DML commands
6. Querying (using ANY, ALL, IN, Exists, NOT EXISTS, UNION, INTERSECT, Constraints etc.)
7. Queries using Aggregate functions, GROUP BY, HAVING and Creation and dropping of Views.
8. Triggers (Creation of insert trigger, delete trigger, update trigger)
9. Procedures
10. Usage of Cursors
2|Page
EXPERIMENT- 1
CONCEPT DESIGN WITH E-R MODEL
AIM: To Relate the entities appropriately. Apply cardinalities for each relationship. Identify strong and weak
entities. Indicate the type of relationships (total/partial). Incorporate generalization, aggregation and specialization
etc wherever required.
E-R Model
Bus
BusNo
Source
Destination
CoachType
SCHEMA
Bus: Bus(BusNo :String ,Source : String, Destination: String, Coach Type: String)
Ticket
TicketNo
DOJ
Address
ContactNo
BusNo
SeatNo
Source
Destination
1|Page
SCHEMA
Ticket (TicketNo: string, DOJ: date, Address: string, ContactNo : string, BusNo:String
SeatNo : Integer, Source: String, Destination: String)
Passenger
PassportID
TicketNo
Name
ContactNo
Age
Sex
Address
SCHEMA
Passenger (PassportID: String, TicketNo :string, Name: String, ContactNo: string, Age:
integer, Sex: character, Address: String)
2|Page
Reservation
PNRNo
DOJ
No_of_seats
Address
ContactNo
BusNo
SeatNo
SCHEMA
Reservation(PNRNo: String, DOJ: Date, NoofSeats: integer , Address: String ,ContactNo: String, ,
BusNo: String,SeatNo:Integer)
3|Page
Cancellation
PNRNo
DOJ
SeatNo
ContactNo
Status
SCHEMA
Cancellation (PNRNo: String, DOJ: Date, SeatNo: integer, ContactNo: String, Status:
String)
4|Page
CONCEPT DESIGN WITH E-R MODEL
5|Page
EXPERIMENT – 2
RELATIONAL MODEL
AIM: To Represent all the entities (Strong, Weak) in tabular fashion. Represent relationships in a tabular fashion.
Mysql>desc Bus;
Ticket:
Ticket(TicketNo: string, DOJ: date, Address:string,ContactNo: string, BusNo:String, SeatNo :Integer, Source: String,
Destination: String)
6|Page
ColumnName Datatype Constraints Type of Attributes
TicketNo Varchar(20) Primary Key Single-valued
Mysql> create table ticket(ticketno varchar(20), doj date,address varchar(20),contactno int, busno
varchar(20),seatno int,source varchar(10),destination varchar(10),primary key(ticketno,busno) foreign key(busno)
references bus(busno);
Mysql>desc Ticket;
Passenger:
Type of
ColumnName Datatype Constraints Attributes
PassportID Varchar(15) Primary Key Single-valued
7|Page
TicketNo Varchar(20) Foreign Key Single-valued
8|Page
Name Varchar(20) Composite
ContactNo Varchar(20) Multi-valued
Age Integer Single-valued
Sex character Simple
Address Varchar(20) Composite
Reservation:
9|Page
ContactNo Varchar(10) Multi-valued
10 | P a g e
BusNo Varchar(10) Foreign Single-valued
Key
SeatNo Integer Simple
Mysql> Create table Resevation(PNRNo varchar(20),DOJ date,NoofSeates integer,Address varchar(20),ContactNo
varchar(20),BusNo varchar(20),SeatNo integer, primary key(PNRNo,BusNo),foreign key(BusNo) references
Bus(BusNo));
Cancellation:
11 | P a g e
12 | P a g e
13 | P a g e
EXPERIMENT – 3
NORMALIZATION
AIM: Apply the database Normalization techniques for designing relational database tables to minimize
duplication of information like 1NF, 2NF, 3NF, BCNF.
Normalization is a process of converting a relation to be standard form by decomposition a larger relation into
smaller efficient relation that depicts a good database design.
1NF: A Relation scheme is said to be in 1NF if the attribute values in the relation are atomic.i.e., Mutli –valued
attributes are not permitted.
2NF: A Relation scheme is said to be in 2NF,iff and every Non-key attribute is fully functionally dependent on
primary Key.
3NF: A Relation scheme is said to be in 3NF,iff and does not have transitivity dependencies. A Relation is said
to be 3NF if every determinant is a key for each & every functional dependency.
BCNF: A Relation scheme is said to be BCNF if the following statements are true for eacg FD P->Q in set F of
FDs that holds for each FD. P->Q in set F of FD’s that holds over R. Here P is the subset of attributes of R & Q
is a single attribute of R.
P is a super key.
14 | P a g e
Normalized tables are:-
Mysql> Create table Reservation2(PNRNO integer Primary key, JourneyDate DateTime,NoofSeats int,Address
varchar(20),ContactNo Integer);
Mysql> Create table Ticket2(TicketNo Integer Primary key,JourneyDate DateTime, Age Int(4),Sex char(2),Source
varchar(20),Destination varchar(20),DeptTime varchar(2));
15 | P a g e
EXPERIMENT – 4
PRACTICING DDL COMMANDS
16 | P a g e
Mysql>Alter Table passenger3 add Foreign key(TicketNo) references Ticket(TicketNo);
17 | P a g e
Mysql>Alter table passenger drop foreign key fk1;
18 | P a g e
EXPERIMENT – 5
PRACTICING DML COMMANDS
AIM: Create a DML Commands are used to manage data within the scheme objects.
DML Commands:
19 | P a g e
mysql> select * from Bus2;
20 | P a g e
mysql> select * from Passenger2;
21 | P a g e
UPDATE COMMAND ON BUS2 RELATION
mysql> Update Bus2 SET Source='Secundrabad' where BusNo=1234; Query OK, 1 row affected (0.05 sec)
22 | P a g e
DELETE COMMAND ON BUS2 RELATION
mysql> Delete from Bus2 where BusNo=1234; Query OK, 1 row affected (0.05 sec)
23 | P a g e
mysql> Delete from Bus2 where Source=’Secundrabad’; Query OK, 1 row affected (0.05 sec)
24 | P a g e
EXPERIMENT – 6
Querying (using ANY, ALL, IN, Exists, NOT EXISTS, UNION, INTERSECT, Constraints etc.) Aim:
25 | P a g e
mysql> insert into passenger2 values(82302,'Smith',23,'M','Hyderabad');
PNR_No
10201
10202
10203
10204
27 | P a g e
2. Display all the names of male passengers.
28 | P a g e
29 | P a g e
3. Display the ticket numbers and names of all the passengers.
30 | P a g e
4. Find the ticket numbers of the passengers whose name start with ‘r’ and ends with ‘h’.
Name
Rajesh
Ramesh
Ramesh
31 | P a g e
5. Find the names of Passengers whose age is between 30 and 45.
32 | P a g e
6. Display all the passengers names beginning with ‘A’.
Name
Akash
Arivind
Avinash
33 | P a g e
7. Display the sorted list of Passengers names
34 | P a g e
EXPERIMENT – 7
Querying Aggregate Functions(COUNT,SUM,AVG,MAX and MIN)
1. Write a Query to display the information present in the passenger and cancellation tables
35 | P a g e
MySQL> SELECT * FROM RESERVATION UNION
36 | P a g e
3. Find number of tickets booked for each PNR_No using GROUP BY CLAUSE
37 | P a g e
5 Mysql> select sum(Noofseats) from Cancellation2;
38 | P a g e
Creation and Droping of Views
mysql> create table students(sid int primary key,name varchar(15),login varchar(15), age
int,gpa real); mysql> create table Enrolled(sid int,cid int,grade varchar(5),primary
key(sid,cid), foreign key(sid) references students(sid));
E.grade='B';
39
EXPERIMENT – 8
TRIGGERS
Aim: Creation of insert trigger, delete trigger and update trigger.
40
CREATE TRIGGER BEFORE_BUS_UPDATE BEFORE UPDATE ON BUS
UPDATE :
41
SNo Source Changedon Action
1 Banglore 2014:03:23 12:51:00 Insert
2 Kerela 2014:03:25:12:56:00 Update
3 Mumbai 2014:04:26:12:59:02 Delete
INSERT:
42
CREATE TRIGGER BEFORE_BUS_DELETE BEFORE DELETE ON BUS
Examples
BEGIN
END;
43
44
EXPERIMENT – 9
PROCEDURES
Ex1:
END$$
CALL BUS_PROC1()$$
Ex2:
CREATE PROCEDURE SAMPLE2() BEGIN
DECLARE X INT(3); SET X=10;
SELECT X;
END$$
Mysql> CALL SAMPLE2()$$
45
Ex3: CREATE PROCEDURE SIMPLE_PROC(OUT PARAM1 INT) BEGIN
END$$
46
EXPERIMENT – 10
Cursors
Aim: Declare a cursor that defines a result set. Open the cursor to establish the result set.
Fetch the data into local variables as needed from the cursor, one row at a time. Close the
cursor when done.
Cursors
In MySQL, a cursor allows row-by-row processing of the result sets. A
cursor is used for the result set and returned from a query. By using a
cursor, you can iterate, or by step through the results of a query and
perform certain operations on each row. The cursor allows you to iterate
through the result set and then perform the additional processing only on
the rows that require it.
□ Declare a cursor
47
Syntax : DECLARE cursor_name CURSOR FOR select_statement;
2 . Open a cursor statement : For open a cursor we must use the open
statement.If we want to fetch rows from it you must open thecursor.
If any row exists, then the above statement fetches the next row and cursor
pointer moves ahead to the next row.
Syntax: CLOSE_name;
Delimiter $$
48
***
49
ADDITIONAL PROGRAMS
EMPLOYEES TABLE
50
DEPARTMENT TABLE
51
Sailors , Reserves , Boats Tables
Mysql> Create table Sailors(Sid integer PRIMARY KEY,sname varchar(15), rating int,age
real); Mysql>Create table Reserves(Sid int,Bid int,Day Date);
52
mysql> select S.sname from sailors S, reserves R where S.sid=R.sid AND R.bid=103;
mysql> select sname from sailors s,Reserves R where S.sid=R.sid AND bid=103; mysql>
select R.sid from Boats B,Reserves R where B.bid=R.bid AND B.color='red';
mysql> select S.sname from sailors S,reserves R,Boats B where S.sid=R.sid AND
R.bid=B.bid AND B.color='red';
mysql> select B.color from Sailors S,Reserves R,Boats B where S.sid=R.sid AND
R.bid=B.bid AND S.sname='Lubber';
53
mysql> select S.sname,S.rating+1 AS rating from Sailors S,Reserves R1,Reserves R2 where
S.sid=R1.sid AND S.sid=R2.sid AND R1.day=R2.day AND R1.bid<>R2.bid;
54
USING UNION , INTERSECT , AND EXCEPT
1).Find the names of sailors who have reserved a red or a green boat.
OR
2). Find the names of sailors who have reserved both a red and a green boat.
SELECT S.SNAME
SELECT S2.SNAME
NESTED QUERIES
1) Find sailors whose rating is better than some sailor called Horatio
1) Find the age of the youngest sailor for each rating level.
2) Find the age of the youngest sailor who is eligible to vote for each rating level with at
least two such sailors
4) Find the average age of sailors for each rating level that has at least two sailors