Professional Documents
Culture Documents
Course Objectives:
Introduce ER data model, database design and normalization Learn SQL basics
for data definition and data manipulation
Course Outcomes:
Design database schema for a given application and apply normalization
Acquire skills in using SQL commands for data definition and data
manipulation.
Develop solutions for database applications using procedures, cursors and
triggers
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
1
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
Analyze the problem carefully and come up with the entities in it. Identify what data 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.
E-R Model:
Entity set: It is a collection of all entities of particular entity type in the database.
Extension of entity type: The collections of entities of a particular entity type are grouped
together into an entity set.
Attribute: It is a particular property, which describes the entity.(or) Each entity &
relationship has a property called Attributes.
The difference symbols which are used to draw E-R models:
2. Ellipse (Attributes)
Super Key:
A Super Key of an entity set is a set of one (or) more attributes whose values uniquely
determine each entity.
Candidate Key:
A Candidate Key of an entity set is a minimal super key. For example:
1) Customer_Id is Candidate Key of Customer
2) Account_ Number is Candidate Key of Account
Primary Key:
Although several Candidate keys may exist, one of the Candidate keys is selected to be the
Primary Key.
Partial Key:
It is a set of attributes that can uniquely identify weak entities and that are related to same
owner entity. It is sometime called as Discriminator.
The entities for the above company named “Roadway Travels” are as follows
1. Bus
2. Ticket
3. Passenger
4. Reservation
5. Cancellation
Now we will identify the attributes for each entity and construct the E-R Model.
3
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
1. BUS Entity:
i. Bus_No
ii. Source
iii. Destination
iv. Dep_Time
Dep_Tim Destination
e
Bus_No Source
BUS
2. TICKET Entity:
The attributes are:
i. Ticket_No
ii. Journey_Date
iii. Age
iv. Sex
v. Source
vi. Destination
vii. Dep_Time
Sex
4
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
Age Sourc
e
Journey_Dat Destinatio
e n
Ticket_No Dep_Tim
e
TICKET
3. PASSENGER Entity:
Nam Age
e
Ticket_No Sex
PNR_No PPNo
PASSENGER
In the above figure PNR_No is the Primary Key and Ticket_No is Foreign Key
4. RESERVATION Entity:
The attributes are:
i. PNR_No
ii. Journey_Date
iii. No_Of_Seats
iv. Address
v. Contact_No
vi. Status
No_OF_Sea Addres
5
ts s
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
Journey_Dat Contact_N
e o
Status
PNR_No
RESERVATION
5. CANCELLATION Entity:
i. PNR_No
ii. Journey_Date
iii. No_Of_Seats
iv. Address
v. Contact_No
vi. Status
No_OF_Sea Addres
ts s
Journey_Dat Contact_N
e o
Status
PNR_No
CANCELLATION
6
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
Relate the entities appropriately. Apply cardinalities for each relationship. Identify the strong entities and
weak entities (if any). Indicate the type of relationships (total/partial). Try to incorporate generalization,
aggregation, specialization etc… wherever required.
The following diagram depicts the relationship exists between BUS Entity and PASSENGER Entity.
Dep_Tim Destination
e
Bus_No Source
BUS
Travel
in
PASSENGER
PNR_No PPNo
Ticket_No Sex
Nam Age
e
The relationship between BUS Entity and PASSENGER Entity is strong relationship. Because the two
entities consists of primary keys. Here the two entities are strong entities. So the relationship is strong
relationship
The following diagram depicts the relationship exists between PASSENGER Entity and
7
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
RESERVATION Entity.
Nam Age
e
Ticket_No Sex
PNR_No PPNo
PASSENGER
Takes
RESERVATION
PNR_No Statu
s
Journey_Date Contact_N
o
No_Of_Seat Addres
s s
The relationship between PASSENGER Entity and RESERVATION Entity is weak relationship. Because
the one entity consists of primary key and another entity consists of foreign key. Here the one entity is
strong entity (single border rectangle) and another entity is weak entity (double border rectangle). The
relationship between strong entity and weak entity is weak relationship.
8
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
The following diagram depicts the relationship exists between PASSENGER Entity and
CANCELLATION Entity.
Nam Age
e
Ticket_No Sex
PNR_No PPNo
PASSENGER
Cancel
s
CANCELLATION
PNR_No Status
Journey_Date Contact_N
o
No_Of_Seat Addres
s s
The relationship between PASSENGER Entity and CANCELLATION Entity is weak relationship.
Because the one entity consists of primary key and another entity consists of foreign key. Here the one
9
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
entity is strong entity (single border rectangle) and another entity is weak entity (double border rectangle).
The relationship between strong entity and weak entity is weak relationship.
Represent all the entities (Strong, Weak) in tabular fashion. Represent relationships in a tabular fashion.
There are different ways of representing relationships as tables based on the cardinality. Represent
attributes as columns in tables or as tables based on the requirement. Different types of attributes
(Composite, Multivalve and Derived) have different way of representation.
Relationship:-
It is defined as an association among several entities. A relationship can be one-to- one, one-to-many (or)
many-to-many.
A Collection of similar relationships is called a relationship set and is denoted by a Rhombus.
• Unary Relationship---Where the association is within a single entity
• Binary Relationship---A relationship that associates two entities
• Ternary Relationship---A relationship that association three entities
• Quaternary Relationship—A relationship that associates with four entities
Attribute: It is a particular property, which describes the entity. (or) Each entity & relationship has a property
called Attributes.
1) Simple Attribute -- These attributes are also called as atomic attribute. These cannot be subdivided further.
2) Composite Attributes -- An attribute which can be further divided into smaller components are called
Composite Attribute.
3) Single-Valued Attributes -- Certain Attribute take only a single value in all instances.
4) Multi-Valued Attributes -- Attributes that can have more than one value at a time for an instance.
5) Stored & Derived Attributes -- Some attributes need not be stored but can be derived from available other
attributes.
The following are the tabular representations of the entities used in this company.
11
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
12
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
WEEK 4: NORMALIZATION
Database Normalization is a technique for designing relational database tables to minimize duplication of
information and, in doing, to safeguard the database against certain types of logical or structural problems,
namely data anomalies. For example, when multiple instances of a given piece of information occur in a
table, the possibility exists that these instances will not be kept consistent when the data within the table is
updated, leading to a loss of data integrity. A table that is sufficiently normalized is less vulnerable to
problems of this kind. Because its structure reflects the basic assumptions for when multiple instances of
the same information should be represented by a single instance only.
Normalization: Database designed based on the E-R model may have some amount of inconsistency,
ambiguity and redundancy. To resolve these issues some amount of refinement is required. This
refinement process is called normalization.
Let us now consider the “transportation table”.
BUS
Bus_no Source Destination Dep_Time
1234 Anantapur Kurnool 10:30am
4567 Kadapa Hyderabad 10:30pm
1245 Hyderabad Chennai 07:00pm
3467 Bangalore Hyderabad 12:30pm
13
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
TICKET
Ticket_No Journey_Date Age Sex Source Destination Dep_Time
2345 20-08-2016 25 F Anantapur Kurnool 10:30am
5678 25-08-2016 20 M Hyderabad Chennai 07:00pm
2457 30-08-2016 32 M Bangalore Hyderabad 12:30pm
4679 05-09-2016 28 F Kadapa Hyderabad 10:30pm
PASSENGER
PNR_No Ticket_No Name Age Sex PPNo
1 2345 Varuni 25 F 9003345678
2 5678 Rahul 20 M 9247212345
3 2457 Ajay 32 M 9989892731
4 4679 Suni 28 F 9885252069
RESERVATION
PNR_No Journey_Date No_of_seats Address Contact_No Status
1 20-08-2016 2 Anantapur 9003345678 Yes
3 30-08-2016 1 Bangalore 9989892731 No
2 25-08-2016 2 Hyderabad 9885252069 Yes
4 05-09-2016 2 Kadapa 9247212345 No
CANCELLATION
PNR_No Journey_Date No_of_seats Address Contact_No Status
3 30-08-2016 1 Bangalore 9989892731 No
The above table shows the data file of the transportation in a table format. To reduce the anomalies
normalization is applied
A relation R is said to be in the first normal form (1NF) if and only if all the attributes of the relation R are
atomic in nature.
Consider the Transportation table to reproduce the table. The following table shows the table in 1NF.
BUS
Bus_no Source Destination Dep_Time
1234 Anantapur Kurnool 10:30am
4567 Kadapa Hyderabad 10:30pm
1245 Hyderabad Chennai 07:00pm
3467 Bangalore Hyderabad 12:30pm
TICKET
Ticket_No Journey_Date Age Sex Source Destination Dep_Time
2345 20-08-2016 25 F Anantapur Kurnool 10:30am
5678 25-08-2016 20 M Hyderabad Chennai 07:00pm
2457 30-08-2016 32 M Bangalore Hyderabad 12:30pm
4679 05-09-2016 28 F Kadapa Hyderabad 10:30pm
14
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
PASSENGER
PNR_No Ticket_No Name Age Sex PPNo
1 2345 Varuni 25 F 9003345678
2 5678 Rahul 20 M 9247212345
3 2457 Ajay 32 M 9989892731
4 4679 Suni 28 F 9885252069
RESERVATION
PNR_No Journey_Date No_of_seats Address Contact_No Status
1 20-08-2016 2 Anantapur 9003345678 Yes
3 30-08-2016 1 Bangalore 9989892731 No
2 25-08-2016 2 Hyderabad 9885252069 Yes
4 05-09-2016 2 Kadapa 9247212345 No
CANCELLATION
PNR_No Journey_Date No_of_seats Address Contact_No Status
3 30-08-2016 1 Bangalore 9989892731 No
In the new form, all the attributes are atomic, meaning that they are not further decomposable.
Key Attributes:
In a given relationship if the attribute X uniquely defines all other attributes, then the attribute
X is a key attribute.
Non-Key Attributes:
In a given relationship R, all the attributes which are not key attributes are called non-key
To make the table 2NF we have to remove all the partial dependencies
15
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
BUS
Bus_no Source Destination Dep_Time
1234 Anantapur Kurnool 10:30am
4567 Kadapa Hyderabad 10:30pm
1245 Hyderabad Chennai 07:00pm
3467 Bangalore Hyderabad 12:30pm
TICKET
Ticket_No Journey_Date
2345 20-08-2016
5678 25-08-2016
2457 30-08-2016
4679 05-09-2016
PASSENGER
PNR_No Name Age Sex PPNo
1 Varuni 25 F 9003345678
2 Rahul 20 M 9247212345
3 Ajay 32 M 9989892731
4 Suni 28 F 9885252069
RESERVATION
PNR_No Journey_Date No_of_seats Address Contact_No Status
1 20-08-2016 2 Anantapur 9003345678 Yes
3 30-08-2016 1 Bangalore 9989892731 No
2 25-08-2016 2 Hyderabad 9885252069 Yes
4 05-09-2016 2 Kadapa 9247212345 No
CANCELLATION
PNR_No Journey_Date No_of_seats Address Contact_No Status
3 30-08-2016 1 Bangalore 9989892731 No
BUS
Bus_no Source Destination Dep_Time
1234 Anantapur Kurnool 10:30am
16
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
TICKET
Ticket_No Journey_Date
2345 20-08-2016
5678 25-08-2016
2457 30-08-2016
4679 05-09-2016
PASSENGER
PNR_No Name Age Sex PPNo
1 Varuni 25 F 9003345678
2 Rahul 20 M 9247212345
3 Ajay 32 M 9989892731
4 Suni 28 F 9885252069
RESERVATION
PNR_No Journey_Date No_of_seats Address Contact_No Status
1 20-08-2016 2 Anantapur 9003345678 Yes
3 30-08-2016 1 Bangalore 9989892731 No
2 25-08-2016 2 Hyderabad 9885252069 Yes
4 05-09-2016 2 Kadapa 9247212345 No
Here the reservation table is split into two tables in order to remove transitive dependency. The following
tables shows the 3rd normal form.
CANCELLATION
PNR_No Journey_Date No_of_seats Address Contact_No Status
3 30-08-2016 1 Bangalore 9989892731 No
17
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
Here the cancellation table is split into two tables in order to remove transitive dependency. The following
tables shows the 3rd normal form.
18
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
Installation of MySQL. In this week you will learn creating databases. How to create table altering
the database, dropping table and databases if not required. You will also try truncate, rename
commands etc…
Create table passenger (passport id Integer primary key, name char(50) null, age
integer, Sex char);
19
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
7. Destroying tables. Drop command is used to destroy the existing table (or) a view.
Syntax: Drop table <tablename>;
Ex: drop table emp;
BUS:
SQL> CREATE TABLE BUS (BusNo varchar (10) primary key, Source varchar (15),
Destination varchar (15));
PASSENGER:
SQL> CREATE TABLE Passenger(PNR_No NUMERIC (9) primary key, Ticket_No
Numeric(9),Name varchar (15),Age number(4),Sex Char(10),PPNO varchar (15));
CANCELLATION:
20
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
21
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
DML Commands are used to for managing data within schema objects. Some examples: SELECT
Retrieve data from the database
DELETE – Deletes all records from a table, the space for the records remain
1. SELECT Command:
The SELECT Command is used to retrieve data from the database. The syntax of the command is as follows.
SELECT A1,A2,……,An
FROM Tablename
WHERE P;
Example:
2.INSERT Command:
The INSERT Command is used to insert data into a relation, we either specify a tuple to be inserted or write a
query whose result is a set of tuples to be inserted. The syntax is as follows.
To insert a single value into the table the following syntax is used.
To insert the multiple values into the table the following syntax is used.
SQL> INSERT INTO tablenameVALUES(&value1,&value2,………,valueN);
Example:
22
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
3. UPDATE Command:
UPDATE Command is used to update the existing data within the table. In certain situations we may wish to
change a value in a tuple (row) without changing all values in the table. For this purpose, the update statement
can be used. The syntax of the command is as follows.
Example:
4. DELETE Command:
DELETE Command is used to delete all records from a table, the space for the records remain. The syntax of
the command is as follows.
Example:
23
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
WEEK 7: Querying
In this week you are going to practice queries (along with sub queries) using ANY, ALL,
5. Find the ticket numbers of the passengers whose name starts with ‘A’ and ends with
‘A’.
24
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
SQL>SELECT Name FROM Passenger WHERE Age >=30 and Age <=50;
8. Display the source and the destination having journey time more than 10 hours.
9. Display the details of passengers who are travelling either in A/c or Non-A/c (using in operator).
You are going to practice queries using Aggregate Functions (COUNT, SUM, AVG, MAX and MIN),
GROUP BY, HAVING and Creation and Dropping of Views.
1. Write a query to display the information present in the passenger and cancellation tables (using
UNION operator).
2. Display the number of days ina week on which the 9wo1 bus is available.
4. Find the number of tickets booked for each pnr_no using GROUP B CLAUSE (use
GROUP BY on pnr_no).
5. Write a query to count the number of tickets for the buses , which travelled after the
25
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
7. Find the number of tickets booked in each class where the number of seats is greater
In this week you are going to work on Triggers. Creation of Insert Trigger, Delete Trigger,
DELIMITER //
CREATE TRIGGER insert_trig BEFORE INSERT ON Passenger
FOR EACH ROW
BEGIN
IF NEW.Age<0 THEN
SET NEW.Age=0;
END IF;
END //
DELIMITER ;
26
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
In this session you are going to learn Creation of stored procedure, execution of procedure and
modification of procedure. Practice procedures using above database.
DELIMITER / /
CREATE PROCEDURE p1 ()
BEGIN
SELECT Ticket_No, Name, Ph_no FROM Passenger WHERE No_of_seats>2;
END //
DELIMITER ;
DELIMITER //
CREATE PROCEDURE p2 (IN n varchar (20))
BEGIN
SELECT Ticket_No, PNR_No, Age, Ph_no FROM Passenger WHERE Name=n;
END //
DELIMITER ;
27
B.TECH :III / I
DATABASE MANAGEMENT SYSTEMS LAB
28
B.TECH :III / I
WEEK 12: Cursors
In this week you need to do the following: Declare a cursor that defines the result set. Open
the cursor
that establishes the result set. Fetch the data into local variables as needed from the cursor,
one row at a
time. Close the cursor when done.
Write a program for declare a cursor that defines a resultset .Open the cursor to establish the
resultset.
Fetch the data into local variable as needed from the cursor ,one row at a time.Close the
cursor when
done.
DELIMITER //
CREATE PROCEDURE not_reserved ()
BEGIN
DECLARE pnr, seats, pno numeric;
DECLARE jdate date;
DECLARE address, stat varchar (20);
CREATE cur CURSOR FOR SELECT * FROM Reservation_details WHERE Status=
‘No’;
OPEN cur;
FETCH cur INTO pnr, jdate, seats, address, pno, stat;
SELECT , jdate, seats, address, pno, stat;
CLOSE cur;
END //
DELIMITER ;
Additional Programs
19.1 IMPLEMENTATION OF DATA AND BUILT IN FUNCTIONS IN SQL
CHARACTER/STRING FUNCTION: