Dbms Lab Manual
Dbms Lab Manual
E-R Model
Analyze the problem carefully and come up with entities in it. Identify what date has to
be persisted in the database. This contains the entities, attributes etc.
Identify the primary keys for all the entities. Identify the other keys like candidate keys,
partial keys, if any.
Definitions:
1. Entity: It is a “thing” or “object” in the real world that is distinguishable from all other
objects. ER Model represents an entity with an independent existence.
Eg:
2. Attributes: The properties that characterize an entity set are called its attributes. An
attribute is referred to by the terms data items, data element, data field item.
Ex: attributes for bus entity and ticket entity.
2.1.2. Single valued and multivalued attributes: Single valued attribute can take on
only a single value for each entity instance.
Ex: Age of passenger in Passenger entity.
Multivalued attribute can take up many values.
Ex: Phno in Passenger entity.
2.1.3. Derived attributes:In some cases, two or more attribute values are related. With
the help of one attribute we get the value of another attribute. Age and DOB
attributes. With the DOB we get the age of the person to the current date.
1
2.2. Types of Keys :
Datatypes :
1. Bus entity:
Busno : Varchar(8): Primary key
Name : Varchar(15)
Type: Varchar (10)
2. Ticket entity:
Tktno: Numeric(8): Primary key
Jour_date : Date
Src: Varchar(10)
dest: Varchar(10)
Dept_time: Time
Busno: Varchar(8): Foreign key
Amt: Varchar(4)
Reach_time: Time
2
3. Passenger entity:
Pnr_no: Numeric(9): Primary key
Tktno: Numeric(8): Foreign key
Name: Varchar(15)
Age: Int(4)
Gender: Char(10): Male/Female
Ppno:Varchar(15)
4. Reservation entity:
Pnr_no: Numeric(9): Foreign key
Jour_date : Date
Noofseats: Int(8)
Address: Varchar(50)
Contactno: numeric(9)
Status: Char (3): Yes/No
5. Cancellation entity:
Pnr_no: Numeric(9): Foreign key
Jour_date : Date
Noofseats: Int(8)
Address: Varchar(50)
Contactno: numeric(9)
Status: Char (3): Yes/No
All these entities are used for extract data when necessary while writing the queries in
the coming weeks. There are some extra entities which are not used for writing queries.
6. Branch entity:
Name: Varchar(8)
Location: Varchar(20)
3
Week: 2
Concept design with E – R model
Relate the entities appropriately. Apply cardinalities for each relationship. Identify
strong entities and weak entities (if any). Indicate the type of relationship (total/partial). Try
to incorporate generalization, aggregation, specialization etc wherever required.
ER-Model:
Describes data as entities, relationships and attributes .The ER-Model is important
preliminary for its role in database design. ER Model is usually shown pictorially using entity
relationship diagrams.
Bus no
Name
Bus
Type
4
Entity diagram for Ticket
Jour_date
Tktno
Src
Busno
Ticket
Dest
Amt
Dept_tim
Reach_tim e
e
Pnrno
Tktno
Passenger Name
Gender
Age
Ppno
Pnrno
Status
Reservation Jour_date
contactno
Noofseats
Addres
s
5
Entity diagram for Cancellation
Pnrno
Status
Cancellation Jour_date
contactno
Noofseats
Addres
s
Name
Location
Branch
The cardinality ratio: - specifies the number of entities to which another entity can be
associated via a relationship set.
For a binary relationship set R between entity sets A & B, the mapping cardinality must
be one of the following:
1. One-to-One: An entity in A is associated with at most one entity in B and vice versa.
tktno
Journe
tktno name
y Date
Pnrno
Src age
Ticket Passenger
Issued
to
Dest
Gen
ppno der
Reach_tim
Dept_tim e
e
6
Ex: “contains” relation between bus and passenger entities.
name
Busno Pnrno
Age
Name Bus Contain Passenger
s
Ppno Gender
Type
tktno
Age Ppno
Type
Gender
7
Types of entities :-
Weak and strong entity: - an entity set may not have sufficient attributes to form a primary
key. Such an entity set is termed a weak entity set. An entity set that has primary key is termed
a strong entity set.
Entity Relationship diagram consisting of Bus, ticket, Passenger and Branch entites :
Name
Location
Bus no
Branch
Deals
with Name
Bus
Type
Consist
s of
tktno
Journe
name
y Date
Pnrno
Src age
Ticket Passenger
Issued
to
Dest
Gen
tktno der
Reach_tim ppno
Dept_tim e
e
8
1. Generalization: It consists of identifying some common characteristics of a collection
of entity set and creating new entity set that contains entities possessing these common
characteristics.
9
3. Specialization: It is the process of identifying subsets of an entity set (the super set)
that share some distinguishing characteristics. This entity type is called the super class
of the specialization.
Ex:
10
Week-3
Relational model
Represent all entities (strong, week) in tabular fashion. Represent relationships in a
tabular fashion. There are different ways of representing as tables based on the cardinality.
Represent attributes as columns in the tables or as tables based on the requirement. Different
types of attributes (composite, multivalued and derived).
Schema : Databases change over time as information is inserted and deleted. The collection of
information stored in a database at a particular moment is called an instance of the database.
Reservation(pnrno:numeric(9),jour_date:date,noofseats:int(8),address:varchar(50),contactno
:numeric(10),status:char(3))
Cancellations((pnrno:numeric(9),jour_date:date,noofseats:int(8),address:varchar(50),contact
no:numeric(10),status:char(3))
Bus:
Busno name type
100 Xyz a/c
101 Pop Non a/c
102 Xxx a/c
Ticket:
Tktno Jour_date Src Dest amt busno Dept_time Reach_time
10001 20-07-10 Hyd Delhi 800 100 06:00 22:00
10002 21-07-10 Hyd Chennai 700 101 08:00 23:00
10003 05-08-10 Delhi Hyd 800 102 06:00 22:00
11
Passenger:
Pnrno name tktno age Gender ppno
1001 Alekhya 10001 25 F ff11112
1002 Krupani 10002 27 F ff22332
1003 Prathima 10003 28 F F234444
1004 Prem 10004 30 M Ff202020
Reservation:
Pnrno Jour_date Noofseats Address Contactno Status
1001 20-07-10 4 Hyd 9492500000 Yes
1002 21-07-10 5 Sec 9492511111 Yes
1003 05-08-10 10 hyd 9949022222 No
Cancels:
Pnrno Joudate Noofseats Address Contact_no Status
1001 20-07-10 4 Hyd 9492500000 Yes
1002 21-07-10 5 Sec 9492511111 Yes
12
Week: 4
Normalization:
Database normalization is a technique for designing relational database tables to
minimize duplication of information and, in doing so , to safeguard the database against certain
types of logical or structural problems namely data anomalies.
Passenger:
The above relation is now in 1NF and the relation is 2NF as there are no partial functional
dependencies and the relation is also in 3NF as there are no transitive dependencies.
Bus:
Busno serviceno source destination bustype Noofseats
1001 3300 Hyd Delhi A/c 20
1002 4400 Hyd Chennai A/c 28
1003 5500 Hyd Bglore Non a/c 30
In this relation the values in each column are atomic so it is already in 1NF.
In the Bus entity Busno+serviceno is the primary key.
13
There exists following partial dependencies.
Busno ----> Bustype,Noofseats
Serviceno---->Source,Dest
The above relation is 2NF. And all columns directly depend on primary key. So there is no
transitive dependency and the relation is 3NF.
In this relation the values in each column are atomic so it is already in 1NF.
In the above relation there are no partial functional dependencies so the relation is in 2NF.
The ticket entity might face the following transitive dependency
Ticketno-------> catcard
Catcard--------->amount
So the relation is in 3NF.
Ticketno Joudate Source Destination Catcard
1111 2010-10-08 Hyd Delhi No
2222 2010-10-08 Hyd Chennai Yes
3333 2010-08-08 Hyd Bglore Yes
Put the catcard and amount attributes in a separate table. Then the relation should be in 3NF.
Catcard Amount
No 1200
Yes 1000
Yes 800
The above relation is 3NF as we have eliminated the transitive dependencies.
Finally all the tables are normalized and free from data redundancy, partial functional
dependencies and transitive dependencies.
14
Week 5:
Installation of Mysql and Practicing DDL commands: Installation of Mysql. In this week you
will learn creating databases, how to create tables, altering the tables, dropping tables and
databases if not required. You will also try truncate, rename commands etc.
Step: 1 download mysql essential from the website www.mysql.com/downloads and save the
.exe file.
Steps: 4&5
Steps: 6&7
15
Steps: 8&9
Steps: 10&11
Step: 12&13
16
Steps: 14&15
Steps: 16&17
Steps: 18&19
17
1. Creation of databases:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.09 sec)
2. Creation of tables:
mysql> create table groupamem(rollno numeric(10),name varchar(15),phone numeric (10),
branch varchar(10));
Query OK, 0 rows affected (0.42 sec)
18
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| rollno | decimal(10,0) | YES | | NULL | |
| name | varchar(15) | YES | | NULL | |
| phone | decimal(10,0) | YES | | NULL | |
| branch | varchar(10) | YES | | NULL | |
| gender | char(3) | YES | | NULL | |
+--------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
19
5 rows in set (0.00 sec)
2. Ticket
mysql> create table ticket(tkt_no numeric(8) primary key, jour_date date, src varchar(10),dest
varchar(10), busno varchar(8),amt varchar(4),dept_time time, reach_time time);
Query OK, 0 rows affected (0.08 sec)
mysql> alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno);
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
3. Passenger
mysql> create table passenger(pnr_no numeric(9),primary key,tktno numeric(8),name varc
har(15),age int(4),Gender char(10),ppno varchar(15));
Query OK, 0 rows affected (0.06 sec)
mysql> alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno);
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
20
4. Reservation
mysql> create table reservation(pnr_no numeric(9),noofseats int(8),jour_date datetime,
address varchar (50), phno numeric(10),status char(3));
Query OK, 0 rows affected (0.16 sec)
mysql> alter table reservation add constraint res_fk foreign key(pnr_no) references passenger
(pnr_no);
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
5. Cancel
mysql> create table cancel(pnr_no numeric(9),noofseats int(4),address varchar(20), phno
numeric(10),status char(3));
Query OK, 0 rows affected (0.06 sec)
mysql> alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger
(pnr_no);
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
6. Branch
mysql>create table branch(name varchar(8),location varchar(20));
Query OK, 0 rows affected (0.06 sec)
21
Week 6:
Practicing DML commands:DML commands are used to for managing data within the schema
objects.
22
Inserting values into cancellation table:
mysql> insert into cancel values(2003,5, ’2010-08-08’,'hno:101,chpy',9000011111,'yes');
Query OK, 1 row affected (0.05 sec)
mysql> insert into cancel values(2004,8, ’2010-08-08’,'hno:102,chpy',9000000000,'yes');
Query OK, 1 row affected (0.03 sec)
mysql> insert into cancel values(2002,2, ’2010-10-08’,'hno:102,msrd',9999900000,'yes');
Query OK, 1 row affected (0.05 sec)
+--------+------------+--------+------+----------+----------+-----------+--------+
| tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt |
+--------+------------+--------+------+----------+----------+-----------+--------+
| 1111 | 2010-10-08 | hyd | delhi | 09:00:00 | 21:00:00 | 1001 |1200|
| 2222 | 2010-10-08| hyd | delhi | 09:00:00 | 21:00:00 | 1001 |1200|
| 3333 | 2010-11-08 | hyd | bglore | 09:00:00 | 15:00:00 | 1002 |800|
| 4444 | 2010-11-08 | hyd | bglore | 09:00:00 | 15:00:00 | 1002 |800|
+--------+------------+--------+------+----------+----------+-----------+
4 rows in set (0.00 sec)set (0.00 sec)
+-------+-----------+---------+------+------+--------+
| pnrno | ticnumber | pname | age | Gender | ppno |
+-------+-----------+---------+------+------+--------+
| 1001 | 1111 | Alekhya | 25 | f | pp111 |
| 1002 | 2222 | krupani | 26 | f | pp2222 |
| 1003 | 3333 | pratima | 28| f | pp3333 |
| 1004 | 4444 | prem | 28 | m | pp444 4 |
+-------+-----------+---------+------+------+--------+
4 rows in set (0.03 sec)
23
mysql> select *from reservation;
+-----------+-----------+--------------------+------------+--------+
| pnrnumber | noofseats | address | phno | status |jour_date|
+-----------+-----------+--------------------+------------+--------+----------+
| 2001 | 4 | hno:101,msrd | 9999911111 | yes |2010-10-08|
| 2001 | 6 | hno:102,msrd | 9999900000| yes |2010-10-08|
| 2002 | 5 | hno:101,chpy | 9000011111 | yes |2010-10-08|
| 2003 | 8 | hno:102,chpy | 9000000000 | no |2010-08-08|
+-----------+-----------+--------------------+------------+--------+----------+
4 rows in set (0.02 sec)
24
Use of DELETE command:
mysql>delete from branch where location =’sec’;
Query OK, 1 row affected (0.03 sec)
25
Week: 7
Querying: In this week you are going to practice the queries (along with sub queries)
using ANY, ALL, IN, EXISTS, NOT EXISTS, UNION, INTERSECT, Constraints etc.
4. Display the source and destination having journey time more than 10 hours.
26
5. Find the ticket numbers of passengers whose name starts with ‘A’ and ends with ‘H’.
mysql> select tktno from ticket where tktno=any(select tktno from passenger wher
e name like 'a%h');
Empty set (0.03 sec)
mysql> select tktno from ticket where tktno=any(select tktno from passenger wher
e name like 'a%h');
+-------+
| tktno |
+-------+
| 4444 |
+-------+
1 row in set (0.00 sec)
mysql> select name from passenger where age between 30 and 45;
+-------+
| name |
+-------+
| amith |
+-------+
1 row in set (0.00 sec)
mysql> select all name from passenger where name like 'a%';
+---------+
| name |
+---------+
| alekhya |
| amith |
+---------+
2 rows in set (0.00 sec)
27
9. Display the Bus numbers that travel on Sunday and Wednesday.
mysql> select busno from bus where busno in(select busno from ticket where dayof
week(jour_date)=1 or dayofweek(jour_date)=4);
Empty set (0.05 sec)
10. Display the details of passengers who are traveling either in AC or NON_AC.
mysql> select pnr_no,name,age,Gender from passenger where tktno in(select tktno fro
m ticket where busno in(select busno from bus where type='a/c' or type='non a/c'
));
+---------+-----------+--------+---------+
| pnr_no | name | age | Gender |
+--------+----------+------+------+
| 2001 | alekhya | 25 | f |
| 2002 | krupani | 26 | f |
| 2003 | prathima | 28 | f |
| 2004 | amith | 35 | m |
+--------+----------+------+------+
4 rows in set (0.00 sec)
28
Week: 8 & 9
You are going to practice the queries using Aggregate functions (COUNT, SUM, AVG, MAX
and MIN), GROUP BY, HAVING AND Creation of Views.
1. Write a query to display the information present in the Passenger and Cancellation
tables.
mysql> select pnr_no from passenger union select pnr_no from cancel;
+--------+
| pnr_no |
+--------+
| 2001 |
| 2002 |
| 2003 |
| 2004 |
+--------+
4 rows in set (0.00 sec)
2. Write a query to display the busnumber with source and destination available in
Roadway Travels.
mysql> select busno,src,dest from bus b ,ticket t where b.busno=t.busno group by busno;
+--------+--------+------+
| busno | source | dest |
+--------+--------+------+
| 1001 | hyd | delhi |
| 1001 | hyd | delhi |
| 1002 | hyd | bglore |
| 1002 | hyd | bglore |
| +--------+--------+-----+
4 rows in set (0.00 sec)
mysql> select count(jour_date) from ticket where busno in(select busno from bus
where busno='1111') and jour_date between '2010-10-04' and '2010-10-10';
+------------------+
| count(jour_date) |
+------------------+
| 0|
+------------------+
1 row in set (0.00 sec)
4. Find the ticket numbers booked for each PNR_no using Group By clause.
29
5. Find the distinct PNR numbers that are present.
6. Find the number of tickets booked for each bus with bustype where the number of
seats is greater than 1.
8. Write a query to count the number of tickets for the buses which traveled after the
date ‘2010-08-06’.
mysql> select busno,type,noofseats from bus b,reservation r,ticket t,passenger p
where b.busno=t.busno and t.tktno=p.tktno and p.pnr_no=r.pnr_no and status='yes
' and jour_date>'2010-02-02' group by tktno having count(*)>=1;
+--------+---------+--------------+
| busno | bustype | booked_seats |
+--------+---------+--------------+
| ap444 | nonac | 6|
| ap891 | nonac | 8|
| ap8830 | metro | 5|
+--------+---------+--------------+
3 rows in set (0.01 sec)
30
Creation of Views:
mysql> create view takes as
-> select tktno,name from ticket t,passenger p where t.tktno=p.tktno;
Query OK, 0 rows affected (0.44 sec)
Dropping of Views:
31
Week 10: TRIGGERS
In this week you are going to work on the triggers. Creation of insert trigger, delete
trigger, update trigger. Practice triggers using above database.
A Trigger is a named database object which defines some action that the database
should take when some databases related event occurs.
Triggers are executed when you issues a data manipulation command like INSERT,
DELETE, UPDATE on a table for which the trigger has been created. They are automatically
executed and also transparent to the user. But for creating the trigger the user must have the
CREATE TRIGGER privilege.
In this section we will describe you about the syntax to create and drop the triggers
and describe you some examples of how to use them.
CREATE TRIGGER:
By using above statement we can create the new trigger. The trigger can associate only
with the table name and that must be refer to a permanent table. Trigger_time means trigger
action time. It can be BEFORE or AFTER. It is used to define that the trigger fires before or
after the statement that executed it. Trigger_event specifies the statement that executes the
trigger. The trigger_event can be any of the DML Statement: INSERT, UPDATE, DELETE.
We can not have the two trigger for a given table, which have the same trigger action
time and event. For Instance : we cannot have two BEFORE INSERT triggers for same table.
But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table.
Trigger_statement have the statement that executes when the trigger fires but if you
want to execute multiple statement the you have to use the BEGIN…END compound
statement. We can refer the columns of the table that associated with trigger by using the
OLD and NEW .keyword. OLD.column_name is used to refer the column of an existing row
before it is deleted or updated and NEW.column_name is used to refer the column of a new
row that is inserted or after updated existing row.
In INSERT trigger we can use only NEW.column_name because there is no old row and
in a DELETE trigger we can use only OLD.column_name because there is no new row. But in
UPDATE trigger we can use both, OLD.column_name is used to refer the columns of a row
before it is updated and NEW.Column_name is used to refer the column of the row after it is
updated.
1. Update Trigger:
mysql> create trigger tri1 before update on reservation
-> for each row
-> begin
-> if new.noofseats>20 then
-> set new.noofseats=old.noofseats;
-> else
-> set new.noofseats=new.noofseats;
-> end if;
-> end//
Query OK, 0 rows affected (0.11 sec)
32
mysql> select *from reservation;
-> //
+--------+-----------+---------------------+--------------+------------+--------+
| pnr_no | noofseats | jour_date | address | phno | status|
+--------+-----------+---------------------+--------------+------------+--------+
| 2001 | 10 | 2010-10-08 00:00:00 | hno:101,msrd | 9999911111 | yes|
| 2002 | 6 | 2010-10-08 00:00:00 | hno:102,msrd | 9999900000 | yes|
| 2003 | 5 | 2010-08-08 00:00:00 | hno:101,chpy | 9000011111 | yes|
| 2004 | 8 | 2010-08-08 00:00:00 | hno:102,chpy | 9000000000 | no|
+--------+-----------+---------------------+--------------+------------+--------+
4 rows in set (0.00 sec)
2. Insert Trigger:
mysql> create trigger tri2 before insert on passenger
-> for each row
-> begin
-> if new.age>29 then
-> set new.ppno='pp4567';
-> else
-> set new.ppno=" ";
-> end if;
-> end//
Query OK, 0 rows affected (0.11 sec)
33
mysql> select *from passenger;
+--------+-------+----------+------+------+--------+
| pnr_no | tktno | name | age | Gender | ppno |
+--------+-------+----------+------+------+--------+
| 2001 | 1111 | alekhya | 25 | f | pp1111 |
| 2002 | 2222 | krupani | 26 | f | pp2222 |
| 2003 | 3333 | prathima | 28 | f | pp3333 |
| 2004 | 4444 | amith | 35 | m | pp4444 |
| 2005 | 5555 | prem | 27 | m | |
+--------+-------+----------+------+------+--------+
5 rows in set (0.00 sec)
3. Delete Trigger:
mysql> create trigger tri3 before delete on cancel
-> for each row
-> begin
-> insert into reservation values(old.pnr_no,old.noofseats,old.jour_date,old
.address,old.phno,old.status);
-> end//
Query OK, 0 rows affected (0.11 sec)
34
Week 11: Procedures
In this session you are going to learn Creation of stored procedures, execution of
procedure and modification of procedures. Practice the procedures using above
database.
35
Week 12: Cursors
In this week you need to do the following: Declare the cursor that defines the 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 are used when the SQL Select statement is expected to return more than one
row. Cursors are supported inside procedures and functions.
Cursors must be declared and its definition contains the query. The cursor must be
defined in the DECLARE section of the program. A cursor must be opened before processing
and closed after processing.
Syntax to declare the cursor:
DECLARE <cursor_name> CURSOR FOR <select_statement>
Multiple cursors can be declared in the procedures and functions but each cursor must
have a unique name. And in defining the cursor the select_statement cannot have INTO
clause.
36
Query OK, 0 rows affected (0.02 sec)
37