ER Diagrams & Modelling Relationships
The design process
- List the nouns in your requirements → candidate entities (tables): user, expense, category, account, bill
- List their properties → attributes (columns)
- Find the verbs → relationships: a user logs expenses, an expense belongs to a category
- Decide the cardinality of each relationship (1:1, 1:N, M:N)
- Choose keys, data types and constraints
- Normalize (lesson 10.3), then review with real queries in mind
ER diagram (Entity–Relationship)
- Entity → a box with the table name and its columns
- Relationship → a line between boxes
- Crow's foot notation shows cardinality:
||exactly one|ozero or one|<one or manyo<zero or many
users ||──o< expenses >o──|| categories
users ||──o< accounts
users ||──o< bills
categories ||──o< categories (parent → children)
Turning relationships into tables
| Relationship | Implementation |
|---|---|
| 1:N | FK on the "many" side |
| 1:1 | FK + UNIQUE (or share the primary key) |
| M:N | junction table with two FKs (+ extra columns like share, joined_at) |
| Optional | nullable FK |
| Required | NOT NULL FK |
Tools
dbdiagram.io (write DBML text → diagram), drawSQL, Lucidchart, draw.io, Supabase Schema Visualizer, and pgAdmin/DBeaver can generate diagrams from an existing database.
Questions to ask while modelling
- Can this value have several entries? (phones, tags, addresses → a separate table or an array)
- Does it change over time and do we need the history? (prices, addresses → history table)
- Who owns each row? (multi-user apps:
user_ideverywhere) - What will the most common queries be?
Example
-- From an ER sketch to tables: a "shared trips" feature
-- users ||──o< trip_members >o──|| trips (many-to-many)
-- trips ||──o< trip_expenses (one-to-many)
-- trip_expenses >o──|| users (paid_by) (many-to-one)
CREATE TABLE trips (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL,
starts_on DATE,
ends_on DATE,
created_by INT NOT NULL REFERENCES users(id),
CHECK (ends_on >= starts_on)
);
CREATE TABLE trip_members ( -- junction table (M:N)
trip_id INT REFERENCES trips(id) ON DELETE CASCADE,
user_id INT REFERENCES users(id) ON DELETE CASCADE,
role TEXT NOT NULL DEFAULT 'member' CHECK (role IN ('owner', 'member')),
joined_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (trip_id, user_id)
);
CREATE TABLE trip_expenses ( -- 1:N from trips
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
trip_id INT NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
paid_by INT NOT NULL REFERENCES users(id),
title TEXT NOT NULL,
amount NUMERIC(10,2) NOT NULL CHECK (amount > 0)
);
INSERT INTO trips (name, starts_on, ends_on, created_by) VALUES ('Pokhara weekend', '2026-12-05', '2026-12-07', 1);
INSERT INTO trip_members (trip_id, user_id, role) VALUES (1, 1, 'owner'), (1, 2, 'member'), (1, 3, 'member');
INSERT INTO trip_expenses (trip_id, paid_by, title, amount) VALUES
(1, 1, 'Hotel', 9000), (1, 2, 'Boating', 1500), (1, 3, 'Dinner', 3000);
-- Who paid what, and what each member owes (equal split)
WITH totals AS (
SELECT (SELECT sum(amount) FROM trip_expenses WHERE trip_id = 1) AS total, -- 13500
(SELECT count(*) FROM trip_members WHERE trip_id = 1) AS people -- 3
)
SELECT u.name,
COALESCE(sum(te.amount), 0) AS paid,
round((SELECT total / people FROM totals), 2) AS fair_share,
COALESCE(sum(te.amount), 0) - round((SELECT total / people FROM totals), 2) AS balance
FROM trip_members m
JOIN users u ON u.id = m.user_id
LEFT JOIN trip_expenses te ON te.trip_id = m.trip_id AND te.paid_by = m.user_id
WHERE m.trip_id = 1
GROUP BY u.name
ORDER BY balance DESC;
-- Asha +4500 (is owed), Sita -1500, Ravi -3000 (owe money)