Databases & SQL

Lesson 5 of 7

Lesson 5 — Normalisation, Constraints and Good Design

1NF to 3NF, constraints, indexes — how to design a schema that stays correct as it grows.

🟡 Intermediate 90 XP

Learn it

Normalisation removes duplicated data so each fact is stored exactly once.

1NF: no repeating groups — one value per cell. 2NF: no partial dependency on part of a composite key. 3NF: no column depending on another non-key column.

Constraints (NOT NULL, UNIQUE, CHECK, FOREIGN KEY) let the database refuse bad data.

Sometimes you denormalise on purpose — copying a value to make a heavy read query fast.

Key terms

Normalisation
Organising tables so each fact is stored once.
Update anomaly
Inconsistency caused by the same fact living in many rows.
3NF
No non-key column depends on another non-key column.
CHECK constraint
A rule the database enforces on column values.
Denormalisation
Deliberately duplicating data to speed up reads.

Normalise a messy orders sheet

One table holds order_id, customer_name, customer_email, product_name, product_price, qty.

  1. 1Spot repetition: Customer email repeats on every order; product price repeats on every line.
  2. 21NF: Split 'products: pen, pad, ruler' in one cell into separate order_line rows.
  3. 32NF: product_price depends only on product, not on (order_id, product_id) — move it to product.
  4. 43NF: customer_email depends on customer_name, not the order — move it to customer.
  5. 5Result: customer, product, orders and order_line — each fact once.

Constraints doing the work

sqlCREATE TABLE order_line (
  order_id   INTEGER NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
  product_id INTEGER NOT NULL REFERENCES product(id),
  qty        INTEGER NOT NULL CHECK (qty > 0),
  unit_price NUMERIC(8,2) NOT NULL CHECK (unit_price >= 0),
  PRIMARY KEY (order_id, product_id)
);

CREATE INDEX idx_order_line_product ON order_line(product_id);

unit_price is copied onto the line on purpose — a historic invoice must not change when the product's price changes later. That is denormalisation with a reason.

Try it

Put the normalisation workflow in the right order.

  • 1Remove transitive dependencies between non-key columns (3NF)
  • 2Remove partial dependencies on a composite key (2NF)
  • 3Add constraints and indexes
  • 4Write down every attribute in one flat table
  • 5Remove repeating groups (1NF)

Challenge

This table is unnormalised: booking(id, guest_name, guest_phone, room_no, room_type, room_rate, nights, hotel_name, hotel_city). Normalise it to 3NF, listing each table with keys, and justify one place you would denormalise.

Pick whichever way suits you — every mode earns the same bonus XP.

Write at least 40 more characters to submit.

Mark your own work

Guided walkthrough — 0/3 clues revealed

  1. Clue 1 locked — reveal it only if you get stuck.
  2. Clue 2 locked — reveal it only if you get stuck.
  3. Clue 3 locked — reveal it only if you get stuck.

Each clue costs 5 XP (never below 23 XP). You'd earn 45 XP right now.

Extension: Add a CHECK that nights > 0 and a constraint preventing two bookings overlapping in the same room.

Quiz time

Question 1 of 4Score 0

3NF removes which kind of dependency?