Lab Manual CS403P – Database Management
Systems (Practical)
Page
Lab No. Lab Topic
No.
1 Lab 1: Installation of Microsoft SQL Server 2
2 Lab 2: Context Level DFD (Data Flow Diagram) 23
3 Lab 3: Entity Relationship Diagram 25
4 Lab 4: Super type and subtype in the ERD (Entity Relationship Diagram) 27
5 Lab 5: Relational Database 28
6 Lab 6: ERD to RDM 29
7 Lab 7: Relational Algebra Operations 30
8 Lab 8: Normalization (2NF) 33
9 Lab 9: Normalization (3NF) 36
10 Lab 10: SQL Queries (Create and Insert) 38
11 Lab 11: SQL Queries 40
12 Lab 12: SQL Queries (Select, Group By and Having clause) 43
13 Lab 13: Joins 45
14 Lab 14: Indexes 47
15 Lab 15: Views 49
16 Lab 16: Views (Continued) 50
1|Page
Lab 1
Lab Title: Installation of Microsoft SQL Server
Problem Statement
You are required to install any version of Microsoft SQL Server in your system according to
your system and operating system requirements.
Installation Steps:
2|Page
3|Page
4|Page
5|Page
6|Page
7|Page
8|Page
9|Page
10 | P a g e
11 | P a g e
12 | P a g e
13 | P a g e
14 | P a g e
15 | P a g e
16 | P a g e
17 | P a g e
18 | P a g e
19 | P a g e
20 | P a g e
21 | P a g e
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
22 | P a g e
Lab 2
Lab Title: Context Level DFD (Data Flow Diagram)
Problem Statement
Lemon Soda Service
“Thela”, lemon soda offering company has decided to take orders online. By using this online
system customer can place the order and view receipt generated by system. Order is
prepared/processed and sent to delivery man. The delivery man will deliver order and receive
payment form the customer. After receiving payment, the delivery man can update status in the
system as payment received.
Identify the External Entities and processes from the above scenario and draw:
Context Level DFD
Level 0 DFD
Solution:
Context Level DFD
23 | P a g e
Level 0 DFD
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
24 | P a g e
Lab 3
Lab Title: Entity Relationship Diagram
Problem Statement
Lemon Soda Service
“Thela”, lemon soda offering company has decided to take orders online. Customer places the
order online and at the time of delivery pays the bill. Order is prepared and delivered by the
delivery man to the customer. Customer pays the bill to the delivery man.
Perform the following tasks on the given scenario:
Identify Entities.
Identify the attributes of the entities.
Identify the relationships between the entities.
Draw the Entity Relationship diagram from the information extracted in first three tasks.
25 | P a g e
Solution:
ERD:
Mechanism to Conduct Lab: Lab will be conducted via Google Meet / Zoom.
26 | P a g e
Lab 4
Lab Title: Super type and subtype in the ERD (Entity Relationship Diagram)
Problem Statement
Lemon Soda Service
“Thela”, lemon soda offering company has decided to take orders online. Customer places the
order online and at the time of delivery pays the bill. Order is prepared and delivered by the
delivery man to the customer. Customer pays the bill to the delivery man.
Customer and Delivery man both are the person. Introduce the concept of super type and subtype
in the ERD drawn in Lab 3.
Solution
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
27 | P a g e
Lab 5
Lab Title: Relational Database
Problem Statement
Consider the following relation scheme of “Candidate” entity in the domain of Election
Commission of Pakistan (ECP).
Candidate Scheme = (CandName : Text , Constituency : Text , Age : Number , Qualification :
Text )
You are required to perform the following task:
1. A database relation having 4 tuples.
2. Represent the database relation created in 1 as a two dimensional structure called Table.
Solution:
1. Database relation:
Candidate = { (Rana Maqsood, NA-45, 40, BA) , (Sohail Khan, NA-16, 55, MA), (Shiraz
Hussain Dar, NA-21, 37, MS) ,(Asad Ullah, NA-65, 35, M.Sc) }
2. Table
CandName Constituency Age Qualification
Rana Maqsood NA-45 40 BA
Sohail Khan NA-16 55 MA
Shiraz Hussain Dar NA-21 37 MS
Asad Ullah NA-65 35 M.Sc
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
28 | P a g e
Lab 6
Lab Title: ERD to RDM
Problem Statement
You are required to transform the following ERD (Entity Relationship Diagram) to Relational
Data model using the transforming/mapping rules taught in the video lectures and handouts. You
also have to show the primary keys and foreign keys.
ERD:
Solution:
Customer (CusID, CusName, CusAddress)
Order (OrderID, OrderDate, Details, CusID, EmpID)
Bill (BillID, TotalPrice, CusID, EmpD, OrderID)
DeliveryMan (EmpID, EmpName)
Mechanism to Conduct Lab: Lab will be conducted via Google Meet / Zoom.
29 | P a g e
Lab 7
Lab Title: Relational Algebra Operations
Problem Statement
Consider the following two relations:
Employee 1:
EmpName Designation Age Salary
Khalid Khan Director 38 65000
Tariq Khan Supervisor 27 16000
Sadiq Amin Account Officer 28 28000
Shabir Hussain Manager 35 40000
Tanveer Ahmad Admin Officer 30 30000
Farooq Clerk 25 12000
Employee 2:
EmpName Designation Age Salary
Farukh Lecturer 27 25000
Waqar Shah Assistant Professor 35 45000
Jibran Akhtar Network Admin 28 27000
Yasir Zaib HR Manager 35 42000
You are required to perform the following relational algebra operations.
1. σ Salary >20000 (Employee 1)
2. σ Salary <40000 (Employee 2)
30 | P a g e
3. Π EmpName, Salary (Employee 2)
4. Employee 1 U Employee 2
Solution:
1
EmpName Designation Age Salary
Khalid Khan Director 38 65000
Sadiq Amin Account Officer 28 28000
Shabir Hussain Manager 35 40000
Tanveer Ahmad Admin Officer 30 30000
2
EmpName Designation Age Salary
Farukh Lecturer 27 25000
Jibran Akhtar Network Adin 28 27000
3
EmpName Salary
Farukh 25000
Waqar Shah 45000
Jibran Akhtar 27000
Yasir Zaib 42000
4
31 | P a g e
EmpName Designation Age Salary
Khalid Khan Director 38 65000
Tariq Khan Supervisor 27 16000
Sadiq Amin Account Officer 28 28000
Shabir Hussain Manager 35 40000
Tanveer Ahmad Admin Officer 30 30000
Farooq Clerk 25 12000
Farukh Lecturer 27 25000
Waqar Shah Assistant Professor 35 45000
Jibran Akhtar Network Adin 28 27000
Yasir Zaib HR Manager 35 42000
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
32 | P a g e
Lab 8
Lab Title: Normalization (2NF)
Problem Statement
You have studied the concept of normalization (and different normal forms) in this course.
Following relation is already in First Normal Form. You are required to convert it into Second
Normal Form using the techniques you have studied so far.
EmpID Name DeptName salary CourseTitle DateCompleted
101 Latif Marketing 35000 Advertising 15/03/2010
101 Latif Marketing 35000 Survey 15/03/2010
103 Mukhtar Accounting 47000 MS Excel 10/06/2011
104 Zafar IT 57000 Oracle 18/04/2009
104 Zafar IT 57000 Java 14/09/2007
106 Nasir Finance 70000 Investment 19/08/2008
107 Ali Marketing 35000 Advertising 15/03/2010
107 Ali Marketing 35000 ecommerce 21/05/2012
The above relation contains different redundancies. The relation can be expressed in a shorthand
notation as follows:
EMPLOYEE (EmpID, Name, DeptName, Salary, CourseTitle, DateCompleted)
The above relation contains a composite primary key of EmpID and CourseTitle. The
functional dependencies in this relation are as follows:
EmpID Name, DeptName, Salary
EmpID, CourseTitle DateCompleted
33 | P a g e
Solution:
All the partially dependent attributes are removed and placed in another relation when a relation
is converted from 1NF to 2NF. The decomposition must satisfy one of the following conditions.
A relation will be in 2NF if any of the following conditions are met:
1. The primary key consists of only one attribute
2. No non-key attributes exist in the relation
3. Every non-key attribute is functionally dependent on the full set of primary key attributes.
The EMPLOYEE relation can be divided into two relations EMPLOYEE and COURSE as
follows:
EMPLOYEE (EmpID, Name, DeptName, Salary)
EmpID Name DeptName Salary
101 Latif Marketing 35000
103 Mukhtar Accounting 47000
104 Zafar IT 57000
106 Nasir Finance 70000
107 Ali Marketing 35000
COURSE: (EmpID, CourseTitle, DateCompleted)
EmpID CourseTitle DateCompleted
34 | P a g e
101 Advertising 15/03/2010
101 Survey 15/03/2010
103 MS Excel 10/06/2011
104 Oracle 18/04/2009
104 Java 14/09/2007
106 Investment 19/08/2008
107 Advertising 15/03/2010
107 ecommerce 21/05/2012
The primary key in EMPLOYEE relation consists of only one attribute. It satisfies the first
condition. The attribute DateCompleted in COURSE relation is fully functionally depends on
whole composite key. It satisfies the third condition. It means that both relations are in second
normal forms.
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
35 | P a g e
Lab 9
Lab Title: Normalization (3NF)
Problem Statement
Following relation is already in Second Normal Form. You are required to convert it into Third
Normal Form using the techniques you have studied so far.
CustomerID Name SalesMan Region
11 Hamza Khalid South
21 Shoaib Umair West
31 Shahbaz khalid South
44 Salman Zulfiqar East
51 Rizwan Umair West
61 Farooq Uzair North
The relation can be expressed in a shorthand notation as follows:
SALES (CustomerID, Name, SalesMan, Region)
The attribute CustomerID is used as primary key. Each salesman is assigned a unique region.
The above relation contains the following functional dependencies:
CustomerID Name, SalesMan
SalesMan Region
The above relation is in 2NF because the primary key consists of single attribute. A transitive
dependency exists in the relation. The Region (a non-key attribute) is dependent on another non-
key attribute (SalesMan) and SalesMan is functionally dependent on CustomerID. It means that
region is transitively dependent on CustomerID.
Solution:
Transitive dependency exists if a non-key attribute depends on any other non-key attribute.
36 | P a g e
The transitive dependency can be removed by decomposing the above relation into two relations
as follows:
CustomerID Name SalesMan
11 Hamza Khalid
21 Shoaib Umair
31 Shahbaz khalid
44 Salman Zulfiqar
51 Rizwan Umair
61 farooq Uzair
SALES (CustomerID, Name, SalesMan)
SalesMan Region
Khalid South
Umair West
Zulfiqar East
Uzair North
SALESMAN (SalesMan, Region)
Both relations are now in 3NF. There is no transitive dependency in these relations.
The determinant attribute in transitive dependency becomes the primary key in SALESMAN
relation.
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
37 | P a g e
Lab 10
Lab Title: SQL Queries (Create and Insert)
Problem Statement
Consider the following tables:
Teacher:
TeacherID Name Office Phone Salary Email
Waheed
Masood
T003 Dr. Farhana Khan F001 9222 125000 [email protected]
Class:
ClassID CourseCode DayofWeek StartTime EndTime Venue
A001 CS403 Monday 09:00 AM 10:00 AM A Block
B002 CS301 Tuesday 09:00 AM 10:00 AM B Block
C003 CS201 Monday 10:30 AM 12:00 PM C Block
Consider the above given tables and perform the following Tasks;
1. Write the query to create the above tables in SQL Server.
2. Insert the records given in the above tables.
Solution:
CREATE TABLE Teacher(
38 | P a g e
TeacherID varchar(5) primary key,
Name varchar(30),
Office vrchar(10),
Phone smallint,
Salary int,
Email varchar(20)
);
CREATE TABLE Class(
ClassID varchar(5) primary key,
CourseCode varchar(10),
DayofWeek varchar(10),
StartTime time,
EndTime time,
Venue varchar(10)
);
INSERT INTO Teacher(TeacherID, Name, Office, Phone, Salary, Email)
VALUES('T001','Dr. Najma Waheed','F001', 9125, 150000,'
[email protected]');
INSERT INTO Teacher(TeacherID, Name,Office, Phone, Salary, Email)
VALUES('T002','Dr. Nouman Masood','F003', 5291, 175000,'
[email protected]');
INSERT INTO Teacher(TeacherID, Name, Office, Phone, Salary, Email)
VALUES('T003','Dr. Farhana Khan','F001', 9222, 125000,'
[email protected]');
INSERT INTO class(ClassID, CourseCode, DayofWeek, StartTime, EndTime, Venue)
VALUES('A001','CS403','Monday', '09:00:00', '10:00:00','A Block');
INSERT INTO class(ClassID, CourseCode, DayofWeek, StartTime, EndTime, Venue)
VALUES('B002','CS301','Tuesday', '09:00:00', '10:00:00','B Block');
INSERT INTO class(ClassID, CourseCode, DayofWeek, StartTime, EndTime, Venue)
VALUES('C003','CS201','Monday', '10:30:00', '12:00:00','C Block');
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
39 | P a g e
Lab 11
Lab Title: SQL Queries
Problem Statement
Given are the tables of Teacher and Class:
TeacherID Name Office Phone Email
Masood
Ahmad
Raheem
ClassID CourseCode DayofWeek TimeStart TimeEnd Venue TeacherID
A001 CS403 Monday 09:00 AM 10:00 AM A Block T003
B002 CS301 Tuesday 09:00 AM 10:00 AM B Block T005
A001 CS304 Tuesday 14:00 PM 15:00 PM A Block T002
C003 CS201 Monday 10:30 AM 12:00 PM C Block T006
C003 CS301 Friday 12:00 PM 13:00 PM C Block T005
B005 CS403 Friday 12:30 PM 14:00 PM B Block T003
40 | P a g e
Consider the above given tables and perform the following Tasks;
1. Display all the records of the class whose start time is between 09:00 and 12:00
2. Retrieve the details of teachers with first name Dr.
3. On which days classes of CS403 are scheduled at 09:00 AM?
4. List down the Subjects which are taught in B Block.
5. Select the course code from Class table, avoiding the duplicate values.
6. Display all the records of the class whose start time is 09:00 or 12:00 and end time is less
than 14:00
Solution:
1:
Select *
From Classes
Where StartTime Between '09:00' and '12:00'
2:
Select * from Teacher
Where Name Like 'Dr.%'
3:
Select DayofWeek
From Classes
Where CourseCode='CS403' AND StartTime='09:00'
4:
Select CourseCode
From Classes
Where Venue='B Block'
41 | P a g e
5:
Select distinct CourseCode
From Classes
6:
Select *
From Classes
Where (StartTime='09:00' OR StartTime='12:00') AND EndTime<'14:00'
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
42 | P a g e
Lab 12
Lab Title: SQL Queries (Select, Group By and Having clause)
Problem Statement
Teacher:
TeacherID Name Office Phone Salary Email
Waheed
Masood
T003 Dr. Farhana Khan F001 9222 125000 [email protected]
Class:
ClassID CourseCode DayofWeek StartTime EndTime Venue
A001 CS403 Monday 09:00 AM 10:00 AM A Block
B002 CS301 Tuesday 09:00 AM 10:00 AM B Block
C003 CS201 Monday 10:30 AM 12:00 PM C Block
Consider the above given tables and perform the following Tasks;
1. Count the number of teachers
2. Count the unique courses from Classes table
3. List subject-wise classes according to earlier end time.
4. Calculate the average salary of the teachers sitting in the same office with office not null.
43 | P a g e
Solution:
1:
Select count(*) from Teacher;
2:
Select count(distinct CourseCode) As All_Courses
from Class;
3:
Select Min(EndTime) As Late_Classes
from Class
GROUP BY CourseCode;
4:
Select AVG(Salary)
From Teacher
GROUP BY Office
Having Office IS NOT NULL;
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
44 | P a g e
Lab 13
Lab Title: Joins
Problem Statement
TeacherID Name Office Phone Email
Masood
Ahmad
Raheem
ClassID CourseCode DayofWeek TimeStart TimeEnd Venue TeacherID
A001 CS403 Monday 09:00 AM 10:00 AM A Block T003
B002 CS301 Tuesday 09:00 AM 10:00 AM B Block T005
C003 CS201 Tuesday 14:00 PM 15:00 PM A Block T002
D004 CS501 Monday 10:30 AM 12:00 PM C Block T006
E005 CS401 Friday 12:00 PM 13:00 PM C Block T005
F006 CS402 Friday 12:30 PM 14:00 PM B Block T003
45 | P a g e
Consider the above given tables and apply the following types of joins:
1. Inner join
2. Full Outer join
3. Right Outer join
4. Left Outer join
Solution:
1. SELECT * FROM Teacher INNER JOIN Class ON
Teacher.TeacherID=Class.TeacherID;
2. SELECT * FROM Teacher FULL OUTER JOIN Class ON
Teacher.TeacherID=Class.TeacherID;
3. SELECT * FROM Teacher RIGHT OUTER JOIN Class ON
Teacher.TeacherID=Class.TeacherID;
4. SELECT * FROM Teacher LEFT OUTER JOIN Class ON
Teacher.TeacherID=Class.TeacherID;
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
46 | P a g e
Lab 14
Lab Title: Indexes
Problem Statement
Consider the following table:
TEACHER:
TeacherID Name Office Phone Email
Masood
Ahmad
Raheem
You are required to perform the following tasks:
1. Write SQL query to create a UNIQUE INDEX on the “TeacherID” column of the given
table in Descending order.
2. Write SQL query to create a UNIQUE INDEX on the “Name” and “Office” columns of
the given table in ascending order.
47 | P a g e
Solution:
CREATE UNIQUE INDEX Teacher_Index1
On TEACHER (TeacherID DESC);
CREATE UNIQUE INDEX Teacher_Index2
On TEACHER (Name, Office ASC);
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
48 | P a g e
Lab 15
Lab Title: Views
Problem Statement
Class:
ClassID CourseCode DayofWeek TimeStart TimeEnd Venue TeacherID
A001 CS403 Monday 09:00 AM 10:00 AM A Block T003
B002 CS301 Tuesday 09:00 AM 10:00 AM B Block T005
C003 CS201 Tuesday 14:00 PM 15:00 PM A Block T002
D004 CS501 Monday 10:30 AM 12:00 PM C Block T006
E005 CS401 Friday 12:00 PM 13:00 PM C Block T005
F006 CS402 Friday 12:30 PM 14:00 PM B Block T003
You are required to write an SQL query to create a “View” on this table having ClassID,
CourseCode and Venue columns. Furthermore, the view would have information for only those
classes conducted in the “A Block” venue.
Solution:
CREATE VIEW Class_View AS
SELECT ClassID, CourseCode, Venue
FROM CLASS
WHERE Venue='A Block';
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
49 | P a g e
Lab 16
Lab Title: Views (Continued)
Problem Statement
Consider the following table:
Products:
ProductName Purchase_Price Sale_Price Stock_Qty
Bread 20 25 50
French Pastry 17 25 35
Slice 20 25 20
Large Cake 50 60 30
Dairy Cream Cake 40 47 40
Pastry 10 15 70
Biscuit 15 20 45
Rolls 25 30 65
Lays Chips (small size) 18 20 115
Lays Chips (Medium size) 47 50 100
Lays Chips (Large size) 92 100 80
You are required to write an SQL query to create a “View” on this table having ProductName,
Sale_Price and Stock_Qty columns. Furthermore, the view would have information for only those
products having Sale price greater than the average sale price in the given table.
50 | P a g e
Solution:
CREATE VIEW Product_View AS
SELECT ProductName, Sale_Price, Stock_Qty
FROM Products
WHERE Sale_Price > (SELECT AVG(Sale_Price) FROM Products) ;
Mechanism to Conduct Lab:
Lab will be conducted via Google Meet / Zoom.
51 | P a g e