Topic 059 / Core
Data modeling and entity relationships
Chapter 5.1 · Data Modeling and Relational Databases
What and why
Before any table exists, the real-world things a system tracks must be identified as entities with attributes, connected by relationships of a known cardinality (1:1, 1:N, M:N). Data modeling is the deliberate act of doing this before writing SQL, so the schema reflects the domain rather than whatever query was written first. Builds on: → 58 (database fundamentals).
Diagram
CUSTOMER (1) ───places──► (N) ORDER (1) ───contains──► (N) ORDER_ITEM (N) ◄──of── (1) PRODUCT
How it works
- List the entities the domain cares about (customer, order, product).
- List each entity's attributes (name, email; order date, status).
- Identify relationships and their cardinality — a customer places many orders (1:N); an order contains many products and a product appears in many orders (M:N, resolved by a join table: order_item).
- Translate entities into tables, relationships into foreign keys (and join tables for M:N), and add a primary key to every table.
- Add history or audit tables wherever the domain cares about who changed what, when — not every table needs one.
Practical example
CREATE TABLE customer (id SERIAL PRIMARY KEY, email TEXT UNIQUE NOT NULL);
CREATE TABLE product (id SERIAL PRIMARY KEY, name TEXT NOT NULL, price_cents INT NOT NULL);
CREATE TABLE "order" (
id SERIAL PRIMARY KEY,
customer_id INT NOT NULL REFERENCES customer(id),
status TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE order_item (
order_id INT NOT NULL REFERENCES "order"(id),
product_id INT NOT NULL REFERENCES product(id),
quantity INT NOT NULL,
PRIMARY KEY (order_id, product_id)
);
When to use it
- Use when: data is structured, relationships matter, and multiple queries will need to join or filter across entities.
- Avoid when: the "relationship" is really one self-contained document with no cross-entity queries — a document shape may fit better (→ 61).
- Failure scenarios: missing or wrong cardinality (modeling M:N as 1:N) forces awkward duplicate rows or lost data later; no clear owning entity for a fact, so it gets stored in two places and drifts (→ 81).
- Simpler alternative: for a genuinely tiny app, fewer tables with looser constraints — but name that trade-off, don't stumble into it.
Practice
(15–20 min, no lab needed). Draw the entity-relationship diagram for a
small library system: members, books, and loans. Get the cardinality of each relationship
right (a book may have many copies; a loan links one copy to one member for a period) before
writing any CREATE TABLE. Done when every relationship's cardinality is marked and the
copy-vs-book distinction survives into the tables.
Key takeaway
Model entities and relationships before tables; cardinality mistakes here become anomalies or awkward joins later. Done when you can: draw an ER diagram for a small domain and defend each relationship's cardinality. Used next in → 60, 63.
References
None — this topic makes no external claims.
Source directory