…
…
Starting from a single-table old reading log, build the member, author, book and reading tables with their constraints, move the data over with queries, and test the design with a report.
A campus book club has written its readings into a single table for years: every row holds the member's name and email, the book's title, its author, the page count, the finish date and a rating. As the list grew, the same author name was typed dozens of times, nobody knows how many rows to fix when an email changes, and even a simple question like "how many books does this author have?" has no reliable answer. In this project you will read the club's requirements, design the database from scratch, move the old record into the new tables and test the design with a report.
Ready-made data: only the old_ledger table (fictional sample data, 43 rows): row_no, member_name, email, book, author, pages, finished_on (the finish date) and rating (1–5; empty on some rows). The tables you will build are member, author, book and reading, which links a member to a book.
Lessons used: creating tables, constraints and foreign keys (28–30), data modeling and normalization (31–32), emptying a table (33), INSERT and INSERT ... SELECT (34); joins, grouping and EXCEPT in the audit and report steps (11–22). No new concept is taught.
Project cells are permanent: every command you run here stays in the database; it is not rolled back as in lesson exercises. That is why each cell is written to be rebuilt from scratch: a cell that creates tables first drops the old ones with DROP TABLE IF EXISTS, and a cell that loads data first empties the tables with TRUNCATE. You can re-run a cell as many times as you like; if you re-run an earlier cell, run the later ones again in order. The first line of the two cells that read data starts with "Why this query:"; finish it with one sentence before you write the query (it is not checked). Do not change the -- Check query lines at the end of the cells.
What you'll do
Required topics
book_club.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The club's whole record sits in one table: old_ledger. Each row is one book finished by one member, and the member's name and email, the book's title, its author and its page count are rewritten on every row. Before you design anything, see how big that repetition is.
How many rows, how many different members (by email), how many different books and how many different authors does the ledger hold? Return the result in one row with the names entries, members, books and authors. The gap between the row count and the member and book counts shows how many times the same fact was written down.
This step's code cell is for members.Sign up free
Three separate things are tangled together in the ledger: the member, the book and the author. A reading is an event that links a member to a book. First build the two tables that have no relationship to other tables.
The club's requirements: every member has a number, a name and an email, and the database assigns the number itself. Name and email cannot be empty, and two members cannot share an email. The club also wants to know whether a member still attends: active cannot be empty and a new member starts as active. An author has a number and a name too; the name cannot be empty and the same author cannot be entered twice.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
You decide which constraint meets each requirement. Use the column names and types from the comment in the cell; the check query looks at columns, types, required flags, defaults and constraints.
This step's code cell is for members.Sign up free
In the ledger, the author's name is rewritten on every book row. An author has many books and each book has one author: this is a one-to-many relationship, and the link goes on the many side, the book.
Requirements: the database assigns the book's number; title, author and page count cannot be empty; the author must exist in the author table; the page count must be positive. Different authors can have books with the same title, but the same author cannot have two books with the same title. That last rule belongs to the combination of two columns, not to one; keep it in mind as you write the table. In the CHECK condition, write the column on the left and the bound on the right as a whole number (as in pages > 0); in the composite rule, list the columns in the order (title, author_id).
This step's code cell is for members.Sign up free
A member reads many books and a book is read by many members: this is a many-to-many relationship and cannot be built directly between two tables. You need a junction table where each row is one reading. The finish date and the rating belong neither to the member alone nor to the book alone; they belong to that member's reading of that book, so they live in the junction table.
Requirements: the same member cannot record the same book twice, so instead of a separate number, the combination of member and book identifies the row (member_id first, then book_id). The member and the book must exist in their own tables. The finish date cannot be empty. The rating must be between 1 and 5 but can be empty: some members in the ledger gave no rating. If a member leaves the club and the record is deleted, their readings should be deleted too; a book that has been read cannot be deleted (the default behavior).
Write the rating rule with CHECK, including both bounds.
This step's code cell is for members.Sign up free
The tables are empty. Instead of typing the data by hand, you will move it from the old ledger with a query: INSERT ... SELECT inserts every row the query returns. The same member and the same book appear many times in the ledger, so each must come in only once.
The member import is ready as an example. You import the authors and the books. The book table holds the author's number, not the name; you will find the number by matching the author's name in the ledger with the author table. That is why authors must be inserted before books.
This step's code cell is for members.Sign up free
Now the real content of the ledger: who finished which book, when, and with what rating. Every ledger row must become exactly one reading row. The reading table has numbers, not names: you will find the member's number from the email and the book's number from the title (and author).
How do you know the import is right? The check query joins the new tables back together and compares them with the ledger: the number of ledger rows missing from the new design (missing) must be 0. The rows of members who gave no rating must come across too; the rule that the rating can be empty was made for exactly that.
This step's code cell is for members.Sign up free
Ines Taylor has joined the club and has already finished two books: *The Observatory on the Roof* (November 20, 2026) and *The Cat Cartographer* (November 24, 2026). On the form Ines rated them out of 10: 10 and 8. The club's scale is 1 to 5, so the ratings must be halved when entered.
A volunteer wrote the entry, but the cell fails. Run it first and read the error message: which rule is broken? There are two separate mistakes in the code; fixing one reveals the other. Trust your constraints: the table catches both. The ON CONFLICT DO NOTHING lines keep the same member and the same readings from being inserted twice if the cell runs a second time.
This step's code cell is for members.Sign up free
The design is ready; now test whether it works with one question. The club is choosing next term's books and wants to look at authors: how many books does each author have in the club, how many times have their books been read in total, and what is the average rating?
Return the result with the names author, books, readings and avg_rating (two decimals); the most-read author first, ties by author name. Watch out: when you join the author table to books and books to readings, each book is repeated once per reading; count books accordingly. Readings without a rating must stay out of the average. Use the new tables, not the ledger.
This step's code cell is for members.Sign up free