6 min read
•Question 10 of 29mediumWhat is database normalization?
Understanding normal forms.
What You'll Learn
- What normalization is
- Normal forms (1NF, 2NF, 3NF)
- When to denormalize
What is Normalization?
Organizing data to reduce redundancy and improve data integrity.
First Normal Form (1NF)
- Each column contains atomic (single) values
- No repeating groups
query.sqlSQL
-- Bad (not 1NF)
| id | name | phones |
|----|------|---------------------|
| 1 | John | 123-456, 789-012 |
-- Good (1NF)
| id | name | phone |
|----|------|---------|
| 1 | John | 123-456 |
| 1 | John | 789-012 |Second Normal Form (2NF)
- Must be in 1NF
- All non-key columns depend on the entire primary key
query.sqlSQL
-- Bad (not 2NF) - product_name depends only on product_id
| order_id | product_id | product_name | quantity |
-- Good (2NF) - separate tables
orders: | order_id | product_id | quantity |
products: | product_id | product_name |Third Normal Form (3NF)
- Must be in 2NF
- No transitive dependencies (non-key depends on non-key)
query.sqlSQL
-- Bad (not 3NF) - city depends on zip_code, not on id
| id | name | zip_code | city |
-- Good (3NF)
users: | id | name | zip_code |
locations: | zip_code | city |When to Denormalize
- Read-heavy applications
- Reporting/analytics
- Caching frequently joined data
- Performance optimization
query.sqlSQL
-- Denormalized for performance
CREATE TABLE order_summary (
order_id INT,
user_name VARCHAR(100), -- Copied from users
product_names TEXT, -- Aggregated
total DECIMAL(10,2)
);