Normalization to 2NF and 3NF
Definition
Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It involves dividing large tables into smaller ones and defining relationships between them.
Second Normal Form (2NF)
A table is in Second Normal Form if:
- It is in First Normal Form (1NF).
- It has no partial dependency; all non-key attributes must depend on the entire primary key.
Steps to Achieve 2NF
- Identify the primary key of the table.
- Check for partial dependencies; if any exist, separate them into a new table.
- Ensure that all non-key attributes are fully functionally dependent on the primary key.
Important: Always start by ensuring the table is in 1NF before moving to 2NF.
Example of 2NF
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
ProductID INT,
CustomerID INT,
ProductName VARCHAR(255),
Quantity INT,
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
Third Normal Form (3NF)
A table is in Third Normal Form if:
- It is in Second Normal Form (2NF).
- It has no transitive dependency; non-key attributes should not depend on other non-key attributes.
Steps to Achieve 3NF
- Ensure the table is in 2NF.
- Identify transitive dependencies and separate them into new tables.
- Ensure all attributes are dependent only on the primary key.
Tip: When designing a database, review the relationships frequently to avoid transitive dependencies.
Example of 3NF
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(255),
ContactNumber VARCHAR(50)
);
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
Best Practices
- Always analyze data requirements thoroughly before normalizing.
- Document the normalization process to maintain clarity.
- Consider performance trade-offs; sometimes denormalization may be necessary for performance.
- Regularly review and update the database design as application needs change.
FAQ
What is the purpose of normalization?
Normalization aims to eliminate data redundancy, enforce data integrity, and make database maintenance easier.
What is the difference between 2NF and 3NF?
2NF eliminates partial dependencies, while 3NF eliminates transitive dependencies within the table.
Can a table be in 3NF but not in 2NF?
No, a table must be in 2NF before it can be considered in 3NF.