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.

Beginner 4 min readLesson 1 of 14 Interview Q&A

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

idnameemaildept_id1Ada Lovelaceada@x.io12Alan Turingalan@x.io23Grace Hoppergrace@x.io1
Table users: each row is a tuple, each column an attribute.
Formal termEveryday term
RelationTable
TupleRow (record)
AttributeColumn (field)
DomainThe set of allowed values (a column's type)
SchemaThe table's structure: name, columns, types, constraints
Cardinality / degreeNumber 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

KeyDefinitionExample
Super keyAny set of columns that uniquely identifies a row{id, name}
Candidate keyA minimal super key{id}, {email}
Primary keyThe candidate key chosen as the main identifier; NOT NULL and uniqueid
Alternate keyOther candidate keys, enforced with UNIQUEemail
Foreign keyColumn(s) referencing another table's keyusers.dept_id → departments.id
Composite keyA 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:

SQL
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()
);
ConstraintGuarantees
PRIMARY KEYUnique, not null identity
NOT NULL, CHECK, typesValid values
UNIQUENo duplicates in a column set
FOREIGN KEYReferences point at existing rows; ON DELETE CASCADE / SET NULL / RESTRICT defines what happens on delete

Relationships

KindExampleHow it's modelled
One-to-oneuser ↔ profileFK with UNIQUE (or the same primary key)
One-to-manydepartment → usersFK on the "many" side (users.dept_id)
Many-to-manystudents ↔ coursesA junction table enrollments(student_id, course_id) with two FKs
FKFKstudentsid · nameenrollmentsstudent_id · course_id · gradecoursesid · title

Relational algebra

SQL is built on a small set of operations that take relations and return relations, so they compose:

OperationSymbolSQL
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
πname(σdept_id=1(users))  ≡  SELECT name FROM users WHERE dept_id = 1\pi_{\text{name}}\big(\sigma_{\text{dept\_id} = 1}(\text{users})\big) \;\equiv\; \texttt{SELECT name FROM users WHERE dept\_id = 1}

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.

Key takeaways
  • 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.
Interview questions
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.