Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

In the name of Allah, the most beneficent, the most merciful

Advanced Database Systems


Lecture # 8
3NF + Advanced NFs (revision)

Instructor: Furqan Shahid


Third normal form (3NF) - definition
• A table (or relation) is in 3NF when already in 2NF and no transitive
dependency present in it

• Transitive dependency: If A,B and C are three attributes such that, A


is determinant of B (A → B) and B is determinant of C (B → C), then C
is transitively dependent on A

A→B→C

C is transitively dependent on A
Instructor: Furqan Shahid
Normalization example-3
STUDENT
RegNo Name Address DateOfBirth DegreeTitle DegreeDuration DegreeFee
1001 Usman H#1,St#1, City1 17-May-1990 BS CS 8-semester 250,000
1002 Uzair H#2,St#2,City1 09-Aug-1991 MS CS 4-semester 175,000
1003 Bilal H#1,St#1,City2 19-Apr-1989 BS CS 8-semester 250, 000

• No repeating group (i.e. in 1NF)


• No partial dependency (i.e. in 2NF)
• No composite identifier, no partial dependency
• Transitive dependencies:
RegNo → DegreeTitle → DegreeDuration
RegNo → DegreeTitle → DegreeFee

Instructor: Furqan Shahid


Normalization example-3 (cont.)
STUDENT
RegNo Name Address DateOfBirth DegreeTitle DegreeDuration DegreeFee
1001 Usman H#1,St#1, City1 17-May-1990 BS CS 8-semester 250,000
1002 Uzair H#2,St#2,City1 09-Aug-1991 MS CS 4-semester 175,000
1003 Bilal H#1,St#1,City2 19-Apr-1989 BS CS 8-semester 250, 000

• Anomalies:
• Can’t insert record of “Degree” without “Student”
3NF

STUDENT:
RegNo Name Address DateOfBirth DegreeTitle DEGREE:

1001 Usman H#1,St#1, City1 17-May-1990 BS CS Title Duration Fee

1002 Uzair H#2,St#2,City1 09-Aug-1991 MS CS BS CS 8-semester 250,000

1003 Bilal H#1,St#1,City2 19-Apr-1989 BS CS MS CS 4-semester 175,000

Instructor: Furqan Shahid


Normalization example-4
MANAGER:
MgrNO Name Experience DateOfHiring AllocatedCarNo CarModel CarPrice CarInsurer InsurerOffice InsurerContact
1001 Usman 5 years 17-May-2016 IDG 6877 2015 650,000 SLP Islamabad 051-xxxxxx
1002 Uzair 7 years 09-Aug-2017 RI 567 2017 775,000 JIP Lahore 042-xxxxxx
1003 Bilal 4 years 19-Apr-2014 LHR 3003 2014 550, 000 SLP Islamabad 051-xxxxxx

• A manager has max. one car


• A car has max. one insurer
• No repeating group (i.e. in 1NF)
• No partial dependency (i.e. in 2NF)
• Contains Transitive dependencies (i.e. not in 3NF)
• E.g. CarRegNo → CarModel, CarPrice

Instructor: Furqan Shahid


MANAGER:
Solution example-4
MgrNO Name Experience DateOfHiring AllocatedCarNo CarModel CarPrice CarInsurer InsurerOffice InsurerContact
1001 Usman 5 years 17-May-2016 IDG 6877 2015 650,000 SLP Islamabad 051-xxxxxx
1002 Uzair 7 years 09-Aug-2017 RI 567 2017 775,000 JIP Lahore 042-xxxxxx
1003 Bilal 4 years 19-Apr-2014 LHR 3003 2014 550, 000 SLP Islamabad 051-xxxxxx

MANAGER:
MgrNO Name Experience DateOfHiring AllocatedCarNo • Still contains transitive
1001 Usman 5 years 17-May-2016 IDG 6877 dependencies
1002 Uzair 7 years 09-Aug-2017 RI 567
InsurerCode → Office, Contact
1003 Bilal 4 years 19-Apr-2014 LHR 3003
CAR:
CarRegNo CarModel CarPrice CarInsurer InsurerOffice InsurerContact
IDG 6877 2015 650,000 SLP Islamabad 051-xxxxxx
RI 567 2017 775,000 JIP Lahore 042-xxxxxx
LHR 3003 2014 550, 000 SLP Islamabad 051-xxxxxx Instructor: Furqan Shahid
Solution example-4 (cont.)
MANAGER:
MgrNO Name Experience DateOfHiring AllocatedCarNo
1001 Usman 5 years 17-May-2016 IDG 6877
1002 Uzair 7 years 09-Aug-2017 RI 567
1003 Bilal 4 years 19-Apr-2014 LHR 3003

CAR:
CarRegNo Model Price InsurerCode
IDG 6877 2015 650,000 SLP
RI 567 2017 775,000 JIP
LHR 3003 2014 550, 000 SLP
INSURER:
InsurerCode Office Contact
SLP Islamabad 051-xxxxxx
JIP Lahore 042-xxxxxx

Instructor: Furqan Shahid


Normalization example-5
STAFF:
StafNO Name Salary DateOfHiring AssignedProject PrjDuration PrjCost RoleOnPrj PrjSponsor SpsOffice SpsPhone
1001 Usman 50000 17-May-2016 PRJ-1 6 month 7 billion Testing AA Ent. Karachi 021-xxxxxx
PRJ-3 1.5 month 3 billion Planning AB Co. Islamabad 051-xxxxxx
1002 Uzair 70000 09-Aug-2017 PRJ-1 6 month 7 billion Architect AA Ent. Karachi 021-xxxxxx
PRJ-2 8 month 12 billion Designing AM Co. Lahore 042-xxxxxx
1003 Bilal 40000 19-Apr-2014 PRJ-3 1.5 month 3 billion Testing AB Co. Islamabad 051-xxxxxx

• Contains repeating group (i.e. not in 1NF)


• An employee can be assigned to many projects

Instructor: Furqan Shahid


STAFF: Solution example-5 (1NF)
StafNO Name Salary DateOfHiring AssignedProject PrjDuration PrjCost RoleOnPrj PrjSponsor SpsOffice SpsPhone
1001 Usman 50000 17-May-2016 PRJ-1 6 month 7 billion Testing AA Ent. Karachi 021-xxxxxx
1001 Usman 50000 17-May-2016 PRJ-3 1.5 month 3 billion Planning AB Co. Islamabad 051-xxxxxx
1002 Uzair 70000 09-Aug-2017 PRJ-1 6 month 7 billion Architect AA Ent. Karachi 021-xxxxxx
1002 Uzair 70000 09-Aug-2017 PRJ-2 8 month 12 billion Designing AM Co. Lahore 042-xxxxxx
1003 Bilal 40000 19-Apr-2014 PRJ-3 1.5 month 3 billion Testing AB Co. Islamabad 051-xxxxxx

• Make “ProjectCode” part of the primary key (for 1NF)


• Contains partial dependencies (i.e. not in 2NF)
(StafNo, ProjectCode) → Name, Salary, DateOfHiring
(StafNo, ProjectCode) → Duration, Cost, Sponsor, Office, Phone

Instructor: Furqan Shahid


Solution example-5 (2NF)
STAFF: StafRoleForProject:
StafNO Name Salary DateOfHiring StafNO ProjectCode Role
1001 Usman 50000 17-May-2016 1001 PRJ-1 Testing
1002 Uzair 70000 09-Aug-2017 1001 PRJ-3 Planning
1003 Bilal 40000 19-Apr-2014 1002 PRJ-1 Architect
1002 PRJ-2 Designing
1003 PRJ-3 Testing

PROJECT:
Code Duration Cost Sponsor SpsOffice SpsPhone
PRJ-1 6 month 7 billion AA Ent. Karachi 021-xxxxxx
PRJ-2 8 month 12 billion AM Co. Lahore 042-xxxxxx
PRJ-3 1.5 month 3 billion AB Co. Islamabad 051-xxxxxx

Instructor: Furqan Shahid


Solution example-5 (3NF Yes/No?)
STAFF: StafRoleForProject:
StafNO Name Salary DateOfHiring StafNO ProjectCode Role
1001 Usman 50000 17-May-2016 1001 PRJ-1 Testing
1002 Uzair 70000 09-Aug-2017 1001 PRJ-3 Planning
1003 Bilal 40000 19-Apr-2014 1002 PRJ-1 Architect
1002 PRJ-2 Designing
1003 PRJ-3 Testing

PROJECT:
Code Duration Cost Sponsor SpsOffice SpsPhone • Contains transitive dependencies
PRJ-1 6 month 7 billion AA Ent. Karachi 021-xxxxxx (not in 3NF)
PRJ-2 8 month 12 billion AM Co. Lahore 042-xxxxxx SponsorCode → Office, Phone
PRJ-3 1.5 month 3 billion AB Co. Islamabad 051-xxxxxx

Instructor: Furqan Shahid


Solution example-5 (3NF)
STAFF: StafRoleForProject:
StafNO Name Salary DateOfHiring StafNO ProjectCode Role
1001 Usman 50000 17-May-2016 1001 PRJ-1 Testing
1002 Uzair 70000 09-Aug-2017 1001 PRJ-3 Planning
1003 Bilal 40000 19-Apr-2014 1002 PRJ-1 Architect
1002 PRJ-2 Designing
1003 PRJ-3 Testing

PROJECT: SPONSOR:
Code Duration Cost SponsorCode Code Office Phone
PRJ-1 6 month 7 billion AA Ent. AA Ent. Karachi 021-xxxxxx
PRJ-2 8 month 12 billion AM Co. AM Co. Lahore 042-xxxxxx
PRJ-3 1.5 month 3 billion AB Co. AB Co. Islamabad 051-xxxxxx

Instructor: Furqan Shahid


Boyce Codd Normal Form (BCNF)
• Advanced version of 3NF
• A relation is in BCNF when already in 2NF and every determinant is a
candidate key
• 3NF fails to remove anomalies when there are multiple overlapping
candidate keys in the table

Instructor: Furqan Shahid


Fourth normal form (4NF)
• A relation is in 4NF when already in 3NF/BCNF and no multi-valued
dependency present in it
• Multi-valued dependency: When two attributes in a table are
independent of each other but, both depend on a third attribute
EMPLOYEECONTACT:
EmpNo CellNo Email

• Both CellNo and Email depend on EmpNo, however, independent of each


other

Instructor: Furqan Shahid


Multi-valued dependency
EMPLOYEECONTACT:
• Anomalies:
EmpNo CellNo Email
• Can’t insert CellNo if no Email is present, and vice versa
1 Cell11 Mail11 • Increased data duplication
1 Cell12 Mail11
1 Cell13 Mail11
1 Cell11 Mail12
1 Cell12 Mail12
1 Cell13 Mail12
2 Cell21 Mail21
2 Cell22 Mail21
3 Cell31 Mail31
3 Cell31 Mail32

Instructor: Furqan Shahid


Removing multi-valued dependency
EMPLOYEE_CONTACT:
EmpNo CellNo Email
1 Cell11 Mail11
EMPLOYEE_CELL: EMPLOYEE_EMAIL:
1 Cell12 Mail11
EmpNo CellNo EmpNo Email
1 Cell13 Mail11
1 Cell11 1 Mail11
1 Cell11 Mail12
1 Cell12 1 Mail12
1 Cell12 Mail12
1 Cell13 2 Mail21
1 Cell13 Mail12
2 Cell21 3 Mail31
2 Cell21 Mail21
2 Cell22 3 Mail32
2 Cell22 Mail21
3 Cell31
3 Cell31 Mail31
3 Cell31 Mail32

Instructor: Furqan Shahid


Domain key normal form (DKNF)
• A relation is in DKNF when it contains no constraint except the
domain constraints and the key constraints
• Domain constraints:
• GPA must be less or equal 4.0
• Salary must be greater than zero
• First name can contain just alphabets etc.

Instructor: Furqan Shahid


Key constraints
• Primary key constraint: The primary key (or identifier) must be
unique and non-null
• Also known as entity integrity

• Foreign key constraint: Each non-null value in the foreign key must
already be existing in the corresponding parent key
• Also known as referential integrity

Instructor: Furqan Shahid


Thank you

Instructor: Furqan Shahid

You might also like