awesome-everything RU
↑ Back to the climb

Databases

What a relation is: tables, rows, keys, and constraints

Crux The core vocabulary of the relational model — relations, tuples, candidate keys, and why declarative constraints are the model''''s enduring win.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at junior altitude — the surface
◷ 12 min

A team stores “user has many tags” as a comma-separated column. Six months later: “how many users have tag X?” — every row must be parsed. A relational design answers that question as a point lookup. The difference is the model you start with.

What a relation is

Edgar Codd’s 1970 paper defined the foundations of every SQL database since. The vocabulary is small:

  • Relation — a set of tuples sharing the same shape. The table is the SQL implementation.
  • Tuple — a single row; every tuple in a relation has the same attributes.
  • Attribute — a column; each draws values from a domain (a type).
  • Candidate key — a minimal subset of attributes that uniquely identifies every tuple. A table may have several candidate keys; one is designated the primary key.
ConceptSQL termWhat it means
RelationTableA set of rows sharing one shape
TupleRowOne record
AttributeColumnOne named typed value per row
DomainTypeThe set of valid values for an attribute
Candidate keyPRIMARY KEY / UNIQUEA minimal row identifier

Why constraints are the model’s enduring win

The relational model does not just store data — it refuses bad data. Constraints are declarative rules the engine checks before any insert, update, or delete:

  • PRIMARY KEY — uniquely identifies each row; implies NOT NULL and UNIQUE.
  • FOREIGN KEY — a column referencing a primary or unique key in another table; the engine refuses orphan rows.
  • NOT NULL — this attribute must always have a value.
  • UNIQUE — this column or column set has no duplicates across rows.
  • CHECK — an arbitrary boolean expression evaluated on every write; e.g. CHECK (amount >= 0).

Without constraints you have a key-value store with SQL syntax. Application bugs can commit bad data. With constraints, the engine refuses on behalf of every caller — application code does not have to remember the rules.

The metaphor

A relational schema is a library catalogue. Each table is a drawer (books, authors, loans). Each row is a card with the same fields. A loan card has member-id and book-id pointing at other drawers (foreign keys). The librarian (the engine) refuses to file a loan card if the book-id does not exist. The system stays coherent without humans re-checking.

A practical scenario

Sven · Origin server wants a “favourites” feature for a marketplace. Otto · Origin database asks the shape question. Sven says “user has many favourites.” Otto reaches for the model: users, items, favourites table with (user_id, item_id) as PK plus two FKs. Three DDL lines and the feature is structurally correct — no duplicates, no orphans, queryable both ways. A JSON-array shortcut breaks the moment you ask “who favourited this item.”

Another team stores addresses as comma-separated text on the user row. Six months later marketing asks “how many users in Oregon?” — every text field must be parsed. A year later finance asks “sales by state” — same problem on every order. A relational design (addresses table with structured columns) is two extra schema lines and three orders of magnitude cheaper to query.

Why this works

The cost of the relational model is the constraint check at write time and the discipline of designing the schema before you can write code. Senior engineers pay that cost knowingly; new teams either pay it accidentally (denormalize first, regret later) or skip it (treat the database as a key-value store and accumulate years of integrity debt). This lesson is about why the first path is almost always cheaper across the lifetime of a system.

Order the steps

Order the steps to design a schema for 'user has many addresses':

  1. 1 Identify the entities: User, Address
  2. 2 For each entity, pick a primary key (id BIGSERIAL or uuid)
  3. 3 Identify the relationship: one user has many addresses (1:N)
  4. 4 Add a user_id column to addresses with REFERENCES users(id)
  5. 5 Add NOT NULL on the FK (every address must belong to a user)
  6. 6 Add an index on addresses(user_id) so 'list addresses for user X' is fast
  7. 7 Decide ON DELETE: CASCADE or RESTRICT
Quiz

What is a primary key in a relational table?

Quiz

What does a foreign key declaration buy you?

Complete the analogy

Fill in the blank: the database engine refuses bad inserts because of declared _______ — rules like NOT NULL, UNIQUE, FOREIGN KEY, CHECK.

Recall before you leave
  1. 01
    In two sentences, why is storing 'user has many tags' as a JSON array column usually worse than a tags table plus a user_tags join table?
  2. 02
    Name the five constraint kinds a relational engine enforces and state what each one does.
  3. 03
    What is the difference between a candidate key and a primary key?
Recap

The relational model defines data as sets of typed tuples sharing a fixed shape (a relation). Tables, rows, and columns are the SQL implementations of relations, tuples, and attributes. Every row is identified by a candidate key — one is the primary key. Foreign keys link tables and let the engine refuse orphan rows. The five constraint kinds (PK, FK, NOT NULL, UNIQUE, CHECK) are the declarative guarantees that move correctness from every application to the database boundary. The cost is schema-design discipline before code; the payoff is correctness every caller gets for free.

Connected lessons
appears again in164
Continue the climb ↑Constraints, keys, and Postgres data types
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.