Sub topic 2
Books with several authors: the join table
This chapter assumes you have PostgreSQL 12 or newer, can write SELECT, INSERT, and JOIN, and understand primary and foreign keys. It uses the books and authors tables from earlier chapters, each with an identity book_id or author_id primary key.
Linking books and authors
A book can have several authors, and an author can write several books. This is a many-to-many relationship. You model it with a join table, which is a table whose only job is to pair keys from two other tables.
CREATE TABLE book_authors (
book_id bigint NOT NULL REFERENCES books (book_id) ON DELETE CASCADE,
author_id bigint NOT NULL REFERENCES authors (author_id) ON DELETE RESTRICT,
author_position smallint NOT NULL DEFAULT 1,
PRIMARY KEY (book_id, author_id)
);The composite primary key (book_id, author_id) stops the same author from being linked to the same book twice. ON DELETE CASCADE removes the link when a book is deleted. ON DELETE RESTRICT blocks deleting an author who still has books.
Reading status, dates, and ratings
An enum is a fixed list of allowed values. The reading_status enum below limits the status column to four choices. The CHECK constraints keep dates in order and allow a rating only on finished books.
CREATE TYPE reading_status AS ENUM ('want_to_read', 'reading', 'finished', 'abandoned');
CREATE TABLE readings (
reading_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
book_id bigint NOT NULL REFERENCES books (book_id),
status reading_status NOT NULL DEFAULT 'want_to_read',
started_on date,
finished_on date,
rating smallint CHECK (rating BETWEEN 1 AND 5),
CHECK (finished_on IS NULL OR started_on IS NULL OR finished_on >= started_on),
CHECK (rating IS NULL OR status = 'finished')
);Loading related data
A data-modifying CTE (a WITH clause that contains an INSERT) can insert a book and an author, then link them, in one statement. RETURNING passes the generated IDs to the next step.
WITH new_book AS (
INSERT INTO books (title, isbn13, published_year, page_count)
VALUES ('Example Title', '9780000000001', 2001, 320)
RETURNING book_id
), new_author AS (
INSERT INTO authors (name)
VALUES ('Example Author')
RETURNING author_id
)
INSERT INTO book_authors (book_id, author_id)
SELECT book_id, author_id FROM new_book, new_author;Joining books, authors, and readings
Joins combine rows across tables. string_agg concatenates the author names for each book into one cell, ordered by author_position.
SELECT b.title,
string_agg(a.name, ', ' ORDER BY ba.author_position) AS authors,
r.status,
r.rating
FROM books b
JOIN book_authors ba USING (book_id)
JOIN authors a USING (author_id)
LEFT JOIN readings r USING (book_id)
GROUP BY b.book_id, b.title, r.status, r.rating
ORDER BY b.title;The LEFT JOIN keeps books that have no reading entry yet, and their status and rating appear as NULL.
Exercise
Why does the readings table reject a rating on a book whose status is 'reading'?
Show answer
The CHECK (rating IS NULL OR status = 'finished') constraint allows a rating only when the status is 'finished', so a rating on an unfinished book is refused.