skip to content

the relational model

everything is a table, even the answer. codd's idea, and why it beat everything before it.

address
everything.start.relational_model
part
part 1, start here
chapter
chapter 2 of 7
reading time
9 min

in the relational model, all data lives in relations (tables): a heading of named, typed columns and a body of rows. every operation takes tables and gives back a table, so even the answer is a table, and you can ask the next question of it.

that's E. F. Codd's idea from 1970. no pointers between records, no paths to follow: just values, and questions about them.

the picture

you know how a query's result looks exactly like a table? same columns-and-rows shape, and you can even SELECT from it? that's not a coincidence. it's the whole design.

think of lego. every brick has the same studs on top, so anything you build can be the base for the next thing. a table is the brick. a query takes bricks and hands you back a brick, never a bag of loose pieces, so the answer always clicks into the next question.

first, the parts of one brick. Codd gave them formal names; SQL gave them friendlier ones. this book uses the SQL words, but you'll meet both.

look at =
customers
idnamecitycountry
int4texttextchar
3Sam LeeTorontoCA
9Sam LeeSeoulKR
10NullBerlinDE
42Ada LovelaceLondonGB

relation = tablethe whole thing: a heading plus a body. this one is called customers. Codd's word is relation, SQL says table.

real output, postgres 18.6 (PGlite 0.5.12) · shop customers 3, 9, 10 and 42
pick a word and the part of the table it names lights up here. four real customers: two different people both called Sam Lee, someone literally named Null, and Ada.

notice the key. two rows say Sam Lee, and they're different people (Toronto and Seoul). a row has no address, so the only way to point at one is by a value that's different for every row. that's what a primary key is for, and keys is how to pick one.

before codd: follow the pointers

in the 1960s a database was a web of records wired together with pointers. a customer record pointed at its first order, each order at the next one and at its items. to answer a question, your program walked the wires. here's Ada's corner of the shop, stored that way (the orders and items are hers, from the real data):

  • ├──
  • ├──
  • ├──
  • │ ├── the art of sql
  • │ └── chef's knife
  • ├──
  • │ ├── the art of sql
  • │ ├── steel speaker
  • │ └── chef's knife
  • ├──
  • ├──
  • │ └── mini bath mat
  • └──
a hierarchical database stores records under their parents. "what did Ada buy?" is a short walk down. "who bought the art of sql?" means walking every customer, every order, every item.

that worked well for the questions the designers planned for, and badly for every other one. change the shape (say, file orders under products instead) and every program that walked the old shape breaks. Charles Bachman, who built one of the first of these systems, called his 1973 Turing lecture "The Programmer as Navigator". that was the job.

Codd's paper, "A Relational Model of Data for Large Shared Data Banks" (1970), threw the wires away. store facts as rows of values. connect them by matching values (an order holds customer_id = 42), not by pointers. and let people ask for what they want, while the system works out how to find it.

1970E. F. Codd, at IBM, publishes the relational model: data as relations, connected by values, queried by describing the result.

drawn by hand · the postgres docs' history chapter, and the systems' own histories
click a year, or focus the timeline and use the arrow keys. from pointer-walking to postgres 18, the one running on this page.

everything is a table, even the answer

the relational operators are few. selection keeps the rows that pass a test (WHERE). projection keeps some columns (the SELECT list). a join pairs rows from two tables by matching values. and every one of them takes tables and returns a table. mathematicians call that closure. you can call it "the answer is a table too".

then =
the current answer
idint4nametextcitytextcountrychar
1Maya ChenSan FranciscoUS
2Grace OkaforLagosNG
3Sam LeeTorontoCA
4Zoë MartinParisFR
5Zoe MartinLyonFR
6José SilvanullBR
7Ming LiSingaporeSG
8Sean O'NeilDublinIE
9Sam LeeSeoulKR
10NullBerlinDE
11Ana Lima''PT
12Ravi KumarBengaluruIN
degree 4 · 12 rows
the same chain, as sql
  1. SELECT id, name, city, country
  2. FROM customers
  3. WHERE id <= 12

the starting table: shop's first 12 customers. add an operation, then another. each one eats a table and gives you a table back.

rows: real shop data. operators: model, the chapter runs the sql for real
start from 12 real customers and stack operations. each one eats the table above it and hands back a new one, and the sql on the right nests one query inside the next to say the same thing. try SELECT country, then DISTINCT, then count(*).

that nesting is real SQL. a query's result can sit anywhere a table can, including inside the FROM of another query:

sqldb=shop
SELECT country, count(*) AS customers
FROM (
  SELECT id, name, city, country
  FROM customers
  WHERE id <= 12
) AS first_twelve
GROUP BY country
ORDER BY customers DESC, country;
output, from postgres 18.6
SELECT 11
countrycharcustomersint8
FR2
BR1
CA1
DE1
IE1
IN1
KR1
NG1
PT1
SG1
US1
(11 rows)

even things that don't look like tables are. a list of values is a table, a function can return one, and a single number is a one-row, one-column table. here's a list of values, queried like any table:

sqlempty db
SELECT n, word
FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (n, word)
ORDER BY n;
output, from postgres 18.6
SELECT 3
nint4wordtext
1one
2two
3three
(3 rows)

sets, bags and order

Codd's relations are sets: no row appears twice, and the rows have no order. SQL kept the second rule and quietly dropped the first.

predict the output
sql
SELECT name
FROM customers
WHERE id IN (3, 9)
ORDER BY id;
sqldb=shop
SELECT name FROM customers WHERE id IN (3, 9) ORDER BY id;
SELECT DISTINCT name FROM customers WHERE id IN (3, 9);
output, from postgres 18.6
#1 · SELECT 2
nametext
Sam Lee
Sam Lee
(2 rows)
#2 · SELECT 1
nametext
Sam Lee
(1 row)

and order: a table has none. rows come back in whatever order postgres finds convenient, which can change when the data does. the only way to get an order is to ask for one with ORDER BY, which is why every query on this site that returns more than one row ends with one. sorting with ORDER BY has the details. NULL is the other place SQL bends the model, and it gets a whole chapter.

why it won

because the "how" lives in one place, and it isn't your program. you can add an index, move the table to a faster disk, or let postgres pick a different plan, and the query doesn't change. Codd called that data independence. watch it happen:

sqldb=shop · session lyon
EXPLAIN SELECT name FROM customers WHERE city = 'Lyon';
output, from postgres 18.6
EXPLAIN
Seq Scan on customers (cost=0.00..49.00 rows=1 width=12) Filter: (city = 'Lyon'::text)

postgres reads all 2,000 customers to find the one in Lyon (Seq Scan). now add an index, and ask the exact same question:

sqldb=shop · session lyon
continues session lyon (1 earlier block run first)
CREATE INDEX ON customers (city);
EXPLAIN SELECT name FROM customers WHERE city = 'Lyon';
SELECT name FROM customers WHERE city = 'Lyon';
output, from postgres 18.6
#1 · CREATE INDEX
#2 · EXPLAIN
Index Scan using customers_city_idx on customers (cost=0.28..8.29 rows=1 width=12) Index Cond: (city = 'Lyon'::text)
#3 · SELECT 1
nametext
Zoe Martin
(1 row)

new plan, same query, same answer. don't worry about reading the plans yet (reading EXPLAIN does that). just notice that the first line changed and your SQL didn't. in the pointer days, that change would have meant rewriting every program that walked the old path.

the other reason is the math. because every operator is closed over tables, queries can be rewritten into equivalent ones that run faster, and the rewrite is provably correct. that's relational algebra, and it's what lets a planner exist at all.

keylines

  • a relation is a heading of named, typed columns and a body of rows. SQL says table.
  • rows are found by their values, never by position or pointer. that's why tables need keys.
  • every operation takes tables and returns a table, so answers can be questioned again.
  • SQL keeps duplicates and has no row order unless you ask for one.
  • the query says what. the database decides how, and can change its mind without breaking you.