Designing tables and relationships

Turn an app's data into well-designed SQL tables with keys, constraints and relationships.

Module 5: Databases · Lesson 15 of 26 · 16 min read · Free

Your data has to survive restarts, be shared by thousands of requests at once, and stay correct. That's a database's job. This course uses SQL (relational) databases — the most common kind in business. (If you've done the SQL for Data Analysis course, much of this is familiar; here we design tables rather than just query them.)

From requirements to tables

Bukka Express needs menu items, orders, the items in each order, users and login sessions:

The Bukka Express database

Rules of thumb:

  • One table per kind of thing; one row per thing.
  • Every table has a primary key (id INTEGER PRIMARY KEY — the database assigns it).
  • A one-to-many relationship uses a foreign key: each order_items row has an order_id pointing to orders.
  • Many-to-many (orders ↔ menu items) goes through a linking table: order_items.
  • Store facts that can change at the moment they're true. order_items.price records what the customer paid; if the menu price goes up next week, old receipts must not change.

Creating tables

CREATE TABLE menu_items (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  category TEXT NOT NULL,
  price INTEGER NOT NULL CHECK (price > 0)
);

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  email TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL,
  password_hash TEXT NOT NULL,
  role TEXT NOT NULL DEFAULT 'customer'
);

CREATE TABLE order_items (
  id INTEGER PRIMARY KEY,
  order_id INTEGER NOT NULL REFERENCES orders(id),
  menu_id INTEGER NOT NULL REFERENCES menu_items(id),
  qty INTEGER NOT NULL,
  price INTEGER NOT NULL
);

Constraints are your last line of defence: NOT NULL, UNIQUE (no duplicate emails), CHECK (no negative prices), REFERENCES (no items for orders that don't exist). Even if your code has a bug, the database refuses bad data.

Money

Store money as whole numbers of the smallest unit: naira (or kobo, as Paystack does). Never use floating-point numbers for money — 0.1 + 0.2 is 0.30000000000000004 in JavaScript.

Check yourself

Answer in your head first, then open each question to see the answer and why.

  1. How should an order's many products be stored?

    • A In a linking table like order_items, with order_id and menu_id
    • B As a comma-separated text column in orders
    • C In a separate database
    • D In the users table
    Show the answer

    A) In a linking table like order_items, with order_id and menu_id — A linking table models many-to-many relationships.

  2. Why does order_items store the price as well as menu_id?

    • A Menu prices change; the order must keep what the customer actually paid
    • B To save space
    • C Because SQL requires it
    • D It doesn't need to
    Show the answer

    A) Menu prices change; the order must keep what the customer actually paid — Record facts as they were at the time.

  3. Which constraint stops two users registering with the same email?

    • A UNIQUE
    • B CHECK
    • C DEFAULT
    • D NOT NULL
    Show the answer

    A) UNIQUE — UNIQUE enforces no duplicates.

More free lessons: What the back end does · Install Node.js and use the terminal · Asynchronous code: promises and async/await · HTTP: requests, responses and status codes · Designing a REST API · Storing passwords safely · Automated tests and project structure