Before you start, you need
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.
-- books.author_id references authors.idSee it in action
How do we know who wrote each book?
| id | name |
|---|---|
| 2 | George Orwell |
| title | author_id |
|---|---|
| Nineteen Eighty-Four | 2 |
| Animal Farm | 2 |
SELECT id, name
FROM authors;| id | name |
|---|---|
| 1 | Jane Austen |
| 2 | George Orwell |
| 3 | Virginia Woolf |
| 4 | Douglas Adams |
| 5 | Ursula K. Le Guin |
- authors.id identifies each author. Look up the value of books.author_id in that column to find their name.
- The author with id 1 can appear as a reference in several books. Their name only needs to be stored once, in authors.
- A row's visual position is not its identity. Sorting authors must not change which author a book references.
- 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
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.
The title describes the book, and author_id shows its reference to the author.
You need the title and author_id columns.
The structure is SELECT title, author_id FROM books;
Solution explained
SELECT title, author_id FROM books;author_id stores the value that references authors.id. The query lets you see the relationship data without needing a JOIN yet.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Follow a reference
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.
The reference to the author is stored in each book, not in authors.
Filter the books by the value of their foreign key.
The condition is author_id = 2.
Solution explained
SELECT title, author_id FROM books WHERE author_id = 2;Two books store author_id = 2: Nineteen Eighty-Four and Animal Farm. Repeating the reference is correct in a one-to-many relationship; the author's name is stored only once in authors.
Key takeaways
- A primary key identifies a row.
- A foreign key references a related identity.
- Several books can reference the same author.
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.