Normalization is a systematic approach of decomposing tables to eliminate data redundancy and undesirable characteristics like Insertion, Update, and Deletion Anomalies. It is a multi-step process that puts data into tabular form, removing duplicated data from the relation tables. The process of normalization is divided into several normal forms.
Objectives
- Understand the concept of normal forms.
- Learn about the different types of normal forms.
- Apply normalization techniques to database schema design.
Key Concepts
- First Normal Form (1NF)
A table is in the first normal form if:
- It contains only atomic (indivisible) values.
- Each column contains values of a single type.
- Each column contains unique values.
Example:
Consider the following table that is not in 1NF:
StudentID | Name | Courses |
---|---|---|
1 | John Doe | Math, Science |
2 | Jane Smith | English, History |
To convert it to 1NF, we need to split the multi-valued attribute into separate rows:
StudentID | Name | Course |
---|---|---|
1 | John Doe | Math |
1 | John Doe | Science |
2 | Jane Smith | English |
2 | Jane Smith | History |
- Second Normal Form (2NF)
A table is in the second normal form if:
- It is in 1NF.
- All non-key attributes are fully functional dependent on the primary key.
Example:
Consider the following table that is in 1NF but not in 2NF:
StudentID | CourseID | CourseName | Instructor |
---|---|---|---|
1 | 101 | Math | Dr. Smith |
1 | 102 | Science | Dr. Brown |
2 | 101 | Math | Dr. Smith |
2 | 103 | History | Dr. White |
To convert it to 2NF, we need to remove partial dependencies by creating separate tables:
Students Table:
StudentID | CourseID |
---|---|
1 | 101 |
1 | 102 |
2 | 101 |
2 | 103 |
Courses Table:
CourseID | CourseName | Instructor |
---|---|---|
101 | Math | Dr. Smith |
102 | Science | Dr. Brown |
103 | History | Dr. White |
- Third Normal Form (3NF)
A table is in the third normal form if:
- It is in 2NF.
- All the attributes are functionally dependent only on the primary key.
Example:
Consider the following table that is in 2NF but not in 3NF:
StudentID | CourseID | Instructor | InstructorOffice |
---|---|---|---|
1 | 101 | Dr. Smith | Room 101 |
1 | 102 | Dr. Brown | Room 102 |
2 | 101 | Dr. Smith | Room 101 |
2 | 103 | Dr. White | Room 103 |
To convert it to 3NF, we need to remove transitive dependencies by creating separate tables:
Students Table:
StudentID | CourseID |
---|---|
1 | 101 |
1 | 102 |
2 | 101 |
2 | 103 |
Courses Table:
CourseID | Instructor |
---|---|
101 | Dr. Smith |
102 | Dr. Brown |
103 | Dr. White |
Instructors Table:
Instructor | InstructorOffice |
---|---|
Dr. Smith | Room 101 |
Dr. Brown | Room 102 |
Dr. White | Room 103 |
- Boyce-Codd Normal Form (BCNF)
A table is in BCNF if:
- It is in 3NF.
- For every functional dependency (X → Y), X should be a super key.
Example:
Consider the following table that is in 3NF but not in BCNF:
StudentID | CourseID | Instructor |
---|---|---|
1 | 101 | Dr. Smith |
1 | 102 | Dr. Brown |
2 | 101 | Dr. Smith |
2 | 103 | Dr. White |
To convert it to BCNF, we need to ensure that every determinant is a candidate key:
Students Table:
StudentID | CourseID |
---|---|
1 | 101 |
1 | 102 |
2 | 101 |
2 | 103 |
Courses Table:
CourseID | Instructor |
---|---|
101 | Dr. Smith |
102 | Dr. Brown |
103 | Dr. White |
Practical Exercises
Exercise 1: Convert to 1NF
Given the following table, convert it to 1NF:
EmployeeID | Name | Skills |
---|---|---|
1 | Alice | Java, Python |
2 | Bob | SQL, JavaScript |
Solution:
EmployeeID | Name | Skill |
---|---|---|
1 | Alice | Java |
1 | Alice | Python |
2 | Bob | SQL |
2 | Bob | JavaScript |
Exercise 2: Convert to 2NF
Given the following table in 1NF, convert it to 2NF:
OrderID | ProductID | ProductName | Quantity |
---|---|---|---|
1 | 101 | Laptop | 2 |
1 | 102 | Mouse | 5 |
2 | 101 | Laptop | 1 |
2 | 103 | Keyboard | 3 |
Solution:
Orders Table:
OrderID | ProductID | Quantity |
---|---|---|
1 | 101 | 2 |
1 | 102 | 5 |
2 | 101 | 1 |
2 | 103 | 3 |
Products Table:
ProductID | ProductName |
---|---|
101 | Laptop |
102 | Mouse |
103 | Keyboard |
Exercise 3: Convert to 3NF
Given the following table in 2NF, convert it to 3NF:
OrderID | ProductID | SupplierID | SupplierName |
---|---|---|---|
1 | 101 | 201 | ABC Corp |
1 | 102 | 202 | XYZ Inc |
2 | 101 | 201 | ABC Corp |
2 | 103 | 203 | DEF Ltd |
Solution:
Orders Table:
OrderID | ProductID | SupplierID |
---|---|---|
1 | 101 | 201 |
1 | 102 | 202 |
2 | 101 | 201 |
2 | 103 | 203 |
Suppliers Table:
SupplierID | SupplierName |
---|---|
201 | ABC Corp |
202 | XYZ Inc |
203 | DEF Ltd |
Conclusion
Understanding and applying normal forms is crucial for designing efficient and reliable databases. By following the principles of normalization, you can ensure that your database is free from redundancy and anomalies, making it easier to maintain and query. In the next section, we will delve into the normalization process in more detail.
Fundamentals of Databases
Module 1: Introduction to Databases
Module 2: Relational Databases
Module 3: Non-Relational Databases
- Introduction to NoSQL
- Types of NoSQL Databases
- Comparison between Relational and Non-Relational Databases
Module 4: Schema Design
- Principles of Schema Design
- Entity-Relationship (ER) Diagrams
- Transformation of ER Diagrams to Relational Schemas