Sub topic 1
What the Database Needs to Answer
A database design should begin with questions, not tables. This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE statements. You also need a running PostgreSQL 12 or newer database and a basic understanding of tables, rows, and columns.
Our example tracks a personal reading history. The database should answer questions such as:
- What books are in the collection?
- Who wrote each book?
- Which books have several authors?
- Which books are waiting to be read, in progress, finished, or abandoned?
- When did a reading start or finish?
- What rating did a finished book receive?
- How many books were finished in each year?
- Which books match words in their titles?
These questions suggest three main kinds of things, called entities. A book has information such as a title, ISBN, publication year, and page count. An author has a name. A reading entry records your interaction with a book, including its status, dates, and rating.
Prerequisites
You should be comfortable with basic SQL and understand the difference between a table, a row, and a column. You should also have access to PostgreSQL and a SQL client such as psql.
Separating the domain into tables
Keep facts about different entities in separate tables. A book title belongs to a book, while a person's name belongs to an author. Storing the author name in every book row would duplicate data and make corrections harder.
A reading entry is separate from a book because the same book can be read more than once. It also changes over time. A book's title normally stays the same while its reading status can move from want_to_read to reading and then to finished.
| Entity | Important facts | Relationships |
|---|---|---|
| Book | Title, ISBN, publication year, page count | Has one or more authors; can have reading entries |
| Author | Name | Can write many books |
| Reading entry | Status, start date, finish date, rating | Belongs to one book |
A book and an author have a many-to-many relationship. One book can have several authors, and one author can write several books. Relational databases represent this relationship with a separate join table. A reading entry has a simpler relationship: each entry belongs to one book, while a book can have zero or more entries.
Turning questions into design decisions
Each important question should map to data the database can store or calculate. “Who wrote this book?” requires a connection between books and authors. “How many books were finished in 2025?” requires a finish date and a status. “Can this rating be trusted?” requires a rule that limits ratings to valid values and associates them with finished readings.
This approach prevents a common design error: adding columns only because they seem convenient. Start with the questions, identify the facts needed to answer them, and then choose relationships that avoid duplication.
Exercise
Why should authors be stored separately from books?
Show answer
One author can write many books, and one book can have many authors. Separate author rows plus a join table represent both relationships without repeating author details.