7.1 Database Normalization (1NF, 2NF, 3NF) vs Denormalization
Understand why poorly designed tables suffer from Insert, Update, and Delete anomalies, master 1NF, 2NF, and 3NF in plain English, and know when senior engineers intentionally denormalize.
1. Why Normalize? (The 3 Data Anomalies)
Imagine storing everything in one giant table orders_flat(order_id, customer_name, customer_email, product_name, product_price):
- Update Anomaly: Aarav changes his email → you must update 200 order rows (miss 1 row and your data contradicts itself!).
- Insert Anomaly: You can't add a new product to your catalog until someone actually buys it!
- Delete Anomaly: If Aarav cancels his only order and you delete that row, you accidentally delete Aarav's account info too!
Normalization eliminates all 3 anomalies by organizing columns so every fact is stored in exactly ONE place.
2. 1NF, 2NF & 3NF Explained in Plain English
Famous Mnemonic (Bill Kent): "Every non-key column must depend on the key (1NF), the whole key (2NF), and nothing but the key (3NF)!"
| Normal Form | Plain-English Rule | Bad Example (Violates Rule) | Clean Fix |
|---|---|---|---|
| 1NF (Atomic Values) | No comma-separated lists in a single cell; every row has a PK | skills = 'Python, SQL, DSA' | Separate user_skills(user_id, skill) table (1 skill per row) |
| 2NF (No Partial Dependency) | Every non-key column depends on the entire composite PK (not just half of it) | order_items(order_id, product_id, product_name) — product_name depends only on product_id! | Move product_name into products(product_id, product_name) |
| 3NF (No Transitive Dependency) | Non-key columns must not depend on other non-key columns | users(user_id, zip_code, city) — city depends on zip_code, not user_id! | Move (zip_code, city) into a postal_codes lookup table |
-- Clean 3NF E-Commerce Schema (Zero Redundancy, Zero Anomalies!)
CREATE TABLE customers (
customer_id BIGINT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE products (
product_id BIGINT PRIMARY KEY,
product_name VARCHAR(150) NOT NULL,
current_price DECIMAL(10, 2) NOT NULL
);
CREATE TABLE orders (
order_id BIGINT PRIMARY KEY,
customer_id BIGINT NOT NULL REFERENCES customers(customer_id),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE order_items (
order_id BIGINT NOT NULL REFERENCES orders(order_id),
product_id BIGINT NOT NULL REFERENCES products(product_id),
quantity INT NOT NULL CHECK (quantity > 0),
price_at_sale DECIMAL(10, 2) NOT NULL, -- Intentionally snapshotted!
PRIMARY KEY (order_id, product_id)
);
Pro Detail: Notice
price_at_saleinorder_items! Even in a 3NF schema, you snapshot the historical price at checkout so that raisingproducts.current_pricenext year never alters last year's invoices!
3. Normalization vs Denormalization (When to Break the Rules!)
| Dimension | Normalized (3NF) | Denormalized (Controlled Redundancy) |
|---|---|---|
| Best For | OLTP (Writes, banking, orders, user profiles) | OLAP (Analytics dashboards, read-heavy feeds) |
| Write Speed & Integrity | ✅ Fast, bug-free writes (update 1 row) | ❌ Slower, riskier writes (must sync duplicated counters) |
| Read Speed | Requires JOINs across tables | ✅ Blazing-fast reads (0 JOINs!) |
| Real Example | posts + likes tables | Storing a cached like_count INT column directly on posts so Instagram doesn't COUNT(*) 1M likes on every feed load! |
4. Quick Check Your Understanding
Module 7.1 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Convert Unix epoch seconds/milliseconds to ISO-8601 and local timezones, and bulk-generate UUID v4, time-ordered UUID v7, and sortable ULIDs.
Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.