The Relational Model
Tables, rows and columns as relations; keys (primary, candidate, foreign); integrity constraints; relational algebra; and why the relational model has lasted 50 years.
A database management system (DBMS) stores data so many users and programs can query and update it safely, concurrently and durably. The dominant model since the 1970s is E. F. Codd's relational model: data lives in tables (relations), and you ask what you want in a declarative language (SQL) instead of writing how to find it.
Vocabulary
| Formal term | Everyday term |
|---|---|
| Relation | Table |
| Tuple | Row (record) |
| Attribute | Column (field) |
| Domain | The set of allowed values (a column's type) |
| Schema | The table's structure: name, columns, types, constraints |
| Cardinality / degree | Number of rows / number of columns |
A relation is a set: rows have no inherent order, and (in theory) no duplicates. SQL relaxes this — tables can contain duplicates and ORDER BY imposes an order — but thinking in sets is the key to writing good SQL.
Keys
| Key | Definition | Example |
|---|---|---|
| Super key | Any set of columns that uniquely identifies a row | {id, name} |
| Candidate key | A minimal super key | {id}, {email} |
| Primary key | The candidate key chosen as the main identifier; NOT NULL and unique | id |
| Alternate key | Other candidate keys, enforced with UNIQUE | email |
| Foreign key | Column(s) referencing another table's key | users.dept_id → departments.id |
| Composite key | A key made of several columns | (order_id, line_no) |
Integrity constraints
The database enforces rules so bad data can't get in, no matter which application writes it:
CREATE TABLE departments (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE
);
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY, -- entity integrity
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
age INT CHECK (age >= 0), -- domain integrity
dept_id BIGINT REFERENCES departments(id) -- referential integrity
ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);| Constraint | Guarantees |
|---|---|
PRIMARY KEY | Unique, not null identity |
NOT NULL, CHECK, types | Valid values |
UNIQUE | No duplicates in a column set |
FOREIGN KEY | References point at existing rows; ON DELETE CASCADE / SET NULL / RESTRICT defines what happens on delete |
Relationships
| Kind | Example | How it's modelled |
|---|---|---|
| One-to-one | user ↔ profile | FK with UNIQUE (or the same primary key) |
| One-to-many | department → users | FK on the "many" side (users.dept_id) |
| Many-to-many | students ↔ courses | A junction table enrollments(student_id, course_id) with two FKs |
Relational algebra
SQL is built on a small set of operations that take relations and return relations, so they compose:
| Operation | Symbol | SQL |
|---|---|---|
| Selection (filter rows) | σ | WHERE |
| Projection (pick columns) | π | SELECT col1, col2 |
| Join | ⋈ | JOIN … ON |
| Union / intersection / difference | ∪ ∩ − | UNION, INTERSECT, EXCEPT |
| Cartesian product | × | CROSS JOIN |
| Rename | ρ | AS |
| Aggregation (extended) | γ | GROUP BY |
The query optimiser rewrites queries using algebraic equivalences — pushing filters down before joins, reordering joins — which is why declarative SQL can beat hand-written loops (Query processing).
Why the relational model won
- Data independence — applications depend on the logical schema, not on how data is stored or indexed, so storage can change without rewriting code.
- Declarative queries — the optimiser picks the access path.
- Integrity — constraints and transactions (ACID) protect the data.
- Flexibility — new questions can be answered with new queries over existing data, without restructuring.
NoSQL systems trade some of these properties for scale or flexibility (NoSQL), and many have since added SQL back.
- Relations = tables; tuples = rows; attributes = columns; a schema defines the structure.
- Keys: candidate (minimal unique), primary (the chosen one), foreign (references).
- Constraints (PK, NOT NULL, UNIQUE, CHECK, FK) enforce integrity in the database itself.
- Many-to-many relationships need a junction table.
- Relational algebra (σ, π, ⋈, ∪, −) underlies SQL and query optimisation.
QWhat's the difference between a primary key and a unique key?easy
Both enforce uniqueness. A table has one primary key, which can't be NULL and is the row's main identity (often the clustering key). It can have many unique constraints, which in most databases allow NULLs.