Database normalization is the process of organizing the data in a database to minimize data redundancy and dependency. Normalization involves dividing large tables into smaller tables and linking them through relationships. The main goal of normalization is to ensure that each piece of data is stored in one place and one place only, which helps to eliminate data redundancy and improve data integrity.
There are several normal forms, each with its own set of rules and guidelines. Here are the most common normal forms:
First Normal Form (1NF)
The first normal form, also known as 1NF, is the most basic level of normalization. A table is in 1NF if it meets the following conditions:
- Each row is unique: No duplicate rows are allowed.
- Each column contains only atomic values: Each column must contain a single value, not a list or a set of values.
- No repeating groups: No column can contain a repeating group of values.
In other words, each cell in the table must contain a single value, and there must be no duplicate rows.
Example:
Before 1NF:
| Customer ID | Name | Phone Numbers |
|---|
| 1 | John Smith | 123-4567, 890-1234 |
After 1NF:
| Customer ID | Name | Phone Number |
|---|
| 1 | John Smith | 123-4567 |
| 1 | John Smith | 890-1234 |
Second Normal Form (2NF)
A table is in 2NF if it meets the following conditions:
- It is in 1NF: The table must already be in 1NF.
- All non-key attributes depend on the entire primary key: If a table has a composite primary key (i.e., a primary key made up of multiple columns), then all non-key attributes must depend on the entire primary key.
In other words, if a table has a composite primary key, then each non-key attribute must depend on all the columns in the primary key.
Example:
Before 2NF:
| Order ID | Product ID | Order Date | Product Name |
|---|
| 1 | 1 | 2022-01-01 | Product A |
| 1 | 2 | 2022-01-01 | Product B |
| 2 | 1 | 2022-01-15 | Product A |
After 2NF:
| Order ID | Order Date |
|---|
| 1 | 2022-01-01 |
| 2 | 2022-01-15 |
| Order Item ID | Order ID | Product ID |
|---|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| Product ID | Product Name |
|---|
| 1 | Product A |
| 2 | Product B |
Third Normal Form (3NF)
A table is in 3NF if it meets the following conditions:
- It is in 2NF: The table must already be in 2NF.
- If a table is in 2NF, and a non-key attribute depends on another non-key attribute, then it should be moved to a separate table: If a non-key attribute depends on another non-key attribute, then it should be moved to a separate table.
In other words, if a table has a non-key attribute that depends on another non-key attribute, then it should be moved to a separate table to avoid transitive dependency.
Example:
Before 3NF:
| Order ID | Order Date | Salesperson ID | Salesperson Name |
|---|
| 1 | 2022-01-01 | 1 | John Smith |
| 2 | 2022-01-15 | 1 | John Smith |
| 3 | 2022-02-01 | 2 | Jane Doe |
After 3NF:
| Order ID | Order Date | Salesperson ID |
|---|
| 1 | 2022-01-01 | 1 |
| 2 | 2022-01-15 | 1 |
| 3 | 2022-02-01 | 2 |
| Salesperson ID | Salesperson Name |
|---|
| 1 | John Smith |
| 2 | Jane Doe |
Higher Normal Forms
There are also higher normal forms, such as:
- Boyce-Codd Normal Form (BCNF): A table is in BCNF if and only if it is in 3NF and there are no transitive dependencies.
- Fourth Normal Form (4NF): A table is in 4NF if and only if it is in BCNF and there are no multi-level dependencies.
- Fifth Normal Form (5NF): A table is in 5NF if and only if it is in 4NF and there are no join dependencies.
However, these higher normal forms are less commonly used in practice, and 3NF is often considered sufficient for most database design purposes.
In summary, the main normal forms are:
- First Normal Form (1NF): Each row is unique, each column contains only atomic values, and no repeating groups.
- Second Normal Form (2NF): A table is in 1NF, and all non-key attributes depend on the entire primary key.
- Third Normal Form (3NF): A table is in 2NF, and if a non-key attribute depends on another non-key attribute, then it should be moved to a separate table.
By following these normal forms, you can ensure that your database design is robust, scalable, and free from data redundancy and dependency issues.