Keys and relationships between tables
Understand why databases split data across tables, how primary keys and foreign keys link them, and how to read a simple database diagram of one-to-many relationships.
π― By the end of this lesson you will be able to:
- Explain why data is split into several tables
- Identify primary keys and foreign keys
- Read a one-to-many relationship in a database diagram
Outline
What you'll learn and why it matters
So far each query used one table. Real questions need several: βWhich customers bought which products?β. Before you can combine tables (next lesson), you need to understand how they are linked. This short lesson has no new commands, but it makes joins easy.
Why not one big table?
Imagine storing every order with the customer's full details on every row:
| order_id | customer_name | customer_email | product_name | price |
|---|---|---|---|---|
| 1 | Asha Patel | asha@example.com | Python Basics Book | 450 |
| 2 | Asha Patel | asha@example.com | Notebook A5 | 120 |
| 12 | Asha Patel | asha@example.com | USB-C Cable | 299 |
Asha's details are repeated on every order. If she changes her email, you must update every row, and if you miss one, the data contradicts itself. Splitting the data fixes this: each fact is stored once, in the table where it belongs, and tables refer to each other by id. This design approach is called normalisation.
Primary keys and foreign keys
| Key | What it is | In our shop |
|---|---|---|
| Primary key | A column that uniquely identifies each row in its own table | customers.customer_id, products.product_id, orders.order_id |
| Foreign key | A column that holds another table's primary key, to link to it | orders.customer_id points to a customer; orders.product_id points to a product |
Look at the first few orders. customer_id and product_id are foreign keys:
SELECT * FROM orders WHERE order_id <= 4;Result: 4 rows
| order_id | customer_id | product_id | quantity | order_date | status |
|---|---|---|---|---|---|
| 1 | 1 | 3 | 1 | 2025-09-01 | delivered |
| 2 | 1 | 1 | 3 | 2025-09-01 | delivered |
| 3 | 2 | 5 | 1 | 2025-09-03 | delivered |
| 4 | 3 | 2 | 2 | 2025-09-05 | delivered |
Who placed order 3? Its customer_id is 2. Look that up in the customers table:
SELECT name FROM customers WHERE customer_id = 2;Result: 1 row
| name |
|---|
| Ravi Kumar |
Following a foreign key by hand like this works, but it takes two queries per order. A join does it for every row at once, which is the next lesson.
Reading a database diagram
This kind of picture is called an entity relationship diagram (ERD). The relationship between customers and orders is one-to-many: one customer can have many orders, but each order has exactly one customer. The same is true for products and orders. One-to-many is by far the most common relationship you will meet.
Practice
Guided exercise
Follow a foreign key
Which product was bought in order 7? Find the product_id of order 7, then look up that product's name.
- Run
SELECT product_id FROM orders WHERE order_id = 7;. - Use the id you get in a second query on
products.
Show the solution
SELECT name FROM products WHERE product_id = 7;Result: 1 row
| name |
|---|
| Water Bottle |
How to get there: Order 7 has product_id 7, which is the Water Bottle. Two queries for one order is slow; joins do this for every order in one query.
Your turn
Orders per customer id
Using only the orders table, count how many orders each customer id has placed. Which customer ids from 1 to 10 are missing, and what does that tell you?
Show the solution
SELECT customer_id, COUNT(*) AS orders
FROM orders
GROUP BY customer_id;Result: 8 rows
| customer_id | orders |
|---|---|
| 1 | 3 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
| 5 | 2 |
| 6 | 1 |
| 8 | 2 |
| 9 | 1 |
How to get there: Customer ids 7 and 10 are missing: Sara and Kofi have never ordered. A one-to-many relationship allows zero orders. Lesson 10 shows how to list such customers by name.
Common mistakes
Thinking customer_id means the same thing in every table
You see: Confusion about which column links where
Fix: It is a primary key in customers and a foreign key in orders. Always ask which table a column belongs to.
Storing names instead of ids in a linking table
You see: Mismatches when a name is spelled differently or changes
Fix: Link tables with ids; names belong in one table only.
Assuming every customer has an order
You see: Missing rows in reports
Fix: One-to-many means zero or more. Plan for the zero.
Recap
- Databases store each fact once and link tables with ids, to avoid repeated and contradictory data.
- A primary key uniquely identifies a row; a foreign key holds another table's primary key.
- One-to-many: one customer, many orders; each order, one customer.
- An ERD shows tables, their columns and the links between them.
Key terms
- Foreign key
- A column that refers to the primary key of another table.
- One-to-many
- A relationship where one row in a table can link to many rows in another.
- ERD
- Entity relationship diagram: a picture of tables and how they link.
- Normalisation
- Designing tables so each fact is stored once.
Check your understanding
Answer all 3 questions, then check your answers to see an explanation for each one.
1. In our shop, what is `orders.product_id`?
2. Why are customer details not repeated on every order row?
3. Customers and orders have a one-to-many relationship. What does that mean?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.