intermediate#Database Normalization#Database Design
Understanding Database Normalization
Learn the principles of database normalization and how to apply them to your database design.
Introduction to Database Normalization
Database normalization is the process of organizing the data in a database to minimize data redundancy and improve data integrity. There are several principles of database normalization, including first normal form (1NF), second normal form (2NF), and third normal form (3NF).
First Normal Form (1NF)
A table is in 1NF if each cell in the table contains a single value. For example, consider the following table:
| Customer ID | Name | Orders |
|---|---|---|
| 1 | John Doe | Order 1, Order 2 |
| 2 | Jane Doe | Order 3 |
This table is not in 1NF because the Orders column contains multiple values. To normalize this table, you can create a separate table for orders: | ||
| Customer ID | Order ID | |
| --- | --- | |
| 1 | 1 | |
| 1 | 2 | |
| 2 | 3 |
Second Normal Form (2NF)
A table is in 2NF if it is in 1NF and each non-key attribute in the table depends on the entire primary key. For example, consider the following table:
| Order ID | Customer ID | Order Date |
|---|---|---|
| 1 | 1 | 2022-01-01 |
| 2 | 1 | 2022-01-15 |
| 3 | 2 | 2022-02-01 |
This table is not in 2NF because the Order Date column depends only on the Order ID column, not the Customer ID column. To normalize this table, you can create a separate table for orders: | ||
| Order ID | Order Date | |
| --- | --- | |
| 1 | 2022-01-01 | |
| 2 | 2022-01-15 | |
| 3 | 2022-02-01 |
Third Normal Form (3NF)
A table is in 3NF if it is in 2NF and there are no transitive dependencies. For example, consider the following table:
| Customer ID | Name | Salesperson |
|---|---|---|
| 1 | John Doe | Jane Smith |
| 2 | Jane Doe | John Smith |
This table is not in 3NF because the Salesperson column depends on the Customer ID column, which depends on the Name column. To normalize this table, you can create a separate table for salespeople: | ||
| Salesperson ID | Name | |
| --- | --- | |
| 1 | Jane Smith | |
| 2 | John Smith |