Normalization
Normalization
### **1NF (First Normal Form)**: Eliminate repeating groups and ensure that each
column contains atomic values.
|---------|--------------|-------------------------------------|
Each column must have atomic values, meaning no multiple values in a single field. In
this case, we separate the items into individual rows.
|---------|--------------|-----------|
|1 | John | Burger |
|1 | John | Fries |
|1 | John | Coke |
|2 | Alice | Pizza |
|2 | Alice | Salad |
|2 | Alice | Water |
|3 | Bob | Burger |
|3 | Bob | Fries |
|3 | Bob | Soda |
### **2NF (Second Normal Form)**: Achieve 1NF and remove partial dependencies. All
non-prime attributes must depend on the entire primary key.
Let's assume the `OrderID` and `CustomerName` together make the composite primary
key. The problem is that the `CustomerName` is dependent only on the `OrderID`, not
the whole key. Therefore, we split the table into two:
**Orders Table:**
| OrderID | CustomerName |
|---------|--------------|
|1 | John |
|2 | Alice |
|3 | Bob |
**OrderItems Table:**
| OrderID | Item |
|---------|-----------|
|1 | Burger |
|1 | Fries |
|1 | Coke |
|2 | Pizza |
|2 | Salad |
|2 | Water |
|3 | Burger |
|3 | Fries |
|3 | Soda |
|3 | Ice Cream |
### **3NF (Third Normal Form)**: Achieve 2NF and remove transitive dependencies.
Non-prime attributes should not depend on other non-prime attributes.
Let’s assume that the **Orders** table also includes the `WaiterID` who served the
order, but the **WaiterName** depends on the `WaiterID`. This creates a transitive
dependency where `WaiterName` depends on `WaiterID`, which in turn depends on the
`OrderID`. We separate this into a new table:
**Orders Table:**
|---------|--------------|----------|
|1 | John | 101 |
|2 | Alice | 102 |
|3 | Bob | 101 |
**OrderItems Table:**
| OrderID | Item |
|---------|-----------|
|1 | Burger |
|1 | Fries |
|1 | Coke |
|2 | Pizza |
|2 | Salad |
|2 | Water |
|3 | Burger |
|3 | Fries |
|3 | Soda |
|3 | Ice Cream |
**Waiters Table:**
| WaiterID | WaiterName |
|----------|------------|
| 101 | Sam |
| 102 | Lisa |
### **BCNF (Boyce-Codd Normal Form)**: Every determinant must be a candidate key.
**Orders Table:**
|---------|--------------|----------|
|1 | John | 101 |
|2 | Alice | 102 |
|3 | Bob | 101 |
**OrderItems Table:**
|---------|-----------|---------|
|1 | Burger | 1 |
|1 | Fries |2 |
|1 | Coke |3 |
|2 | Pizza |4 |
|2 | Salad |5 |
|2 | Water |6 |
|3 | Burger | 1 |
|3 | Fries |2 |
|3 | Soda |7 |
|3 | Ice Cream | 8 |
**Waiters Table:**
| WaiterID | WaiterName |
|----------|------------|
| 101 | Sam |
| 102 | Lisa |
|---------|-----------|-------|
|1 | Burger | 5.99 |
|2 | Fries | 2.99 |
|3 | Coke | 1.50 |
|4 | Pizza | 7.99 |
|5 | Salad | 3.99 |
|6 | Water | 1.00 |
|7 | Soda | 1.75 |
Suppose a customer can order multiple items and also provide multiple special requests
(e.g., "extra cheese" or "no onions"). These requests are independent and should be
handled separately.
**Orders Table:**
|---------|--------------|----------|
|1 | John | 101 |
|2 | Alice | 102 |
|3 | Bob | 101 |
**OrderItems Table:**
|---------|-----------|---------|
|1 | Burger | 1 |
|1 | Fries |2 |
|1 | Coke |3 |
|2 | Pizza |4 |
|2 | Salad |5 |
|2 | Water |6 |
|3 | Burger | 1 |
|3 | Fries |2 |
|3 | Soda |7 |
|3 | Ice Cream | 8 |
**Waiters Table:**
| WaiterID | WaiterName |
|----------|------------|
| 101 | Sam |
| 102 | Lisa |
**ItemPrices Table:**
|---------|-----------|-------|
|1 | Burger | 5.99 |
|2 | Fries | 2.99 |
|3 | Coke | 1.50 |
|4 | Pizza | 7.99 |
|5 | Salad | 3.99 |
|6 | Water | 1.00 |
|7 | Soda | 1.75 |
| OrderID | Request |
|---------|--------------------|
|1 | Extra Cheese |
|1 | No Onions |
|2 | No Ice in Drink |
|3 | No Ice in Drink |
### **5NF (Fifth Normal Form)**: Decompose tables to eliminate join dependencies.
If we further normalize by separating out a **Special Requests** table and ensuring the
decomposed relationships do not have redundancy when joined, we achieve 5NF.
At this stage, the database would be fully normalized, ensuring efficient storage and
minimal redundancy.
**Orders Table:**
|---------|--------------|----------|
|1 | John | 101 |
|2 | Alice | 102 |
|3 | Bob | 101 |
**OrderItems Table:**
| OrderID | ItemID |
|---------|--------|
|1 |1 |
|1 |2 |
|1 |3 |
|2 |4 |
|2 |5 |
|2 |6 |
|3 |1 |
|3 |2 |
|3 |7 |
|3 |8 |
**Waiters Table:**
| WaiterID | WaiterName |
|----------|------------|
| 101 | Sam |
| 102 | Lisa |
**ItemPrices Table:**
|--------|-----------|-------|
|1 | Burger | 5.99 |
|2 | Fries | 2.99 |
|3 | Coke | 1.50 |
|4 | Pizza | 7.99 |
|5 | Salad | 3.99 |
|6 | Water | 1.00 |
|7 | Soda | 1.75 |
**SpecialRequests Table:**
| OrderID | Request |
|---------|--------------------|
|1 | Extra Cheese |
|1 | No Onions |
|2 | No Ice in Drink |
|3 | No Ice in Drink |
### Conclusion:
Each normal form reduces redundancy and ensures data integrity. The restaurant
example shows how business data such as orders, items, and special requests can be
normalized from **1NF to 5NF**, ensuring efficient data storage and query
performance.