Sikau

Databases and SQL · Day 11 · about 30 min

Good table design

Normalisation: each fact in one place.

The spreadsheet trap

A single big table with customer_name, customer_phone, dish1, dish2, dish3… looks simple — until a customer changes phone number in one row but not the others, or someone orders four dishes. Normalisation splits data so each fact lives in exactly one place.

Three questions

  • One value per cell — no lists like 'momo, tea' or columns like dish1, dish2. Use rows (and junction tables).
  • Every column describes the whole key — in order_items(order_id, dish_id, qty, dish_price?) the dish's price describes the dish, not the pair → it belongs in dishes.
  • No facts about other facts — orders(customer_id, customer_city?) → city describes the customer → move it to customers.

Sometimes you deliberately store a copy (e.g. the price *at the time of the order*) — that's a different fact, so it's fine. Know why you're duplicating.

The rest of this lesson

  • Anomalies: what goes wrongexplain
  • Splitting a wide tableexplain
  • Spot the problempredict
  • Name the anomalymatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 17 lessons of Databases and SQL