Normalization is the process of structuring a relational database to reduce redundancy, dependency, and improve data integrity. It involves organizing data into multiple related tables using rules called normal forms.
Letβs walk through:
π― Goals of Normalization
- Eliminate redundant data (e.g., storing the same data in multiple tables)
- Ensure data dependencies make sense
- Make insert, update, delete operations efficient and safe
β First Normal Form (1NF)
π― Rule:
- Each cell contains atomic (indivisible) values.
- Each record must be unique.
- No repeating groups or arrays.
β Bad Table (Not in 1NF):
StudentID | Name | Courses |
---|---|---|
1 | Alice | Math, Physics |
2 | Bob | Chemistry |
Courses
is a multi-valued field β Not atomic.
β 1NF Table (Atomic values):
Split into two tables:
Students
StudentID | Name |
---|---|
1 | Alice |
2 | Bob |
Enrollments
StudentID | Course |
---|---|
1 | Math |
1 | Physics |
2 | Chemistry |
β Second Normal Form (2NF)
π― Rule:
- Must first satisfy 1NF
- Remove partial dependencies (i.e., non-key columns depending on only part of a composite key)
β Bad Table (1NF but not 2NF):
StudentID | Course | Instructor | Department |
---|---|---|---|
1 | Math | Dr. Smith | Mathematics |
1 | Physics | Dr. Newton | Physics |
- Composite key:
(StudentID, Course)
Instructor
,Department
depend only on Course, not full key β Partial dependency
β 2NF Solution:
Split into:
Enrollments
StudentID | Course |
---|---|
1 | Math |
1 | Physics |
Courses
Course | Instructor | Department |
---|---|---|
Math | Dr. Smith | Mathematics |
Physics | Dr. Newton | Physics |
Now all non-key fields fully depend on their primary key.
β Third Normal Form (3NF)
π― Rule:
- Must satisfy 2NF
- No transitive dependencies (non-key depends on non-key)
β Bad Table (2NF but not 3NF):
EmployeeID | Name | DeptID | DeptName |
---|---|---|---|
1 | Alice | D01 | HR |
2 | Bob | D02 | Finance |
DeptName
depends onDeptID
, not onEmployeeID
β Transitive dependency
β 3NF Solution:
Employees
EmployeeID | Name | DeptID |
---|---|---|
1 | Alice | D01 |
2 | Bob | D02 |
Departments
DeptID | DeptName |
---|---|
D01 | HR |
D02 | Finance |
β Boyce-Codd Normal Form (BCNF)
π― Rule:
- Stronger version of 3NF
- For any dependency
X β Y
,X
must be a super key
Use when:
- Multiple candidate keys exist
- 3NF is not enough
π Summary Table
Normal Form | Rule Summary |
---|---|
1NF | Atomic columns, unique rows |
2NF | No partial dependency on composite keys |
3NF | No transitive dependency |
BCNF | Every determinant is a candidate key |
π οΈ How to Normalize in Practice
- Start with one large table (denormalized)
- Identify repeating groups, move them into separate tables (1NF)
- Remove partial dependencies β move data into separate tables (2NF)
- Eliminate transitive dependencies β create foreign key relationships (3NF)
- Analyze candidate keys and apply BCNF if needed
π Denormalization (reverse process)
Sometimes, you denormalize for:
- Reporting
- Joins performance
- Simplified queries
Use carefully, only when performance justifies it.
Leave a Reply