Ch7 MKN Normalization Part 2
Ch7 MKN Normalization Part 2
1
Normalization
Normalization is a method for designing a relational
database, which is generate a set of relation schemas that
allows us to store information without unnecessary
redundancy and allows us to retrieve information easily.
2
Normalization
Properties of Normalization
3
Normalization
• This involves restructuring the tables to successively
meeting higher forms of Normalization.
• A properly normalized database should have the
following characteristics
– Scalar values in each fields
– Absence of redundancy.
– Minimal use of null values.
– Minimal loss of information.
4
Normalization
Roll Name Age Book Subject Price
1 Karim 20 b1 Network 500
2 Rahim 21 b2 Algorithm 550
3 Jasim 20 b10 DBMS 600
4 Rayan 19 b4 Communication 703
5 Mahira 22 b15 Electronics 415
6 Mouri 19 b20 Management 425
7 Nihal 20 b11 Math 672
8 Shamim 22 b17 Graphics 737
9 Sohel 23 b19 Automation 532
1 Karim 20 b21 E-commerce 503
6
Levels of Normalization
• Levels of normalization based on the amount of
redundancy in the database.
• Various levels of normalization are:
– First Normal Form (1NF)
– Second Normal Form (2NF)
Redundancy
Number of Tables
– Third Normal Form (3NF)
Complexity
– Boyce-Codd Normal Form (BCNF)
– Fourth Normal Form (4NF)
– Fifth Normal Form (5NF)
– Domain Key Normal Form (DKNF)
0-55-123456-9 Main Street Small House 714-000-0000 $22.95 0-55-123456-9 Jones 123-333-3333
After Decomposition
SID Sname S Addr Major CID Ctitle Iname ILoc Grade
11
2NF - Decomposition
1. If a data item is fully functionally dependent on only a part of the
primary key, move that data item and that part of the primary key to
a new table.
2. If other data items are functionally dependent on the same part of the
key, place them in the new table also
3. Make the partial primary key copied from the original table the
primary key for the new table. Place all items that appear in the
repeating group in a new table
Example 1 (Convert to 2NF)
Old Scheme {Title, PubId, AuId, Price, AuAddress}
New Scheme {Title, PubId, AuId, Price}
New Scheme {AuId, AuAddress}
12
Second Normal Form (2NF)
Example 1 (Not 2NF)
Scheme {Title, PubId, AuId, Price, AuAddress}
1. Key {Title, PubId, AuId}
2. {Title, PubId, AuID} {Price}
3. {AuID} {AuAddress}
4. AuAddress does not belong to a key
5. AuAddress functionally depends on AuId which is a subset of a key
13
Second Normal Form (2NF)
Example 2 (Not 2NF)
Scheme {City, Street, HouseNumber, HouseColor, CityPopulation}
1. key {City, Street, HouseNumber}
2. {City, Street, HouseNumber} {HouseColor}
3. {City} {CityPopulation}
4. CityPopulation does not belong to any key.
5. CityPopulation is functionally dependent on the City which is a
proper subset of the key
14
Second Normal Form (2NF)
Example 1 (Not 2NF)
Example 3 (Not 2NF)
Scheme {studio, movie, budget, studio_city}
1. Key {studio, movie}
2. {studio, movie} {budget}
3. {studio} {studio_city}
4. studio_city is not a part of a key
5. studio_city functionally depends on studio which is a proper subset of
the key
(Convert to 2NF)
Old Scheme {Studio, Movie, Budget, StudioCity}
New Scheme {Movie, Studio, Budget}
New Scheme {Studio, City}
15
Second Normal Form (2NF)
• In the Grade-Report Relation there are 4 FDs
• Primary Key: {SID, CID} is composite key
• SID→Sname Saddr Major
• CID →Ctitle Iname Iloc
• SID CID →Grade
• Iname →Iloc
111
Second
Rahim
Normal
204, MH IS11
Form (2NF)
IS11 DBMS MKN SAC 203 A
111 Rahim 204, MH IS11 IS22 SAD MMH NAC 504 B
222 Karim 405, MV IT IS11 DBMS MKN SAC 203 C
222 Karim 405, MV IT IT22 COA RMZ NAC307 B
222 Karim 405, MV IT IT33 C HK NAC 601 A
18
Third Normal Form (3NF)
Example (Not in 3NF)
Scheme {Title, PubID, PageCount, Price }
1. Key {Title, PubId}
2. {Title, PubId} {PageCount}
3. {PageCount} {Price}
4. Both Price and PageCount depend on a key hence 2NF
5. Transitively {Title, PubId} {PageCount} and {PageCount} {Price}
hence not in 3NF
19
Third Normal Form (3NF)
SID Sname S Addr Major CID Ctitle Iname ILoc
111 IS11 A
111 IS22 B CID →Ctitle
222 IS11 C CID → Iname
CID →Iloc
222 IT22 B
Iname →Iloc
222 IT33 A
20
Third Normal Form (3NF)
CID Ctitle Iname ILoc
SID Sname S Addr Major
22
Third Normal Form (3NF)
Example 2 (Not in 3NF)
Scheme {Studio, StudioCity, CityTemp}
1. Primary Key {Studio}
2. {Studio} {StudioCity}
3. {StudioCity} {CityTemp}
4. {Studio} {CityTemp}
5. Both StudioCity and CityTemp depend on the entire key hence 2NF
6. CityTemp transitively depends on Studio hence violates 3NF
(Convert to 3NF)
Old Scheme {Studio, StudioCity, CityTemp}
New Scheme {Studio, StudioCity}
New Scheme {StudioCity, CityTemp}
23
Third Normal Form (3NF)
Example 3 (Not in 3NF)
Scheme {BuildingID, Contractor, Fee}
1. Primary Key {BuildingID}
2. {BuildingID} {Contractor}
3. {Contractor} {Fee}
4. {BuildingID} {Fee}
5. Fee transitively depends on the BuildingID
6. Both Contractor and Fee depend on the entire key hence 2NF
BuildingI Contractor Fee
D
100 Randolph 1200
(Convert to 3NF) 150 Ingersoll 1100
24
Boyce-Codd Normal Form (BCNF)
• BCNF does not allow dependencies between attributes that belong to
candidate keys.
• BCNF is a refinement of the third normal form in which it drops the
restriction of a non-key attribute from the 3rd normal form.
• Third normal form and BCNF are not same if the following conditions are
true:
– The table has two or more candidate keys
– At least two of the candidate keys are composed of more than one
attribute
– The keys are not disjoint i.e. The composite candidate keys share some
attributes
25
Boyce-Codd Normal Form (BCNF)
Example 1 - Address (Not in BCNF)
Scheme {City, Street, ZipCode }
1. Key1 {City, Street }
2. Key2 {ZipCode, Street}
3. No non-key attribute hence 3NF
4. {City, Street} {ZipCode}
5. {ZipCode} {City}
6. Dependency between attributes belonging to a key
(Convert to BCNF)
Old Scheme {City, Street, ZipCode }
New Scheme1 {ZipCode, Street}
New Scheme2 {City, Street}
• Loss of relation {ZipCode} {City}
Alternate New Scheme1 {ZipCode, Street }
Alternate New Scheme2 {ZipCode, City}
26
Boyce Codd Normal Form (BCNF)
Example 2 - Movie (Not in BCNF)
Scheme {MovieTitle, MovieID, PersonName, Role, Payment }
1. Key1 {MovieTitle, PersonName}
2. Key2 {MovieID, PersonName}
3. Both role and payment functionally depend on both candidate keys thus 3NF
4. {MovieID} {MovieTitle}
5. Dependency between MovieID & MovieTitle Violates BCNF
(Convert to BCNF)
Old Scheme {MovieTitle, MovieID, PersonName, Role, Payment }
New Scheme {MovieID, PersonName, Role, Payment}
New Scheme {MovieTitle, PersonName}
• Loss of relation {MovieID} {MovieTitle}
New Scheme {MovieID, PersonName, Role, Payment}
New Scheme {MovieID, MovieTitle}
• We got the {MovieID} {MovieTitle} relationship back
27
Boyce Codd Normal Form (BCNF)
Example 3 - Consulting (Not in BCNF)
Scheme {Client, Problem, Consultant}
1. Key1 {Client, Problem}
2. Key2 {Client, Consultant}
3. No non-key attribute hence 3NF
4. {Client, Problem} {Consultant}
5. {Client, Consultant} {Problem}
6. Dependency between attributes belonging to keys violates BCNF
28
BCNF - Decomposition
1. Place the two candidate primary keys in separate entities
2. Place each of the remaining data items in one of the resulting entities
according to its dependency on the primary key.
SID Major Advisor There are two FD
123 CSE Instructor-1 SID Major →Advisor
125 History Instructor-2 Advisor →Major
456 BMB Instructor-3
SID Advisor Advisor Major
789 EEE Instructor-4
123 Instructor-1 Instructor-1 CSE
999 CSE Instructor-1
125 Instructor-2 Instructor-2 History
456 Instructor-3 Instructor-3 BMB
789 Instructor-4 Instructor-4 EEE
999 Instructor-1
29
Fourth Normal Form (4NF)
• Fourth normal form eliminates independent many-to-one relationships
between columns.
• To be in Fourth Normal Form,
– a relation must first be in Boyce-Codd Normal Form.
– a given relation may not contain more than one multi-valued attribute.
– Non trivial multivalued dependencies other than a candidate key
Bill Durham Durham Drama
30
The Code Warrier New York Horror
Fourth Normal Form (4NF)
• Fourth normal form eliminates independent many-to-one relationships
between columns.
• To be in Fourth Normal Form,
– a relation must first be in Boyce-Codd Normal Form.
– a given relation may not contain more than one multi-valued attribute.
– Non trivial multivalued dependencies other than a candidate key
Course Instructor Textbook Course Instructor Course Textbook
Database MKN1 Silberschatz Database MKN1 Database Silberschatz
MMH Hector
Database MMH
MSA Database Hector
Database MSA Finance Weston
Finance MSK Weston
Gilford Finance MSK Finance Gilford
31
Fourth Normal Form (4NF)
Example 2 (Not in 4NF) Manager Child Employee
Scheme {Manager, Child, Employee} Jim Beth Alice
2. Each manager can have more than one child Mary NULL Adam
3. Each manager can supervise more than one employee
4. 4NF Violated
33
4NF - Decomposition
Example 2 (Convert to 4NF) Manager Child Manager Employee
Old Scheme {Manager, Child, Employee}
Jim Beth Jim Alice
35
Exercise
Given a relation R( A, B, C, D) and Functional Dependency set
FD = { AB → CD, B → C }, determine whether the given R is in
2NF? If not convert it into 2 NF.
36
Exercise
Solution:
a) R1( B, C)
b) R2(A, B, D)
37
Exercise
Given a relation R( X, Y, Z, W, P) and Functional Dependency
set FD = { X → Y, Y → P, and Z → W}, determine whether the
given R is in 3NF? If not convert it into 3 NF.
XZ + = XZYPW
Since the closure of XZ contains all the attributes of R, hence
XZ is Candidate Key
38
Solution:
Exercise
XZ + = XZYPW
Since the closure of XZ contains all the attributes of R, hence
XZ is Candidate Key
Definition of 3NF: First it should be in 2NF and if there exists a
non-trivial dependency between two sets of attributes
• R1(X, Y) {Using FD X → Y}
• R2(Y, P) {Using FD Y → P}
• R3(Z, W) {Using FD Z → W}
And create one table for Candidate Key XZ
• R4( X, Z) { Using Candidate Key XZ }
39
Exercise
Given a relation R( X, Y, Z) and Functional Dependency set
FD = { XY → Z and Z → Y }, determine whether the given R
is in BCNF? If not convert it into BCNF.
Solution: Let us construct an arrow diagram on R using FD to
calculate the candidate key.
40
Exercise
Solution:
41
First normal form (1NF)
A relation is in first normal form if every attribute in every row can contain
only one single (atomic) value.
A university uses the following relation:
Student(Surname, Name, Skills)
The attribute Skills can contain multiple values and therefore the relation
is not in the first normal form.
But the attributes Name and Surname are atomic attributes
that can contain only one value.
42
Second normal form (2NF)
A relation is in second normal form if it is in 1NF and every non key
attribute is fully functionally dependent on the primary key.
A university uses the following relation:
43
The following functional dependencies
exist:
44
Third normal form (3NF)
A relation is in third normal form if it is in 2NF and no non key attribute is transitively
dependent on the primary key. A bank uses the following relation:
Vendor(ID, Name, Account_No, Bank_Code_No, Bank)
The attribute ID is the identification key. All attributes are single valued (1NF).
The table is also in 2NF.
The following dependencies exist:
1. Name, Account_No, Bank_Code_No are functionally dependent on ID
(ID --> Name, Account_No, Bank_Code_No)
2. Bank is functionally dependent on Bank_Code_No
(Bank_Code_No --> Bank)
45
• Student_IDStudent_Name, Campus_Address, Major
• Course_ID Course_Title, Instructor_Name, Instructor_Location
• {Student_ID, Course_ID} Grade
• Instructor_Name Instructor_Location
46
• Student_IDStudent_Name, Campus_Address, Major
• Course_ID Course_Title, Instructor_Name, Instructor_Location
• {Student_ID, Course_ID} Grade
• Instructor_Name Instructor_Location
48
• Consider the following relation for published books:
• BOOK (book title, author, type_of_book, price, author_affiliation,
publisher).
• Suppose the following dependencies exist:
{ (book_title)(publisher, type_of_book);
(type_of_book)(price);
(author) (author_affiliation) }.
• What is the highest normal form that the above relation satisfies?
49
• You are given the below functional dependencies for relation R(A,B,C,D,E),
• F ={AB → C, AB → D, D → A, BC → D, BC → E}.
a. Is this relation is in BCNF? If not, show all dependencies that violate it.
b. Is this relation in 3NF? If not, show all dependencies that violate it.
50