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:
- Redundant: the same data (Budi's number) is stored again and again. If Budi has 40 orders, his number is written 40 times, and changing it means updating 40 rows.
- Inconsistent: if you forget to update even one row, the data contradicts itself, like Budi's two numbers.
- Lost by accident: Andi cancels his only order, so the app deletes that row with
DELETE FROM orders WHERE id = 4;. Andi's name and number are lost with it, though Andi is still a customer. The opposite happens too: the new product Palm Sugar Milk Coffee can't be recorded until someone orders it, because this table only holds orders.
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)
);
id INTEGER PRIMARY KEY: theidcolumn is the primary key, a column that holds a unique number for each row. With that number you can point at exactly one customer, even when two customers have the same name.customer_id ... REFERENCES customers(id): thecustomer_idcolumn is a foreign key, a column that points at theidin another table, herecustomers. You met foreign keys in the SQL lesson on table relationships.NOT NULL: the column must be filled in. A rule that the database enforces, such asNOT NULL, is called a constraint.
With these three tables, the three problems are gone:
- Changing Budi's number takes one command,
UPDATE customers SET phone = '07700 900999' WHERE id = 1;. Only 1 row changes. All of Budi's orders point at that row throughcustomer_id, so they all use the new number. - Cancelling Andi's order deletes one row in
orders, but Andi stays incustomers. - Palm Sugar Milk Coffee can go into
productsbefore anyone orders it.
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. |
- An entity is a thing whose data is stored (a customer, a product). A relationship is how entities connect, for example a customer places orders and an order holds products. The result of the conceptual stage is usually drawn as an ER diagram, taught in the ER Diagram Basics lesson.
- The logical stage does not choose the database software yet. It is also where normalisation (its levels are called 1NF, 2NF and so on) happens: tidying the tables so no data is stored twice.
- A DBMS (Database Management System) is the software that stores and manages a database, for example SQLite, MySQL and PostgreSQL. In the physical stage, you pick the exact data types that DBMS uses (
TEXTin SQLite,VARCHAR(100)in MySQL). You also decide on indexes (helper lists that make searching faster) and on backups.
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:
- The data is efficient: no pointless copies, and fast queries.
- The data is consistent: one fact is stored in one place, so it can't contradict itself.
- The data is easy to access: the app's questions are answered by simple queries.
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
- "One table for now, we'll tidy it later." Tidying it later means moving production data (the real data of your users) while people use the app. Tidy it while the tables are still empty.
- Pointing by name, not by id. Two people can have the same name, and names change. Store
customer_id, notcustomer_name. - Mixing up logical and physical. "The
customerstable has aphonecolumn" is a logical decision. "phonehas the typeTEXTin SQLite" is a physical decision. - A typo in the exercise text. Your function must return exactly the text the task asks for, capital first letter included:
"Decide the entities and relationships", not"decide the entities & relationships".
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".