Start here
You can keep a whole shop in one big table. This feels easy at first. Later it breaks in three ways.
Normalization fixes this. You split one wide table into small tables. Each fact is then stored once.
A fact is one piece of information, for example “Asha lives in IN”.
Denormalization is the reverse. You join small tables back into one wide table, on purpose.
One wide table
Here is the shop from the earlier chapters in one table. It is called shop_wide. One row is one item on one order.
The product_id column is the number of the product, as in the entities chapter. The product_name column is its name, to keep the rows easy to read.
| order_id | product_id | product_name | quantity | customer_id | name | country | status | price |
|---|---|---|---|---|---|---|---|---|
| 101 | 1 | Pen | 2 | 1 | Asha | IN | paid | 5.00 |
| 101 | 2 | Notebook | 1 | 1 | Asha | IN | paid | 30.00 |
| 102 | 3 | Bag | 1 | 1 | Asha | IN | paid | 25.00 |
| 103 | 4 | Lamp | 1 | 2 | Ben | GB | refunded | 60.00 |
| 104 | 1 | Pen | 3 | 3 | Carla | US | paid | 5.00 |
| 106 | 5 | Chair | 1 | 4 | Dev | NULL | paid | 20.00 |
| 106 | 4 | Lamp | 1 | 4 | Dev | NULL | paid | 60.00 |
The key is two columns together: order_id and product_id. Chapter 3 called this a composite key.
Look at the repeats. Asha is in three rows, so her country IN is stored three times. Pen costs 5.00 in two rows, and Lamp costs 60.00 in two rows.
Two things from the shop are missing. Omar (customer 5) has no order, and order 105 has no items. This table has no place for them.
What you will learn
- See the three ways one wide table breaks.
- Find what depends on what.
- Split the table in three steps (the normal forms).
- Check that the split loses nothing.
- See why analytics joins tables back into one, and what that costs.
