Lab 01 — Model and implement the catalogue
Objectives
After this lab, learners can:
- Turn a written domain description into a normalized schema.
- Seed it repeatably and check the result with a query.
Before You Start
- A scratch database you can drop and recreate, as set up in the study guide.
- The schema from lesson 1's worked example.
Steps
Read the catalogue description and list its entities.
three entities, plus one table for the many-to-many.
Write
CREATE TABLEfor each, with a primary key on every one, and run the script.CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLEHint
Create
authorandbookbeforebook_author: a foreign key needs its target to exist.Write a seed script inserting two authors, three books and one loan. Run it twice from an empty database.
the same row counts both times.
Insert a loan for a book id that does not exist.
ERROR: insert or update on table "loan" violates foreign key constraintCount the loans per author with a left join.
every author, including those with zero loans.
Acceptance
- The schema script runs from an empty database with no errors.
- Every table has a primary key, and every relationship a foreign key.
- The seed script gives the same row counts on a second clean run.
- A loan for a missing book is rejected by the database.
Stretch
Add a member table and make every loan reference one. Which existing rows break,
and how do you migrate them?