Database Relationships and Diagrams · Lesson 24 of 112

Database From Requirements to a Model

What you will learn

  • A step-by-step method from words to a model
  • How to find entities, attributes and relationships in a description
  • How to spot a many-to-many hidden in the requirements
  • How to prove the design by loading data and asking questions

1. The method

Design is easier with steps:

  1. Read the requirements and underline the nouns (candidate entities) and facts (candidate attributes).
  2. List the entities and give each a primary key.
  3. Find the relationships and decide the maximum (one or many) and minimum (required or optional) of each side.
  4. Draw the ER diagram.
  5. Convert to tables: foreign key on the "many" side, junction table for many-to-many.
  6. Test with sample data and the questions the users will ask.

2. The requirements

A small library says:

"Members borrow books. The library may own several physical copies of the same book. We record the date each loan started and the date it was returned. We need to know who has what right now, and how often each book is borrowed."

Nouns: member, book, copy, loan. Facts: member name, book title, loan start date, return date.

Look closer at two sentences:

  • "Several physical copies of the same book": a book (the title) and a copy (the object on the shelf) are different things. One book has many copies, so that is one-to-many.
  • "Members borrow books": a member borrows many times, a copy is borrowed many times over the years. That is many-to-many between members and copies, so it needs a junction table. Here the junction table carries facts (dates), so it deserves its own entity name: loan.
members 1 ---------o< loans >o--------- 1 copies >o--------- 1 books

Read: a member has zero or many loans; a copy has zero or many loans; a book has one or many copies. (Read the diagram with the convention from lesson 3.7.)

3. Building the tables

The library is a made-up business, so its tables do not exist in the sample databases. We give them the prefix lib_ so they cannot clash with a real table. Build them in the sample database you chose in lesson 4.1 (use the Example data picker at the top); they are added to your browser's copy, and Reset data starts again:

CREATE TABLE lib_members (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE lib_books (id INTEGER PRIMARY KEY, title TEXT NOT NULL);
CREATE TABLE lib_copies (id INTEGER PRIMARY KEY, book_id INTEGER NOT NULL REFERENCES lib_books (id));
CREATE TABLE lib_loans (
    id          INTEGER PRIMARY KEY,
    member_id   INTEGER NOT NULL REFERENCES lib_members (id),
    copy_id     INTEGER NOT NULL REFERENCES lib_copies (id),
    borrowed_on TEXT NOT NULL,
    returned_on TEXT
);
INSERT INTO lib_members VALUES (1, 'Rina'), (2, 'Sam');
INSERT INTO lib_books VALUES (1, 'Rivers'), (2, 'Hills');
INSERT INTO lib_copies VALUES (1, 1), (2, 1), (3, 2);
INSERT INTO lib_loans VALUES (1, 1, 1, '2026-03-01', '2026-03-10'), (2, 1, 3, '2026-03-12', NULL), (3, 2, 1, '2026-03-15', NULL);
SELECT * FROM lib_loans ORDER BY id;
id  member_id  copy_id  borrowed_on  returned_on
--  ---------  -------  -----------  -----------
 1          1        1  2026-03-01   2026-03-10
 2          1        3  2026-03-12   NULL
 3          2        1  2026-03-15   NULL
(3 rows)
CREATE TABLE lib_members (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE lib_books (id INTEGER PRIMARY KEY, title TEXT NOT NULL);
CREATE TABLE lib_copies (id INTEGER PRIMARY KEY, book_id INTEGER NOT NULL REFERENCES lib_books (id));
CREATE TABLE lib_loans (
    id          INTEGER PRIMARY KEY,
    member_id   INTEGER NOT NULL REFERENCES lib_members (id),
    copy_id     INTEGER NOT NULL REFERENCES lib_copies (id),
    borrowed_on TEXT NOT NULL,
    returned_on TEXT
);
INSERT INTO lib_members VALUES (1, 'Rina'), (2, 'Sam');
INSERT INTO lib_books VALUES (1, 'Rivers'), (2, 'Hills');
INSERT INTO lib_copies VALUES (1, 1), (2, 1), (3, 2);
INSERT INTO lib_loans VALUES (1, 1, 1, '2026-03-01', '2026-03-10'), (2, 1, 3, '2026-03-12', NULL), (3, 2, 1, '2026-03-15', NULL);
SELECT * FROM lib_loans ORDER BY id;
id  member_id  copy_id  borrowed_on  returned_on
--  ---------  -------  -----------  -----------
 1          1        1  2026-03-01   2026-03-10
 2          1        3  2026-03-12   NULL
 3          2        1  2026-03-15   NULL
(3 rows)
CREATE TABLE lib_members (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE lib_books (id INTEGER PRIMARY KEY, title TEXT NOT NULL);
CREATE TABLE lib_copies (id INTEGER PRIMARY KEY, book_id INTEGER NOT NULL REFERENCES lib_books (id));
CREATE TABLE lib_loans (
    id          INTEGER PRIMARY KEY,
    member_id   INTEGER NOT NULL REFERENCES lib_members (id),
    copy_id     INTEGER NOT NULL REFERENCES lib_copies (id),
    borrowed_on TEXT NOT NULL,
    returned_on TEXT
);
INSERT INTO lib_members VALUES (1, 'Rina'), (2, 'Sam');
INSERT INTO lib_books VALUES (1, 'Rivers'), (2, 'Hills');
INSERT INTO lib_copies VALUES (1, 1), (2, 1), (3, 2);
INSERT INTO lib_loans VALUES (1, 1, 1, '2026-03-01', '2026-03-10'), (2, 1, 3, '2026-03-12', NULL), (3, 2, 1, '2026-03-15', NULL);
SELECT * FROM lib_loans ORDER BY id;
id  member_id  copy_id  borrowed_on  returned_on
--  ---------  -------  -----------  -----------
 1          1        1  2026-03-01   2026-03-10
 2          1        3  2026-03-12   NULL
 3          2        1  2026-03-15   NULL
(3 rows)

Two design choices to notice:

  • returned_on has no NOT NULL: a loan that is still out has no return date yet. NULL here means "not returned".
  • lib_loans.id is its own primary key rather than (member_id, copy_id), because the same member may borrow the same copy again on another day.

4. Proving the design

Ask the library's two questions:

SELECT m.name, b.title
FROM lib_loans l
JOIN lib_members m ON m.id = l.member_id
JOIN lib_copies c ON c.id = l.copy_id
JOIN lib_books b ON b.id = c.book_id
WHERE l.returned_on IS NULL
ORDER BY l.id;
name  title
----  ------
Rina  Hills
Sam   Rivers
(2 rows)
SELECT b.title, COUNT(*) AS times_borrowed
FROM lib_loans l
JOIN lib_copies c ON c.id = l.copy_id
JOIN lib_books b ON b.id = c.book_id
GROUP BY b.id
ORDER BY b.id;
title   times_borrowed
------  --------------
Rivers               2
Hills                1
(2 rows)
SELECT m.name, b.title
FROM lib_loans l
JOIN lib_members m ON m.id = l.member_id
JOIN lib_copies c ON c.id = l.copy_id
JOIN lib_books b ON b.id = c.book_id
WHERE l.returned_on IS NULL
ORDER BY l.id;
name  title
----  ------
Rina  Hills
Sam   Rivers
(2 rows)
SELECT b.title, COUNT(*) AS times_borrowed
FROM lib_loans l
JOIN lib_copies c ON c.id = l.copy_id
JOIN lib_books b ON b.id = c.book_id
GROUP BY b.id
ORDER BY b.id;
title   times_borrowed
------  --------------
Rivers               2
Hills                1
(2 rows)
SELECT m.name, b.title
FROM lib_loans l
JOIN lib_members m ON m.id = l.member_id
JOIN lib_copies c ON c.id = l.copy_id
JOIN lib_books b ON b.id = c.book_id
WHERE l.returned_on IS NULL
ORDER BY l.id;
name  title
----  ------
Rina  Hills
Sam   Rivers
(2 rows)
SELECT b.title, COUNT(*) AS times_borrowed
FROM lib_loans l
JOIN lib_copies c ON c.id = l.copy_id
JOIN lib_books b ON b.id = c.book_id
GROUP BY b.id
ORDER BY b.id;
title   times_borrowed
------  --------------
Rivers               2
Hills                1
(2 rows)

Rina has Hills and Sam has Rivers (the second loan of copy 1 by Sam; the first by Rina ended on 10 March). Rivers has been borrowed twice, Hills once. Both questions are answered with plain queries, so the model works for these requirements.

Worked example: what did the requirements miss?

Test the model with one more question: "Which member has borrowed the most?" It is answerable (GROUP BY member on loans). Now try "How late was each return?" The model has no due date, so it cannot say. The fix is a new column due_on in loans.

Lesson learned: ask the users' questions before building, and check each is answerable from the tables. Names and dates are made up.

Modelling mistakes to watch for

Most beginner models go wrong in the same few ways:

Mistake Why it hurts Better
Several values in one cell ("red, blue") cannot search, count or check them a child table with one row per value
Repeated columns (phone1, phone2, phone3) a fourth phone needs a new column and rewrites every query a child table phones
A table that mixes two things (customer and order in one) repeated facts that drift apart (chapter 5) one table per entity
No key, or a key that can change (name, phone) rows cannot be told apart, links break a stable primary key (lesson 2.5)
A many-to-many drawn as a single foreign key one side can only link to one row a junction table (lesson 3.4)
Status or type stored as free text 'Paid', 'paid ' and 'PAID' all appear a short list of allowed values (CHECK or a lookup table, lesson 2.7)
Rules living only in the app another program can break them constraints in the database

Common mistakes

  • Merging "book" and "copy" into one table, so you cannot tell which physical item is out.
  • Skipping the questions test. A model that cannot answer the real questions is wrong.
  • Making NULL mean several things. Here it means "not yet returned" only.
  • Designing for every future idea. Model what the requirements say; extend later.

Key takeaways

  • Method: underline nouns, list entities, find relationships, draw, convert, test.
  • Distinguish a general thing (book) from its individual items (copy).
  • A many-to-many with its own facts becomes an entity (loan).
  • Test the design with sample data and the users' real questions.

Practice

1. In the library example, why are books and copies separate tables?

Show answer

A book (the title) can have several physical copies, and a loan is about one specific copy.

2. What does returned_on IS NULL mean in this design?

Show answer

The loan is still open: the copy has not been returned.

3. Why is loans.id the primary key instead of (member_id, copy_id)?

Show answer

The same member can borrow the same copy more than once, on different dates.

4. Which relationship was hidden between members and copies?

Show answer

Many-to-many, solved by the loans table.

5. Add a due date requirement. What changes?

Show answer

Add a due_on TEXT column to loans.

6. Apply the method: list the entities for "a gym has members and trainers; a trainer runs many sessions; members book places in sessions".

Show answer

Entities: member, trainer, session, and a booking (junction between member and session, because it is many-to-many). A trainer to sessions is one-to-many.