Chap04 (ICS12)
Chap04 (ICS12)
Unique and • The attribute that identifies each record within the
Not Null relation is unique and never null
The attribute that is used as primary key cannot contain null value
• One relation is called parent relation and other is called child relation
• The Parent relation contains primary key and child relation contains
foreign key
Achieving Referential Integrity
• Referential integrity ensures:
• A value entered in the foreign key field of child relation must exist in the primary
key field of parent relation
• Record cannot be deleted from a child relation if matching records exist in parent
relation
• Primary key value cannot be changed in the parent relation if its corresponding
records exist in
the child relation
Major Problems / Errors in Relations
1 • Synony
m
2 • Homony
m
3 • Redundanc
y
4 • Mutual Exclusiveness of
Data
Major Problems / Errors in Relations
1 Synonym
• A type of problem in the relations that occurs when two different
names are used
for the same attribute
• ITEM ( ItemNo, ItemColor, SupplierCode)
1 • Insertion
Anomaly
2 • Deletion
Anomaly
3 • Modification
Anomaly
Database Anomalies
1. Insertion Anomaly
• The insertion anomaly occurs when a new record is inserted in the
relation
• In this anomaly, the user cannot insert a fact about an
entity until he has an additional fact about another entity
CustomerID Name SalesMan Region
10 Ahsan Ahmad South
20 Babar Bashir West
30 Ali Ahmad South
40 Daood Khalid East
50 Raza Bashir West
60 Farooq Munir North
Abid North
Database Anomalies
2. Deletion Anomaly
• The deletion anomaly occurs when a record is deleted from the relation
• In this anomaly, the deletion of facts about an entity automatically
deletes the fact of another entity
Advantages of Normalization
• Reduces data redundancy
• Allows users to
• Insert, delete, and update rows without causing data inconsistencies (anomalies)
1 2 3 BCNF 4 5
NF NF NF NF NF
• Each normal form has certain requirements or condition
Notatio (arrow
n: )
Functional Dependency
Example 1
• Relation STUDENT with following
attributes:
STUDENT (RollNo, StudentName, Marks,
Email)
• Functional dependency
RollNo StudentName , Marks, Email
• Functional dependency
Method 2
• Place all items that appear in the repeating group in a new table
• Designate a primary key for each new table produced.
• Duplicate in the new table the primary key of the table
from which the repeating group was extracted or vice
First Normal Form (1NF)
Applying Method 1
Example
DEPARTMENT (DeptNo, DeptName, EmpNo,
EmpName)
DeptNo DeptName EmpNo EmpName
10 Management E0 Usman
1 Khalil
E0 Abdullah
2
20 Finance E1 Ali Ahmed
0 Mahmood
E1 Abbas
1
30 IT E25 Hamid Ali
First Normal Form (1NF)
Applying Method 1
Copyright @ IT www.itseries.com.
Partial Dependency
Example
• Relation Employee with following attributes:
Employee (EmpID, Name, DeptName, Salary, CourseTitle,
DateCompleted)
• Contains composite primary key EmpID and CourseTitle
EmpID Name DeptName Salary CourseTitle DateCompleted
100 Ahmad Marketing 25,000 Advertising 19/06/2019
100 Ahmad Marketing 25,000 Surveys 10/09/2019
140 Nazir Accounting 19,000 MS Excel 12/08/2019
110 Hamid IT 24,000 Oracle 14/07/2019
110 Hamid IT 24,000 Java 22/09/2019
190 Rashid Finance 30,000 Investment 20/06/2020
150 Hussain Marketing 25,000 Advertising 19/06/2020
150 Hussain Marketing 25,000 Ecommerce 20/09/2020
Copyright @ IT www.itseries.com.
Partial Dependency
• Functional dependencies in Employee
relation:
EmpID Name, DeptName, Salary
EmpID, CourseTitle DateCompleted
Copyright @ IT www.itseries.com.
Partial Dependency
Problems
• The partial dependency in the above relation creates
redundancy
• It results in certain database anomalies when the relation is
updated
• Insertion Anomaly
• Deletion Anomaly
• Modification Anomaly
Copyright @ IT www.itseries.com.
Second Normal Form (2NF)
It is in There is no Partial
1NF Dependency
Copyright @ IT www.itseries.com.
Second Normal Form (2NF)
DateCompleted
Copyright @ IT www.itseries.com.
Second Normal Form (2NF)
Problems
• Deletion Anomaly
• Modification Anomaly
Copyright @ IT www.itseries.com.
Solution
?
Copyright @ IT www.itseries.com.
Second Normal Form (2NF)
Remove Partial Dependency
Create Separate Tables for Each
Dependency
EmpID Name, DeptName, EmpID, CourseTitle
Salary DateCompleted
EmpID Name DeptName Salary EmpID CourseTitle DateCompleted
100 Ahmad Marketing 25,000 100 Advertising 19/06/2019
100 Surveys 10/09/2019
140 Nazir Accounting 19,000
140 MS Excel 12/08/2020
110 Hamid IT 24,000 110 Oracle 14/07/2019
190 Rashid Finance 30,000 110 Java 22/09/2019
150 Hussain Marketing 25,000 190 Investment 20/06/2020
150 Advertising 19/06/2020
150 Ecommerce 20/09/2020
• Deletion Anomaly
• Modification
Anomaly
CustomerID NAME SALESMAN REGION
10 Ahsan Ahmad South
20 Babar Bashir West
30 Ali Ahmad South
40 Daood Khalid East
50 Raza Bashir West
60 Farooq Munir North
Copyright @ IT www.itseries.com.
Third Normal Form ( 3NF )
It is in There is no Transitive
2NF Dependency
Copyright @ IT www.itseries.com.
Third Normal Form ( 3NF )
How to Achieve 3NF?
Copyright @ IT www.itseries.com.
Third Normal Form ( 3NF )
Dependencies in the Relation
CustomerID Name,
SalesMan SalesMan
Region
CustomerID Name SalesMan Region
10 Ahsan Ahmad South
20 Babar Bashir West
30 Ali Ahmad South
40 Daood Khalid East
50 Raza Bashir West
60 Farooq Munir North
Copyright @ IT www.itseries.com.
Third Normal Form ( 3NF )
Dependencies in the Relation
CustomerID Name,
SalesMan SalesMan
Region
CustomerID Name SalesMan Region
10 Ahsan Ahmad South
20 Babar Bashir West
30 Ali Ahmad South
40 Daood Khalid East
50 Raza Bashir West
60 Farooq Munir North
Copyright @ IT www.itseries.com.
Third Normal Form ( 3NF )
Problems
• Insertion Anomaly
• Deletion Anomaly
• Modification
Anomaly
CustomerID NAME SALESMAN REGION
10 Ahsan Ahmad South
20 Babar Bashir West
30 Ali Ahmad South
40 Daood Khalid East
50 Raza Bashir West
60 Farooq Munir North
Copyright @ IT www.itseries.com.
Solution
?
Copyright @ IT www.itseries.com.
Third Normal Form ( 3NF )
Remove Transitive Dependency