Designing Your First Relational Database, Gently

Designing a database from a blank page feels hard because there seem to be so many decisions. In practice, a small project needs only a few. This post walks through the process I suggest to beginners, using a tiny book club app as the example.

Designing related tables illustration

Step 1: write down the things, not the screens

Forget pages and buttons for a moment. List the *things* your app keeps track of. For a book club:

Each thing you list is a candidate table. Nouns are good candidates; verbs ("discussed") often become link tables later.

Step 2: list facts about each thing

For each table, write the facts you need:

TableFacts
membersname, email, date joined
bookstitle, author
meetingsdate, place

Keep it short. You can always add a column later; removing one that code depends on is harder.

Step 3: give every table a primary key

A primary key is a column whose value identifies one row and never changes. For a beginner project, a simple counting integer is perfect:

CREATE TABLE members (
  id          INTEGER PRIMARY KEY,
  name        TEXT NOT NULL,
  email       TEXT NOT NULL UNIQUE,
  joined_on   TEXT NOT NULL
);

UNIQUE on email stops two members sharing one address.

Step 4: link the tables

Ask how things relate. A meeting discusses one or more books, and a book may be discussed at several meetings. That is a many-to-many relationship, so it needs a small table of its own:

CREATE TABLE meeting_books (
  meeting_id  INTEGER NOT NULL REFERENCES meetings(id),
  book_id     INTEGER NOT NULL REFERENCES books(id),
  PRIMARY KEY (meeting_id, book_id)
);

Each row says "this book was discussed at this meeting". The REFERENCES parts are foreign keys: they point to rows in other tables.

Step 5: test with real questions

Write three questions your app must answer, and check your design can answer them with a query:

  1. Which books did we discuss in March?
  2. Which members joined this year?
  3. Has this book been discussed before?

If a question is awkward to answer, the design may be missing a table or a column. Better to find out now than after you have written the app.

Common first-design mistakes

What I would not worry about yet

Indexes beyond keys, performance tuning and fancy data types can wait. A small, clear design that answers your questions is a great first database. When your project grows, you will know which queries matter and can improve from there.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

Get new pages by email

Related pages on this site