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.
| id | name | city | country |
|---|---|---|---|
| int4 | text | text | char |
| 3 | Sam Lee | Toronto | CA |
| 9 | Sam Lee | Seoul | KR |
| 10 | Null | Berlin | DE |
| 42 | Ada Lovelace | London | GB |
relation = tablethe whole thing: a heading plus a body. this one is called customers. Codd's word is relation, SQL says table.
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
- └──
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.
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".
| idint4 | nametext | citytext | countrychar |
|---|---|---|---|
| 1 | Maya Chen | San Francisco | US |
| 2 | Grace Okafor | Lagos | NG |
| 3 | Sam Lee | Toronto | CA |
| 4 | Zoë Martin | Paris | FR |
| 5 | Zoe Martin | Lyon | FR |
| 6 | José Silva | null | BR |
| 7 | Ming Li | Singapore | SG |
| 8 | Sean O'Neil | Dublin | IE |
| 9 | Sam Lee | Seoul | KR |
| 10 | Null | Berlin | DE |
| 11 | Ana Lima | '' | PT |
| 12 | Ravi Kumar | Bengaluru | IN |
- SELECT id, name, city, country
- FROM customers
- 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.
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:
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;| countrychar | customersint8 |
|---|---|
| FR | 2 |
| BR | 1 |
| CA | 1 |
| DE | 1 |
| IE | 1 |
| IN | 1 |
| KR | 1 |
| NG | 1 |
| PT | 1 |
| SG | 1 |
| US | 1 |
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:
SELECT n, word
FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (n, word)
ORDER BY n;| nint4 | wordtext |
|---|---|
| 1 | one |
| 2 | two |
| 3 | three |
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.
SELECT name
FROM customers
WHERE id IN (3, 9)
ORDER BY id;SELECT name FROM customers WHERE id IN (3, 9) ORDER BY id;
SELECT DISTINCT name FROM customers WHERE id IN (3, 9);| nametext |
|---|
| Sam Lee |
| Sam Lee |
| nametext |
|---|
| Sam Lee |
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:
EXPLAIN SELECT name FROM customers WHERE city = 'Lyon';postgres reads all 2,000 customers to find the one in Lyon (Seq Scan). now add an index, and ask the exact same question:
CREATE INDEX ON customers (city);
EXPLAIN SELECT name FROM customers WHERE city = 'Lyon';
SELECT name FROM customers WHERE city = 'Lyon';| nametext |
|---|
| Zoe Martin |
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.