Designing tables: normal forms in plain words
A table that copies a fact into many rows works fine until that fact changes, and then it gives two answers to one question. Normal forms come down to one rule, one fact in one place, and it is worth knowing when to break it.
01 A bookings table that looks fine
A small hotel starts with one table, the way it would in a spreadsheet: one row per booking, with everything you might want to show on the booking page.
It works, and every query is a simple SELECT. But look at what the rows actually say. Some cells are not about the booking at all: they repeat a fact about something else.
- Ana’s email and name ×3
- “a double costs 120” ×3
- “a single costs 80” ×2
Ana’s name is a fact about Ana, not about booking 3. The price is a fact about the room type, not about room 102. Each is written down once per booking.
02 What goes wrong when a fact is copied
A copy is harmless until the fact changes. Then every copy has to change together, and the database has no idea they are meant to.
- a copy of the same fact
- just updated
- being read
- contradicts another copy
Ana tells the hotel her surname is now Sousa. Her name is stored three times, once in each of her bookings: 1, 3 and 5.
That is a real run on Postgres 16. The same table fails in two more ways, and together they are the classic anomalies:
| Anomaly | What you try | What happens |
|---|---|---|
| Update | Rename Ana, or raise the price of a double | You must find and change every copy. Miss one and the table contradicts itself. |
| Insert | Add room 301, a suite, before anyone books it | There is no row to put it in without inventing a booking with empty guest columns. |
| Delete | Cancel booking 4, Cy’s only booking | Cy’s email and name vanish with it. Had it been the last booking of a single, the fact that singles cost 80 would have gone too. |
03 The rule: every fact depends on the key
Normalisation is one idea: each fact is stored in one place, in the table whose key it describes. A table’s key names one thing, such as one booking or one guest. Every other column should be a fact about that thing, and nothing else. The textbook normal forms are that idea applied in three passes.
| Form | Plain rule | Breaks it here | Fix |
|---|---|---|---|
| 1NF | One value per cell, no repeating columns | `nights = 'Mar 1, Mar 2'` in one cell, or `phone1`, `phone2`, `phone3` | A row per value, in its own table |
| 2NF | With a key of several columns, every other column needs the whole key | A `booking_nights (booking_id, night)` table that also holds `guest_name`, which depends on `booking_id` alone | Move the column to the table keyed by `booking_id` |
| 3NF | No column depends on another non-key column | `price` depends on `room_type`, and `room_type` on `room`; neither is about the booking | A table keyed by what the fact is about: `room_types`, `rooms` |
A quick test for any column: finish the sentence “given the key, there is exactly one…”. Given booking 3, there is exactly one check-in date: it belongs. Given booking 3 there is one guest name too, but only because there is one guest; the name is really fixed by the guest, so it belongs with the guest. There are stricter forms (BCNF, 4NF, 5NF), but a design that passes 3NF with this test is where almost all practical tables stop.
04 Split it: one table per kind of thing
Each repeated fact becomes a row in a table of its own, and the booking refers to it by key. Foreign keys (REFERENCES) make the database check that every reference points at a row that exists.
Each name, each price and each room’s type now appears exactly once. The bookings table holds only what is about a booking: who, which room, when.
Renaming Ana is now one UPDATE guests ... WHERE id = 1, and there is no second copy to miss. A suite can exist before its first booking. Cancelling Cy’s booking leaves Cy in guests. The booking page still gets its flat view, built by joins when you read:
05 Let the database keep it true
Splitting the tables only helps if the links between them cannot break. The constraints in normalised.sql turn mistakes into errors at write time, which is much cheaper than finding them in a report later. Real Postgres 16 errors:
Notice that guests is keyed by a plain id, with email as UNIQUE. Email identifies a guest today, but people change it. If email were the primary key, a change would have to rewrite every booking that points at it. A key that never changes, plus a unique constraint on the natural identifier, gives you both.
06 When to break the rule
Copying a fact on purpose is called denormalising. It is a trade, not a sin, as long as you know which copy is the truth and who keeps the others in step. Before you do it, check that it is really a copy. The price a guest paid is not the same fact as today’s price for that room type:
The price went up, and old bookings must keep what was agreed. price_per_night is a fact about the booking, frozen at the moment it was made, so storing it is normalised. Order lines with unit_price, invoices with an address: the same idea. Real denormalisation is when the copy is meant to stay equal to the original:
review_count or an order’s total, updated in the same transaction as the rows it counts (or by a trigger), so a page does not count thousands of rows on every view.bookings_flat problem again.Start normalised and denormalise only when a measured query needs it. Going the other way, from copies that have already drifted back to one truth, means deciding which of the conflicting values was right, often by hand.
07 When it bites
tags = 'wifi,sea-view,balcony'A list in one cellYou cannot index, join or constrain the parts, and LIKE '%sea%' finds the wrong things. Use a room_tags (room_no, tag) table. Postgres arrays are fine for a short list you only ever read whole.phone1, phone2, phone3Numbered columnsA guest with four phones does not fit, and “find this number” checks three columns. Put phones in their own table, one row each.entity, attribute, valueOne table for everythingStoring every field as a generic key-value row throws away types, NOT NULL and foreign keys, and every read becomes a pile of self-joins. Use real columns, or a jsonb column for the truly free-form part.age intStoring what you can computeAn age is out of date a year later. Store birth_date and compute the age when you read it. The same goes for totals you can sum cheaply.countries, regions, cities, streetsSplitting too farAn address used only as a label on a booking is one fact to that booking. Splitting it into four tables adds joins and protects nothing, because nobody updates “the street” for everyone at once.