Module 4 · Relating tables

Connect tables with keys and relationships

By the end you will be able to explain how one table points to rows from another.

About 12 min

0 of 52 lessons

Before you start, you need

Understand tables, rows and columns

Recognise how two tables are connected

The library stores authors in one table and books in another, so it does not repeat the author's details in every book. To know who wrote it, each book stores its author's id.

authors.id identifies an author. books.author_id stores that same value in the book. If a book has author_id = 2, we look for the author whose id is 2. The dot indicates which table each column belongs to.

The column that identifies each row is called primary key. The one that points to another table is called foreign key. The connection uses their values, not the order in which rows appear or people's names.

An author can have several books, and their id will repeat in them. We call that a one-to-many relationship. They can also have zero books. In the next lessons you will decide whether to show only matches or keep those missing ones.

Minimal syntax
-- books.author_id references authors.id

See it in action

How do we know who wrote each book?

Sample data: 1 rows
idname
2George Orwell
Sample data: 2 rows
titleauthor_id
Nineteen Eighty-Four2
Animal Farm2
SQL
SELECT id, name
FROM authors;
Example result: 5 rows
idname
1Jane Austen
2George Orwell
3Virginia Woolf
4Douglas Adams
5Ursula K. Le Guin
  1. authors.id identifies each author. Look up the value of books.author_id in that column to find their name.
  2. The author with id 1 can appear as a reference in several books. Their name only needs to be stored once, in authors.
  3. A row's visual position is not its identity. Sorting authors must not change which author a book references.
  4. Reading the two tables helps you understand the connection. In the next lesson you will write a query to join them.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Recognise identifiers and references

Not started

Write a query that shows the title and author_id of every book.

Expected result: 7 rows with the columns title, author_id, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Follow a reference

Not started

Show title and author_id of the books that point to the author with id 2.

Expected result: 2 rows with columns title, author_id, in that order. The order of the rows does not matter.

View tables and data

Open this panel to see the full data.

Key takeaways

  • A primary key identifies a row.
  • A foreign key references a related identity.
  • Several books can reference the same author.

Next: Match rows with INNER JOIN

Go deeper

This page explains the model. Declaring PRIMARY KEY and FOREIGN KEY is practised in the design module. The referenced key must be unique; a reference does not need to be unique if the relationship allows several items.