Database 2024-04-02

Database Normalization Explained: 1NF to 3NF with Examples

Learn database normalization from first principles. Understand 1NF, 2NF, and 3NF with practical examples and when denormalization is the right trade-off for performance.

Read in: ja
Database Normalization Explained: 1NF to 3NF with Examples

What is Normalization

Designing to eliminate data redundancy and prevent logical inconsistencies in data.

Prerequisites

Keys

Employee Number ID Name Gender Address Phone Number
1 1001 Taro Yamada Male Chiyoda, Tokyo 03-1234-5678
2 1002 Hanako Tanaka Female Shibuya, Tokyo 03-2345-6789
3 1003 Jiro Suzuki Male Shinjuku, Tokyo 03-3456-7890
4 1004 Saburo Sato Male Minato, Tokyo 03-4567-8901
5 1005 Shiro Takahashi Male Meguro, Tokyo 03-5678-9012

Primary Key: {Employee Number, ID}

Candidate Key: {Employee Number, ID}, {Phone Number}

Super Key: {Employee Number, ID}, {Employee Number, ID, Name}, {Employee Number, ID, Name, Gender} etc...

Functional Dependency

Normal Forms

First Normal Form (1NF)

Second Normal Form (2NF)

Third Normal Form (3NF)

Boyce-Codd Normal Form (BCNF)

Fourth Normal Form (4NF)

Fifth Normal Form (5NF)

Normalization

1NF

Non-normalized form

Invoice Number Product Number Product Name
1 A001A002A003 AppleOrangeBanana
2 A004A005A006 GrapePearStrawberry

1NF

Invoice Number Product Number Product Name
1 A001 Apple
1 A002 Orange
1 A003 Banana
2 A004 Grape
2 A005 Pear
2 A006 Strawberry

2NF

Before 2NF

Invoice Number Product Number Product Name
1 A001 Apple
1 A002 Orange
1 A003 Banana
2 A004 Grape
2 A005 Pear
2 A006 Strawberry

The candidate key {Invoice Number, Product Number} and the non-key attribute Product Name are partially functionally dependent.

2NF Sales Detail Table

Invoice Number Product Number
1 A001
1 A002
1 A003
2 A004
2 A005
2 A006

Product Table

Product Number Product Name
A001 Apple
A002 Orange
A003 Banana
A004 Grape
A005 Pear
A006 Strawberry

Partial functional dependency only occurs when the candidate key is a composite key, so it does not occur when the candidate key is a single attribute.

3NF

Before 3NF

Invoice Number Product Number Customer Number Customer Name
1 A001 B1 Apple Corp
1 A002 B1 Orange Corp
1 A003 B1 Banana Corp
2 A004 C1 Grape Corp
2 A005 C1 Pear Corp
2 A006 C1 Strawberry Corp

When the primary key is {Invoice Number, Product Number}, {Invoice Number, Customer Number}→{Customer Number}→{Customer Name} is transitively dependent.

3NF Sales Detail Table

Invoice Number Product Number
1 A001
1 A002
1 A003
2 A004
2 A005
2 A006

Customer Table

Customer Number Customer Name
B1 Apple Corp
B1 Orange Corp
B1 Banana Corp
C1 Grape Corp
C1 Pear Corp
C1 Strawberry Corp

BCNF

Before BCNF

Name Subject Teacher
Bob Math Yamada
Tom Math Sato
John Math Suzuki
John English Ando

When the primary key is {Name, Subject}, there is a functional dependency {Teacher}→{Subject}, and the determinant (A in A→B) is not a super key.

BCNF Enrollment Table

Name Subject
Bob Math
Tom Math
John Math
John English

Teacher Table

Teacher Subject
Yamada Math
Sato Math
Suzuki Math
Ando English

The information {Name, Subject}→{Teacher} is lost, so it is unclear who John's math teacher is.

4NF

Before 4NF

Name Hobby Favorite Food
Tanaka Baseball Ramen
Suzuki Soccer Sushi
Sato Basketball Curry

When the primary key is {Name, Hobby, Favorite Food}, multiple attributes are determined by {Name}→{Hobby}→{Favorite Food}.

4NF Hobby Table

Name Hobby
Tanaka Baseball
Suzuki Soccer
Sato Basketball

Favorite Food Table

Name Favorite Food
Tanaka Ramen
Suzuki Sushi
Sato Curry

5NF

Before 5NF

Store Stock Item Manufacturer
Tokyo TV Company A
Tokyo TV Company B
Tokyo PC Company A
Kanagawa TV Company A

{Store}→{Stock Item}, {Store}→{Manufacturer}, {Stock Item}→{Manufacturer} can be decomposed into multiple parts.

5NF Stock Table

Store Stock Item
Tokyo TV
Tokyo TV
Tokyo PC
Kanagawa TV

Supplier Table

Store Supplier
Tokyo Company A
Tokyo Company B
Tokyo Company A
Kanagawa Company A

Manufacturer Table

Store Manufacturer
Tokyo Company A
Tokyo Company B
Tokyo Company A
Kanagawa Company A

Thoughts

I am not very confident in my understanding beyond BCNF...

References

Tags: Normalization Theory Normalization
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles