Database Schema Design — SQL

A schema is the design of a database: which tables there are, what columns they have, and how the tables are linked. A good schema stores every fact once. If…

A friend changes their phone number. You change it once in your contacts, and that's it: every WhatsApp group they're in is right again. The groups don't keep their own copy of the number. A group only points at your friend's contact.

A well-designed database works the same way: every fact (one piece of information, like your friend's phone number) is stored once, in one table. Other tables only point at it, the way a group points at a contact. The design of a database is called its schema. A schema is like the floor plan of a house: it says which tables there are, what columns they have, and how the tables are linked.

The trouble with one table for everything

This is the products_flat table: every product stores its own category name.

id name price category
1 Laptop 1200 Electronics
2 Mouse 15 Electronics
3 Monitor 350 Electronics
4 Pen 1 Stationery

The category name "Electronics" is written three times. When the same fact is stored over and over in many rows, it is called data redundancy. Redundancy causes three problems, which you can try in the Lab:

What you want to do In one table
Rename Electronics to Gadget The UPDATE must change 3 rows. If one row is missed, the data contradicts itself.
Delete the Pen product The Stationery category disappears too, because it was only recorded on the Pen row.
Add a Books category with no products yet Impossible: NOT NULL constraint failed: products_flat.name. Every row has to be a product, and a product's name is required.

Two tables: categories and products

The fix: categories get their own table, and products only store the category's number, exactly like customers and orders in the Table Relationships lesson.

CREATE TABLE categories (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL
);

CREATE TABLE products (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  price REAL NOT NULL,
  category_id INTEGER,
  FOREIGN KEY (category_id) REFERENCES categories(id)
);

The data: categories holds (1, Electronics) and (2, Stationery). The Laptop, Mouse and Monitor have category_id 1, the Pen has 2. Now renaming a category takes a change to one row:

UPDATE categories SET name = 'Gadget' WHERE id = 1;

Only 1 row changes. When products and categories are joined with a JOIN, the Laptop, Mouse and Monitor show the category Gadget. Deleting the Pen no longer deletes Stationery either, and Books can go into categories with no products. Splitting one table into several tables so every fact is stored once is called normalisation.

Steps to design a schema

  1. List the things you want to store. These things are called entities, for example customers, products, categories, orders.
  2. Make one table per entity, with a plural name (customers, products) and id as the primary key.
  3. Decide the relationships. One-to-many: the foreign key on the many side (category_id in products). Many-to-many: a pivot table (next section).
  4. Mark required columns with NOT NULL.
  5. Name a foreign key after the table in singular + _id: category_id, customer_id.

Many-to-many: the pivot table

In the JOIN lessons, orders had a product text column, so one order could only hold one product. Yet one order can contain many products, and one product is bought in many orders. This is a many-to-many relationship. The foreign key can't go on either side, because one cell (one column in one row) holds only one value. So you make a table in between, called a pivot table (or junction table):

CREATE TABLE order_items (
  order_id INTEGER NOT NULL REFERENCES orders(id),
  product_id INTEGER NOT NULL REFERENCES products(id),
  qty INTEGER NOT NULL,
  PRIMARY KEY (order_id, product_id)
);

One row of order_items means one product in one order, with its qty (the number of items). order_id points at orders, and product_id points at products. PRIMARY KEY (order_id, product_id) is a composite primary key: the same pair of order and product can't appear twice.

📌 Normal forms: 1NF, 2NF, 3NF

The rules of normalisation are usually written in levels. Each level is called a normal form (NF for short):

Not everything that looks alike is redundancy. If order_items gets a column for the price when it was bought, that price is a different fact from the price in products (today's price), so storing both is fine.

⚠️ Repeated names, links never fitted

TL;DR: A schema is the design of a database: which tables there are, what columns they have, and how the tables are linked. A good schema stores every fact once. If a category name is repeated on every product row (data redundancy), three problems appear. Renaming a category has to change many rows (3 rows in this example, not 1), deleting a category's last product deletes the category, and a new category can't go in without a product. Normalisation (splitting tables so every fact is stored once) divides it into categories and products, linked by category_id + a FOREIGN KEY, with NOT NULL on the required columns. A many-to-many relationship needs a pivot table: a linking table with two foreign keys. The levels of normalisation are called normal forms: 1NF (one cell, one value), 2NF (every column depends on the whole primary key), 3NF (no ordinary column depends on another ordinary column).

⚡ One fact, stored once

Guess first: a products table stores the category name on each row. The Electronics category has 3 products, so "Electronics" is written 3 times. To rename it to Gadget, how many rows have to change?

Three, one per product. If one is missed, the data contradicts itself.

Meaning: When the same fact, like a category name, is stored over and over in many rows, it is called data redundancy. Deleting a category's last product deletes the category too, because the category name is only recorded on the product rows.

⚡ Split it into two tables

Write:

CREATE TABLE categories (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL
);

CREATE TABLE products (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  price REAL NOT NULL,
  category_id INTEGER,
  FOREIGN KEY (category_id) REFERENCES categories(id)
);

Meaning: Categories are stored in their own table, categories. A product only stores its category's number in category_id, a foreign key that points at categories.id. NOT NULL marks the required columns: name and price.

In the exercise, the checker looks at NOT NULL on the required columns and the FOREIGN KEY to categories.

⚡ Rename in one row

Write:

UPDATE categories SET name = 'Gadget' WHERE id = 1;

Meaning: The Electronics category has id 1, so only 1 row in categories changes. All three electronics products show as Gadget when products is joined with categories through a JOIN. Splitting tables so each fact is stored once is called normalisation.

⚡ Many-to-many: the pivot table

Meaning: One order holds many products, and one product is in many orders: a many-to-many relationship. The foreign key can't go on either side, because one column in one row holds only one value. So you make a linking table (a pivot table), order_items, with two foreign keys: order_id and product_id. One row of order_items means one product in one order.

What you will learn