dbms5-6LAB
dbms5-6LAB
Lab Session 5
Objective:
● Understand the concept and significance of database normalization.
● Analyze unnormalized tables to identify issues such as redundancy and anomalies.
● Apply normalization techniques to transform tables into 1NF and 2NF.
● To learn and apply normalization techniques to achieve Third Normal Form (3NF) by
eliminating transitive dependencies.
● Apply Boyce-Codd Normal Form (BCNF) by ensuring every determinant is a candidate
key.
● Use SQL queries in MS SQL Server to implement normalization and manage normalized
tables.
Introduction:
Database normalization is a systematic approach to organizing a relational database to reduce
redundancy and improve data integrity. It involves dividing large tables into smaller,
interconnected ones while adhering to specific design principles.
Functional Dependencies
● Key Attribute: The attribute or a combination of attributes on the left-hand side of the
functional dependency is called the determinant.
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
Example:
Consider a table of employees:
Here:
Role in Normalization:
Functional dependencies help identify the relationships between attributes and serve as the basis
for decomposing tables during normalization. By eliminating unwanted dependencies,
normalization improves the structure and integrity of the database.
Why Normalize?
Normalization Forms:
Normalization progresses through stages, each addressing specific issues:
1. First Normal Form (1NF): Ensures that each field contains atomic (indivisible) values
and eliminates repeating groups.
2. Second Normal Form (2NF): Eliminates partial dependencies, where non-primary
attributes depend on part of a composite primary key.
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
Procedure:
Example Problem Scenario
Consider the following Unnormalized Table (UNF):
1. Repeating groups: The same Order_ID appears multiple times with different products
(Product_ID).
2. Redundant data: Supplier details (Supplier_Name, Supplier_Contact) are repeated
for every product.
3. Anomalies:
○ Update Anomaly: Changing the supplier's contact information requires updates
in multiple rows.
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
○ Deletion Anomaly: Deleting all products from a supplier results in losing
supplier details.
○ Insertion Anomaly: Adding a new supplier without an order is impossible.
Order_ID INT,
Customer_Name VARCHAR(50),
Product_ID VARCHAR(10),
Product_Name VARCHAR(50),
Quantity INT,
Supplier_Name VARCHAR(50),
Supplier_Contact VARCHAR(15)
);
VALUES
1NF Table:
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
Explanation of Changes:
Order_ID INT,
Customer_Name VARCHAR(50),
Product_ID VARCHAR(10),
Product_Name VARCHAR(50),
Quantity INT,
Supplier_Name VARCHAR(50),
Supplier_Contact VARCHAR(15)
);
VALUES
2NF Tables:
Orders Table:
Products Table:
Explanation of Changes:
Customer_Name VARCHAR(50)
);
INSERT INTO Orders (Order_ID, Customer_Name) VALUES (001, 'John Doe'), (002, 'Jane
Smith');
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
OrderDetails Table
Order_ID INT,
Product_ID VARCHAR(10),
Quantity INT,
);
VALUES (001, 'P101', 2), (001, 'P102', 1), (002, 'P101', 1);
Products Table
Product_Name VARCHAR(50),
Supplier_Name VARCHAR(50)
);
Suppliers Table
Supplier_Contact VARCHAR(15)
);
Functi
onal Dependencies:
● StudentID StudentName .
● CourseID CourseName, InstructorName .
Issues:
Functional Dependencies:
● EmployeeID DepartmentID .
● DepartmentID DepartmentName, ManagerID .
Issues:
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
● DepartmentID ManagerID violates BCNF because DepartmentID is not a
candidate key.
CLASS TASKS:
Analyze the given unnormalized table and identify the redundancies present in it. Discuss how
these redundancies can lead to data anomalies.
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
"1234567890, 0987654321" or
Multiple contact numbers and
Repeating Groups "John Smith (CS101), Maria
instructors listed in the same cell.
Khan (CS102)"
CourseEnrollments and
Multi-valued
InstructorDetails hold multiple values CS101, CS102
Attributes
in a single column.
You want to add a new course Cannot store course unless it's
Insertion Anomaly
before any student enrolls. linked to a student.
If Ali Ahmed is deleted, and he’s the Course and instructor data for
Deletion Anomaly
only student in CS102. CS102 may also be lost.
1 CS101
1 CS102
2 CS101
2 CS103
3 CS102
3 CS103
StudentID INT,
StudentName VARCHAR(50),
ContactNumber VARCHAR(15),
CourseCode VARCHAR(10),
InstructorName VARCHAR(50)
);
● Take the 1NF table created in the previous step and normalize it to Second Normal Form
(2NF).
● Ensure there are no partial dependencies by creating new tables if necessary.
2NF (Second Normal Form):
StudentI StudentNa
D me
1 Ali Ahmed
2 Sara Malik
3 Ahmed Raza
StudentName VARCHAR(50)
);
StudentI ContactNumb
D er
1 1234567890
1 0987654321
2 9876543210
2 1122334455
3 5556667778
3 6667778889
ContactNumber VARCHAR(15)
);
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
Task 4:
Write a query to create and populate the original StudentCourse table in SQL Server.
StudentID INT,
StudentName VARCHAR(50),
ContactNumbers VARCHAR(100),
CourseEnrollments VARCHAR(100),
InstructorDetails VARCHAR(200)
);
(1, 'Ali Ahmed', '1234567890, 0987654321', 'CS101, CS102', 'John Smith (CS101), Maria Khan
(CS102)'),
(2, 'Sara Malik', '9876543210, 1122334455', 'CS101, CS103', 'John Smith (CS101), David Brown
(CS103)'),
(3, 'Ahmed Raza', '5556667778, 6667778889', 'CS102, CS103', 'Maria Khan (CS102), David Brown
(CS103)');
Task 5:
Write queries to create and populate the normalized Student and Course tables from the
StudentCourse table in SQL Server.
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
FROM StudentCourseOriginal;
Task 6:
Write queries to create and populate the normalized Employee and Department tables from the
EmployeeDepartment table in SQL Server.
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
EmployeeID INT,
EmployeeName VARCHAR(50),
DepartmentName VARCHAR(50)
);
EmployeeID INT,
EmployeeName VARCHAR(50),
DepartmentName VARCHAR(50)
);
HOME TASKS:
● Create an unnormalized table similar to the one provided (e.g., "Employee Projects").
● Normalize it up to Second Normal Form (2NF) and submit both 1NF and 2NF tables.
Let’s say we have the following unnormalized table that tracks employees and the projects they’re working on.
Database Management
EmpID EmpName System-LContactNumbers
Department Projects Lab Session 05
Iqra University Department of Software Engineering
1 Ayesha Khan IT 03211234567,03001122334 P101-Website Redesign, P102-Testing
EmpName VARCHAR(100),
Department VARCHAR(50)
);
EmpID INT,
ContactNumber VARCHAR(15),
);
ProjectName VARCHAR(100)
);
EmpID INT,
ProjectCode VARCHAR(10),
● Identify a real-world dataset or scenario from your surroundings (e.g., library records,
grocery store inventory).
● Design an unnormalized table for it and outline the process to normalize it to 2NF.
DataSet from Pakistani PharmacyStore:
Database Management
MedicineName ManufacturerSystem-L
BatchNo ExpiryDate StoreLocation Quantity SupplierName Lab Session 05
SupplierContact
Iqra University Department of Software Engineering
Panadol GSK B123 2025-12-30 Lahore 100 Ali Medical Supplies 03001234567
-- 1. Medicines Table
MedicineName VARCHAR(50),
Manufacturer VARCHAR(50)
);
-- 2. Suppliers Table
SupplierName VARCHAR(100),
SupplierContact VARCHAR(20)
);
-- 3. Stock Table
MedicineID INT,
BatchNo VARCHAR(20),
ExpiryDate DATE,
StoreLocation VARCHAR(50),
Quantity INT,
SupplierID INT,
);
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
Task 3: Discuss Limitations of 2NF
● Research and write a brief explanation about the limitations of stopping normalization at
2NF.
● Suggest scenarios where further normalization (e.g., 3NF) is beneficial.
Limitations of 2NF:
3NF removes transitive dependencies. Great for improving data integrity and
consistency.
Task 4:
Write a query to insert additional data into the normalized Student and Course tables and test
the join query to retrieve combined data.
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
-- Insert Students
FROM Students s
Task 5:
Write a query to insert additional data into the normalized Employee and Department tables and
test the join query to retrieve combined data.
-- Insert Employee
FROM Employees e
Task 6:
Database Management System-L Lab Session 05
Iqra University Department of Software Engineering
Modify the data in the Student table (e.g., update a student's course) and write a query to reflect
the change in the combined result.
UPDATE StudentCourses
FROM Students s
Conclusion:
Lab Session 06
Objective:
The Purpose of this Lab is to Introduction to Data Manipulation Language (DML) or Basic
CRUD which includes Insert, Update & Delete Statements.
Theory:
The DML SQL statements are used to manipulate database and table data. The statements shown
in the slide are followed by options to specify data operations (such as table name).
INSERT Statement:
• Syntax
Let's say you have a table called employees with the columns id, name, and age. You want to
insert multiple rows into this table. The SQL query would look like this.
Syntax:
UPDATE table_name
WHERE condition;
•Example
Let's say we have a table called employees with columns id, name, and age. You want to update
the age of the employee with id = 2.
UPDATE employees
SET age = 35
WHERE id = 2;
•Example
Now, if you want to update both the name and age of the employee with id = 3,
UPDATE employees
WHERE id = 3;
DELETE Statement:
Syntax:
WHERE condition;
•Example
We have a table called employees with columns id, name, and age. If you want to delete the
record of the employee with id = 3, you would use the following query:
If you want to delete all employees who are under the age of 25, you would use this query:
CLASS TASKS:
Task 1:
title VARCHAR(100),
year INT,
director VARCHAR(100)
);
INSERT INTO Movie (id, title, year, director)
VALUES
Task 2:
UPDATE Movie
Task 3
Home Tasks:
Task 1:
-- ✅ Syntax of INSERT:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Task 2:
Task 3:
Analysis:
Carefully structuring INSERT queries ensures smooth data entry, while UPDATE and
DELETE commands must always be paired with specific WHERE clauses to avoid
unintended bulk operations. Together, these DML operations provide powerful control
over database content—but with great power comes the need for careful usage and testing.
Database Management System-L Lab Session 06
Iqra University Department of Software Engineering
Conclusion:
Conclusion:
The INSERT, UPDATE, and DELETE statements form the core of SQL’s Data
Manipulation Language (DML), allowing us to add, modify, and remove records from a
database efficiently. Understanding these commands is crucial for managing data
accurately in real-world applications.
Database Management System-L Lab
Session 06
Iqra University Department of Software
Engineering