Theory · Page 4 of 34 · Modelling

4. The relational model and keys

The relational model stores every fact as a row in a table, finds each row by its values alone, and links tables through keys. It has run most of the world's business data since the 1980s because it is simple, precise and checkable.

A relation, its keys and a refused orphan
Playback speed

The clip takes Harbour Mutual's claims table apart. First its heading, the attributes with their kinds of value, and three of its rows from the seed. Then its keys: a set of attributes that is unique but not minimal, the two candidate keys, and the primary key chosen from them. Then the foreign key from claims to policies, and a claim pointing at a policy that does not exist, refused by the database.

The intuition: every fact in one row, found by its values

A claims handler at Harbour Mutual needs one claim. She does not ask for "the fourth row" or "the record at disk address 81"; she asks for claim number such-and-such. The database finds it by its values alone. That simple idea, that data is found by what it says and not by where it is stored, is the heart of the relational model.

Every claim is a row with the same columns: its number, its policy, its loss date, its cause. Every policy is a row in another table. A claim does not contain its policy; it holds the policy's identifier, and the database follows it when asked. Change the policy once and every claim sees the change.

Two rules keep this honest. Each row must be identifiable: there must be a column, or a set of columns, whose values no two rows share. And every reference must lead somewhere: a claim cannot name a policy that does not exist. These are keys and referential integrity, and most of this page is about them.

Remember In the relational model data is found by its values, every row is identified by a key, and every reference must point at a row that exists.

The theory: relations, keys and integrity

Edgar F. Codd, a researcher at IBM, published the relational model in 1970. Before it, programs navigated stored records by following pointers, so a change in storage broke the programs. Codd proposed describing data only by its values, organised as mathematical relations, with a language that says what data is wanted and leaves how to find it to the system.

A relation has a heading, a set of attributes each with a domain (the set of values it may take, such as dates or loss-cause codes), and a body, a set of tuples, each giving one value from its domain to every attribute. In SQL terms: a table, its columns with their types, and its rows. Because the body is a set, the rows have no order and no two are the same; because the heading is a set, the columns have no order either.

Relations are combined by the operations of relational algebra: restrict (keep the rows that meet a condition), project (keep some columns), join (combine rows of two relations that match), union, difference and rename. Each operation takes relations and returns a relation, so operations can be chained without limit. SQL is built on this algebra, with some departures: SQL tables may hold duplicate rows, columns have an order, and values may be NULL.

Keys, from widest to narrowest A superkey is a set of attributes whose values are unique for every row: {claim_id, cause} is one. A candidate key is a superkey with nothing to spare: remove any attribute and it is no longer unique. {claim_id} is a candidate key, and so is {claim_number}. The primary key is the candidate key chosen to identify rows; the others are alternate keys, still declared unique. A foreign key is a set of attributes in one relation whose values must match a candidate key of another: claims.policy_id must match a policies.policy_id.

A natural key is one the business already uses, such as a policy number. A surrogate key is a meaningless number made by the system, such as policy_id. Surrogate keys never change and are short; natural keys mean something to people and to other systems. Good designs use a surrogate primary key and still declare the natural key unique, so the business rule is enforced too.

Two integrity rules come with keys. Entity integrity: no part of a primary key may be missing (NULL), or the row cannot be identified. Referential integrity: every foreign key value must match an existing row in the table it refers to. A database enforces referential integrity by refusing an orphan, and by applying a rule when the referenced row is deleted: refuse the delete, delete the dependent rows too (cascade), or set the reference to NULL.

Here are the keys PostgreSQL holds for Harbour Mutual's policies and claims, read from the database's own catalogue on the seeded data:

The primary and foreign keys of policies and claims, from PostgreSQL's catalogue (run on PostgreSQL 18.3 in CI)
SELECT tc.table_name, tc.constraint_type, kcu.column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
  ON kcu.constraint_schema = tc.constraint_schema
 AND kcu.constraint_name = tc.constraint_name
 AND kcu.table_name = tc.table_name
WHERE tc.table_schema = 'public'
  AND tc.table_name IN ('policies', 'claims')
  AND tc.constraint_type IN ('PRIMARY KEY', 'FOREIGN KEY')
ORDER BY tc.table_name, tc.constraint_type DESC, kcu.column_name

A key is a rule about every possible row, not a fact about today's rows. The data can show that a rule is broken, but never prove that it holds. On the seed, 60 claims carry 60 distinct claim numbers, so claim_number could be a key; the business confirms that it always will be, and the database is told to enforce it.

Is claim_number unique in today's data? (PostgreSQL)
SELECT COUNT(*) AS claims, COUNT(DISTINCT claim_number) AS claim_numbers
FROM claims

Why it matters

Keys are how two systems agree they are talking about the same thing. In the merger, Harbour Mutual's customer_id and Kestrel's klant_nr are both surrogate keys, and they mean nothing to each other; matching customers (chapter 18) is the hard work of finding a key the two can share.

Referential integrity is a promise checked by the machine. Kestrel's claims table does not declare its foreign key to policies, and the merger finds claims whose policy does not exist. A declared foreign key would have refused them on the day they were written.

Where it goes wrong

  • No declared keys. "The application checks it" lasts until the second application, a bulk load or a manual fix.
  • A natural key as the only key. Policy numbers get reformatted, merged and reused; a surrogate key does not.
  • Only a surrogate key. Without the natural key declared unique, the same policy can be entered twice with two ids.
  • Keys inferred from data. A column that happens to be unique today is not a key until the business says it always will be.
  • Cascading deletes by default. Deleting one customer should not quietly delete twenty years of claims.

Check yourself

Choose an answer to each question, then press Check.

1. What makes a superkey a candidate key?
2. A claim names a policy_id that no policy has. Which rule does it break?
3. Why can today's data not prove that a column is a key?
Answer every question first.

Sources

The works this page draws on, summarised in its own words rather than quoted. Read them for the full argument.

  • E. F. Codd, "A Relational Model of Data for Large Shared Data Banks", Communications of the ACM 13(6), 1970Relations, domains, data independence and keys, and the idea of finding data by its values alone.
  • C. J. Date, An Introduction to Database Systems, 8th edition (Addison-Wesley, 2003)Superkeys, candidate, primary, alternate and foreign keys, entity and referential integrity, and where SQL departs from the relational model.
  • ISO/IEC 9075, Database languages: SQL (the SQL standard)Primary key, unique and foreign key constraints, and the actions on delete.

Practise it

The chapters that put this theory to work on Harbour Mutual and Kestrel:

The tools to try it in:

  • Model studio · Draw conceptual, logical and physical models, generate DDL for PostgreSQL, Spark SQL and T-SQL, and run it on Harbour Mutual's data in your browser.

Terms taught here: Primary keyForeign keyCandidate keyReferential integrity

An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Cloud Data Architecture Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.