Keys and Relationships

A primary key identifies a row. It is unique and never null, which is what makes a row addressable at all.

Prefer a key with no meaning, such as an auto-assigned id. Anything meaningful, an email, a title, a national number, eventually changes or turns out not to be unique.

A foreign key points at a row in another table. books.author_id holds the id of a row in authors.

That single idea is what makes the model relational. The author's name is stored once, in one row, and every book refers to it. Correct a spelling there and every book is correct, because there is only one copy of the fact.

Try it

Look at the authors table on its own. Five rows, five names, each stored exactly once.

loading editor…
real SQLite, in your browser

The join is module 5. For now, notice that the name appears once in the authors table and is reached by every book that points at it.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back