Relationships and joins

Model one-to-many relationships

Place the foreign key on the many side of a relationship and query the related rows by that key.

8 minute lesson

~~~

One author can write many posts, while each post belongs to one author. This is a one-to-many relationship.

The foreign key belongs on the many side:

CREATE TABLE posts (
  id INTEGER PRIMARY KEY,
  author_id INTEGER NOT NULL REFERENCES authors(id),
  title VARCHAR(300) NOT NULL
);

Each post stores one author_id. Several post rows can store the same value.

Find all posts by author 7:

SELECT id, title
FROM posts
WHERE author_id = 7
ORDER BY id;

Or join the author name into the result:

SELECT authors.name, posts.title
FROM authors
JOIN posts ON posts.author_id = authors.id
WHERE authors.id = 7;

Do not store a comma-separated list such as '4,8,15' on the author row. The database cannot enforce each listed identifier as a foreign key, and ordinary joins cannot treat the pieces as rows.

The relationship also contains a business decision. If a draft post may exist before an author is assigned, author_id can allow NULL. If every post must always have an author, use NOT NULL.

Your action is to insert one author and three posts that reference it. Query the posts by foreign key, then explain why the same author data should not be copied into every post row.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →