ContentsThe library

Designing a Table to Live With

Say Out Loud What One Row Is Claiming

A table is a sentence with holes in it and a row fills the holes. Write that sentence down and most arguments about table boundaries answer themselves.

Most schemas are the shape the data arrived in, lightly tidied. The repair is

one habit, and it costs a minute per table: before you write any columns, write

the sentence that each row of the table is claiming is true.

A row is a claim

A table is not a container of objects. It is a sentence with blanks in it, and

a row is an assertion that the sentence is true when the blanks are filled with

those particular values.

FIG 1Five tables and the sentence each one asserts
steptablethe sentence, with blanksone row perhow many rowswhat happened
1customerscustomer _ is named _ and signed up on _customer1200The subject is a customer and everything else in the sentence is a property of that customer. Nothing about orders can be stated here without changing the subject.
2orderscustomer _ placed order _ on _ for _order94000The subject has changed. This sentence mentions a customer but it is not about the customer, it is about the order, which is why the customer name does not belong in it.
3order_linesorder _ contains _ of product _ at priceorder and product310000Two blanks are needed to pin the claim down, which is the first sign that the key has two parts. The grain is finer than an order.
4pricesproduct _ cost _ from _ until _product and period8400A sentence with a period in it. The same product appears many times and none of the rows contradict each other, because each claims something about a different stretch of time.
5stock_countson _ we counted _ of product _ at warehoday, product and warehouse2100000Three blanks in the key. Writing the sentence is how you find that out before building the table rather than after the duplicates appear.
5 steps
Read the second column aloud and the fourth almost follows from it. The sentence tells you the grain, which is what one row is per, and the grain tells you roughly how many rows there will be, which is the first number anybody asks for. Notice that the sentence for orders mentions a customer without being about one: the difference between mentioning a thing and being about it is the whole content of the next section.

Two things to notice about the sentences in that figure. They use the words of

the business, not of the system. And none of them contains the word data, or

record, or entity, which are words that appear when somebody is describing a

table rather than the fact it holds.

Writing it down

The sentence has a shape worth copying. One subject, a verb, and the blanks

that make the claim specific.

Customer _ placed order _ on date _ for amount _.

The test of a good sentence is whether somebody who does not work on the system

would agree it is either true or false for a given set of values. If they would

have to ask what the column means, the sentence is not finished.

Two common failures are worth naming because they appear in nearly every

schema. The first is a sentence that needs a technical term: user _ has a row

in the sessions table with status _. That is a sentence about the system and it

will not survive the system being rewritten. The second is a sentence with no

verb: customer _ , name _ , region _ , last login _. A list of columns is not a

claim, and the absence of a verb is the sign that nobody has decided what the

table is for.

The test for one table

Here is where the sentence earns its minute. Read it aloud. If it contains an

and that joins claims about two different subjects, there are two tables.

FIG 2From one sentence to the number of tables
Three questions, applied until the chart stops sending you back to the top. The loop is deliberate: splitting one table usually produces a sentence that needs splitting again, and two or three passes is normal for anything of interest. This chart is a restatement of normal form, derived from what the sentences mean rather than recited as rules, and the next lesson shows what goes wrong when it is skipped.
FIG 3One table before the split and two after it
plaintext
BEFORE, one table

  orders(order_id, placed_on, amount,
         customer_id, customer_name, customer_region)

  sentence: customer _ named _ in region _ placed order _
            on _ for _
                   ^^^ two subjects joined by an and

  what goes wrong
    - a customer who moves region has to be updated on every
      order they ever placed, and a missed row is now a
      disagreement with no tiebreaker
    - a customer with no orders yet cannot be stored at all
    - deleting the last order of a customer deletes the
      customer

AFTER, two tables

  customers(customer_id, name, region)
  sentence: customer _ is named _ and lives in region _

  orders(order_id, customer_id, placed_on, amount)
  sentence: customer _ placed order _ on _ for _

  and the original question is still one join
    select o.order_id, c.name
    from orders o join customers c using (customer_id)
The sentence at the top of the first block contains two subjects joined by an and, which is the defect. Everything wrong with the merged version follows from that one fact and is listed under it. The two sentences below are each about one subject, and the duplication, the impossible insert and the destructive delete all disappear, not because a rule was obeyed but because the claims are now separable. The join at the bottom is how the original question still gets answered, which is the part people worry about and the part that costs the least.

What the sentence settles

Three decisions that teams usually argue about separately are all consequences

of the sentence, which is the practical reason to write it.

The grain is settled. One row per true instance of the claim, which is why the

third row of the first figure is one row per order and product rather than one

per order. Arguments about whether to store a total on the order or on the line

are arguments about which sentence is being asserted, and they end as soon as

somebody says both sentences out loud.

The key is settled. The smallest set of blanks that makes the claim unique is

the key, and if two different sets of blanks both do it, you have found the

alternate keys the third lesson is about.

FIG 4Does this column belong in the orders table
the sentence mentions itit is a fact about an orit belongs somewhere els
placed_on110
amount110
customer_name010
line_item_price001
shipped_on100
customer_lifetime_value010
Three columns fail and they fail in three different ways. The customer name is a fact about a customer, so it lives in the customers table. The line price is at a finer grain, one per product within the order, so it lives in the lines table. The lifetime value is not a fact at all, it is a computed summary that is true only at the instant it was calculated, which makes it the subject of the sixth lesson on duplicating things on purpose. Note that shipped_on passes even though the original sentence did not mention it: a fact about an order genuinely belongs here, and the right response is to extend the sentence rather than to reject the column.

And the empty cells are settled, which is the single most useful consequence. A

blank the sentence requires cannot be empty, because a row with it missing is

not making the claim at all. A column the sentence does not require is

optional, and now you know why it is optional, which is exactly the information

the fifth lesson needs when it asks what an empty cell means.

The next lesson takes the merged table from the figure above and shows what the

duplication does over time, which is where normal form comes from.

Recap

  • A table asserts one sentence with blanks in it, and every row is a claim that the sentence is true with those blanks filled in.
  • If the sentence needs an and joining two claims about different subjects, there are two tables and somebody has merged them.
  • The sentence fixes the grain, the key and which columns can be empty, so writing it first settles three decisions that are otherwise argued about separately.

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

NextThe Same Fact in Two Places →

The rest of this course

  1. 01Say Out Loud What One Row Is Claimingyou are here
  2. 02Seventy-Eight Copies and No Tiebreakeropening only
  3. 03The Email Address Was Reused by the New Employeeopening only
  4. 04There Is No Column That Can Hold a Listopening only
  5. 05The Query Returned Nothing and the Query Was Correctopening only
  6. 06Copy the Value Only If You Are Also Building the Thing That Repairs Itopening only
  7. 07The Report Was Right in March and Is Wrong Nowopening only
  8. 08For Twenty Minutes, Both Versions Have to Be Rightopening only

Read alongside