Lesson 227 of 296
Data modelling: normalisation and relationships
What you will learn
- Entities, attributes and relationships
- One-to-one, one-to-many and many-to-many
- Join tables
- Normalisation: 1NF, 2NF and 3NF in plain words
- Denormalisation and when it is right
- JSONB columns
- A worked example
On this page
Entities and relationships
Data modelling means deciding which tables, columns and relationships represent your business. Start from the nouns: entities (customer, order, product) with attributes (name, price) and relationships between them. An ER diagram (entity-relationship diagram) draws this.
Relationship types
| Type | Example | How to store |
|---|---|---|
| One-to-one | User and user profile | Foreign key with a UNIQUE constraint, or keep it in one table |
| One-to-many | One customer has many orders | Foreign key on the "many" side: orders.customer_id |
| Many-to-many | Orders contain many products; a product appears in many orders | A join table (also called junction or associative table) |
Our order_items table is the join table between orders and products. Join tables often carry their own data: quantity, unit_price_pence. Other examples: students and courses through enrolments (with a grade), users and roles through user_roles.
Normalisation
Normalisation organises tables to remove duplicated data, so each fact is stored once. Duplicated facts get out of sync (update anomalies): if the customer's email is stored on every order, changing it means updating many rows, and missing one leaves the data contradicting itself.
The normal forms in plain words:
| Form | Rule | Breaking example | Fix |
|---|---|---|---|
| 1NF | Each column holds one value; no repeating groups | A phone_numbers column containing "0777..., 0113...", or columns item1, item2, item3 | A separate table with one row per phone number or item |
| 2NF | 1NF, and every non-key column depends on the whole key (matters with composite keys) | In order_items (order_id, product_id, quantity, product_name), product_name depends only on product_id | Move product_name to products |
| 3NF | 2NF, and non-key columns depend only on the key, not on other non-key columns | orders (id, customer_id, customer_email): email depends on the customer, not the order | Keep email in customers only |
The phrase to remember: every column should depend on "the key, the whole key, and nothing but the key". Most OLTP (transaction-processing) systems aim for 3NF.
Denormalisation: on purpose
Sometimes storing data more than once is correct:
- Historical values:
order_items.unit_price_pencecopies the product price at the time of the order. If it read the current price fromproducts, old invoices would change whenever prices change. That is not duplication; it is a different fact (the price paid). - Performance: a
comment_countcolumn on posts avoids counting millions of rows on every page view; keep it correct with a transaction or a trigger. - Reporting: data warehouses use deliberately denormalised star schemas (fact and dimension tables) for fast analytics.
Denormalise deliberately, knowing how the copies will be kept in sync.
JSONB columns
PostgreSQL's jsonb stores JSON inside a row. Good for: flexible attributes that differ per row (product specifications), storing a raw webhook payload, user settings. It can be indexed (GIN) and queried (specs->>'colour' = 'red').
Avoid it for data you join on, filter heavily, or need constraints on: those belong in proper columns. "Everything in one JSON column" throws away the database's strengths.
Worked example: a course platform (like JobPrep)
Requirements: users enrol in courses; courses have ordered lessons; users record progress per lesson; a certificate is issued when a course is completed.
users (id, email UNIQUE, name, created_at)
courses (id, slug UNIQUE, title, published_at)
lessons (id, course_id FK, position, title, body, UNIQUE (course_id, position))
enrolments (user_id FK, course_id FK, enrolled_at, PRIMARY KEY (user_id, course_id))
lesson_progress (user_id FK, lesson_id FK, completed_at, attempts, PRIMARY KEY (user_id, lesson_id))
certificates (id, user_id FK, course_id FK, issued_at, code UNIQUE, UNIQUE (user_id, course_id))Decisions worth explaining:
enrolmentsis a many-to-many join table with its own data (enrolled_at).UNIQUE (course_id, position)stops two lessons sharing a position in a course.UNIQUE (user_id, course_id)on certificates prevents duplicate certificates even if two requests race.- Completion percentage is calculated (completed lessons / total lessons), not stored, unless it becomes a measured performance problem.
- Indexes on foreign keys used for lookups:
lessons (course_id),lesson_progress (lesson_id).
How to say it in an interview
"I model entities and their relationships: one-to-many with a foreign key on the many side, many-to-many with a join table that can carry its own data like quantity. I normalise to third normal form so each fact is stored once: one value per column, no columns depending on part of a composite key, and no columns depending on other non-key columns, which avoids update anomalies. I denormalise deliberately, for example storing the price paid on order items because it is a historical fact, or a cached counter for performance, and I use JSONB only for genuinely flexible data, not for things I join, filter or constrain."
Key words
| Word | Meaning |
|---|---|
| Entity | A thing the system stores (customer, order) |
| ER diagram | A drawing of entities and relationships |
| One-to-many / many-to-many | Relationship types |
| Join table | A table linking two tables in a many-to-many relationship |
| Normalisation | Organising tables so each fact is stored once |
| 1NF / 2NF / 3NF | Increasing levels of normalisation |
| Update anomaly | Duplicated data getting out of sync |
| Denormalisation | Deliberately storing data more than once |
| JSONB | PostgreSQL's binary JSON column type |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.