What Is Database Design

Database design is designing the tables, columns, relationships (how tables connect) and constraints (rules the database enforces) before writing code, so…

A database keeps data in tables, which are made of rows and columns. In most lessons of the SQL track, the tables were already made for you. You only ran commands such as SELECT, INSERT or JOIN. But someone designed each of those tables first. That person decided what data the app keeps, which tables the data is split into, which columns each table has, and how the tables connect. Deciding all that before you write code is called database design.

Database design is like drawing a floor plan before building a house. Moving a wall on paper is easy. Moving a wall once the house stands and people live in it is expensive. A database is the same. While it is still a plan, the structure of a table is easy to change. Once the app runs and its tables hold thousands of rows, changing a table's structure means moving all that data too.

Sunset Coffee: the example for the whole track

This track uses one example from start to finish: Sunset Coffee, a small online coffee shop. Its customers are Budi, Siti and Andi. Its products are Milk Coffee ($4.50), Palm Sugar Milk Coffee ($5.50), Milk Tea ($3.75) and Toast ($5.00), and the tables store each price in whole cents: $4.50 is 450. Customers place orders, and each order holds products.

A query is a command you send to the database, such as SELECT, WHERE and JOIN. How to write queries is taught in the SQL track. This track teaches the step before that: designing the tables, so queries are easy to write and give the right answer.

The trouble with one do-everything table

When it first opened, Sunset Coffee kept everything in one orders table, just like a spreadsheet:

id customer_name customer_phone product price
1 Budi 07700 900111 Milk Coffee 450
2 Budi 07700 900111 Toast 500
3 Siti 07700 900555 Milk Coffee 450
4 Andi 07700 900777 Milk Tea 375

Budi's phone number is written twice. Then Budi changes his number, and the app updates only his newest order (the row with id 2). After that the app asks for the list of customers and their numbers:

UPDATE orders SET customer_phone = '07700 900999' WHERE id = 2;

-- the customers and their numbers
SELECT DISTINCT customer_name, customer_phone FROM orders;

Before you look at the result, guess: does Budi now have one number, two numbers, or does the query fail with an error?

customer_name customer_phone
Budi 07700 900111
Budi 07700 900999
Siti 07700 900555
Andi 07700 900777

Budi now has two numbers, and there is no error at all. To the database, Budi's two rows are two records with no link between them, so it does not refuse the change. This do-everything table has three problems:

This table also gets slower as it gets bigger. Just to list the customers, the database has to read every order row.

The three-table design

In a tidy design, every thing (customer, product, order) has its own table. The orders table only stores the customer's id and the product's id. These three CREATE TABLE commands make the three tables:

CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  phone TEXT
);

CREATE TABLE products (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  price INTEGER NOT NULL  -- in cents: 450 is $4.50
);

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES customers(id),
  product_id INTEGER NOT NULL REFERENCES products(id)
);

With these three tables, the three problems are gone:

What if one order holds several products? This design can't store that yet. We fix it in the Cardinality & Relationships lesson.

The three stages of database design

The three-table design is usually made in three stages, and CREATE TABLE is only written in the third:

Stage What it does At Sunset Coffee
Conceptual Decide the entities and relationships There are customers, products, orders. A customer places orders, an order holds products.
Logical Design the tables and columns The tables customers (id, name, phone), orders (id, customer_id, product_id), and so on.
Physical Implement it in a DBMS CREATE TABLE in SQLite with the types INTEGER and TEXT, plus an index on columns that are searched often.

The goal: efficient, consistent, easy to reach

So database design is the process of designing the structure of tables, columns, relationships and constraints. Its goal is to organise data so it has three qualities:

Without planning you get the first orders table: redundant data, inconsistent data, and slow queries. A bigger server (the computer the database runs on) doesn't help. On the fastest server there is, Budi still has two numbers.

⚠️ What goes wrong when you start designing

TL;DR: Database design is designing the tables, columns, relationships (how tables connect) and constraints (rules the database enforces) before writing code, so data is efficient, consistent and easy to access. One do-everything table (orders, customers and products in one table) has three problems. The same data is stored again and again (redundant). The data easily contradicts itself (one customer, Budi, ends up with two phone numbers). Deleting an order can delete the customer along with it. In a tidy design, each thing has its own table, and the tables are linked by id (a unique number for each row). There are three stages: conceptual (decide the entities, the things whose data is stored, and their relationships), logical (design the tables and columns), and physical (implement it in a DBMS, the database software, such as SQLite, MySQL or PostgreSQL). This track uses the example of Sunset Coffee, an online coffee shop, with the tables customers, products, orders, and later order_items, profiles and phone_numbers.

⚡ One do-everything table

Write:

UPDATE orders SET customer_phone = '07700 900999' WHERE id = 2;

-- the customers and their numbers
SELECT DISTINCT customer_name, customer_phone FROM orders;
Guess first: in the orders table, the customer's phone number is written in every order row, and Budi has two orders. This code changes the number only in the order with id 2. Does Budi now have one number, two numbers, or does the code fail with an error?

Two numbers, and no error at all. Budi's other order still holds the old number. To the database, those two rows are two records with no link between them, so it does not refuse.

Meaning: orders is a do-everything table: customer data, Budi's number included, is written in every order row. Data that is stored again and again is called redundant. If you forget to update even one row, the data contradicts itself, or is inconsistent.

⚡ Every thing gets its own table

Write:

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES customers(id),
  product_id INTEGER NOT NULL REFERENCES products(id)
);

Meaning: In a tidy design, customers and products have their own tables: customers and products. PRIMARY KEY makes id a unique number for each row. The orders table only stores the customer's id (customer_id) and the product's id (product_id), and REFERENCES names where each id comes from. Budi's phone number is now only in customers, so changing it means changing 1 row.

⚡ Three stages, one goal

Meaning: Database design goes through three stages. Conceptual: decide the entities (things whose data is stored) and relationships (how entities connect). Logical: design the tables and columns. Physical: implement it in a DBMS (database software) such as SQLite. The goal is data that is efficient, consistent and easy to access.

In the exercise, your function returns a description of each stage. The text must match exactly, capital first letter included: "Decide the entities and relationships".

What you will learn