Normalization in Practice
Let's walk through normalizing a real-world example step by step.
Example: Online Store
UNNORMALIZED: Orders Table
βββββββ¬βββββββββ¬ββββββββββββββββββββββ¬ββββββββββ¬βββββββββββββ
βOrderβCustomerβProducts βQuantitiesβ Total β
βββββββΌβββββββββΌββββββββββββββββββββββΌββββββββββΌβββββββββββββ€
β 1 β Alice β Phone, Charger β 1, 2 β 600 β
β 2 β Bob β Laptop β 1 β 1200 β
β 3 β Alice β Keyboard, Mouse β 1, 3 β 130 β
βββββββ΄βββββββββ΄ββββββββββββββββββββββ΄ββββββββββ΄βββββββββββββ
Step 1: Convert to 1NF
1NF: Atomic values, no repeating groups
βββββββ¬βββββββββ¬βββββββββββ¬ββββββββ¬βββββββββ
βOrderβCustomerβ Product β Qty β Price β
βββββββΌβββββββββΌβββββββββββΌββββββββΌβββββββββ€
β 1 β Alice β Phone β 1 β 500 β
β 1 β Alice β Charger β 2 β 50 β
β 2 β Bob β Laptop β 1 β 1200 β
β 3 β Alice β Keyboard β 1 β 100 β
β 3 β Alice β Mouse β 3 β 10 β
βββββββ΄βββββββββ΄βββββββββββ΄ββββββββ΄βββββββββ
Step 2: Convert to 2NF
2NF: Remove partial dependencies (PK = OrderID + Product)
Orders OrderItems Products
βββββββ¬βββββββββ βββββββ¬ββββββββββ¬ββββββ βββββββββββ¬βββββββ
β ID βCustomerβ βOrderβProduct β Qty β β Name βPrice β
βββββββΌβββββββββ€ βββββββΌββββββββββΌββββββ€ βββββββββββΌβββββββ€
β 1 β Alice β β 1 β Phone β 1 β β Phone β 500 β
β 2 β Bob β β 1 β Charger β 2 β β Charger β 50 β
β 3 β Alice β β 2 β Laptop β 1 β β Laptop β 1200 β
βββββββ΄βββββββββ β 3 β Keyboardβ 1 β β Keyboardβ 100 β
β 3 β Mouse β 3 β β Mouse β 10 β
βββββββ΄ββββββββββ΄ββββββ βββββββββββ΄βββββββ
Step 3: Convert to 3NF
3NF: Remove transitive dependencies
If Customers has CustomerID β CustomerName β CustomerCity β CityZip
Fix: Separate Customers and Cities tables
Customers Orders OrderItems Products
ββββββ¬βββββββ¬ββββββ ββββββ¬ββββββ βββββββ¬βββββββ¬ββββ ββββββ¬ββββββ
β ID β Name βCity β β ID βCust β βOrderβProd βQtyβ β ID βName β
ββββββΌβββββββΌββββββ€ ββββββΌββββββ€ βββββββΌβββββββΌββββ€ ββββββΌββββββ€
β 1 βAlice β NYC β β 1 β 1 β β 1 βPhone β 1 β β 1 βPhoneβ
β 2 βBob β LA β β 2 β 2 β β 1 βCharg β 2 β β 2 βLaptopβ
ββββββ΄βββββββ΄ββββββ β 3 β 1 β β 2 βLaptopβ 1 β ββββββ΄ββββββ
ββββββ΄ββββββ β 3 βKeybd β 1 β
β 3 βMouse β 3 β
βββββββ΄βββββββ΄ββββ