Nishaglobal Education logo

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.

Lesson 8 of 1220 minQuiz: 3 questions

🎯 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
  1. What you'll learn and why it matters
  2. Why not one big table?
  3. Primary keys and foreign keys
  4. Reading a database diagram
  5. Practice
  6. Common mistakes
  7. Recap
  8. Quiz
  9. Mark lesson complete

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_idcustomer_namecustomer_emailproduct_nameprice
1Asha Patelasha@example.comPython Basics Book450
2Asha Patelasha@example.comNotebook A5120
12Asha Patelasha@example.comUSB-C Cable299

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

KeyWhat it isIn our shop
Primary keyA column that uniquely identifies each row in its own tablecustomers.customer_id, products.product_id, orders.order_id
Foreign keyA column that holds another table's primary key, to link to itorders.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:

SQL
SELECT * FROM orders WHERE order_id <= 4;

Result: 4 rows

Query result
order_idcustomer_idproduct_idquantityorder_datestatus
11312025-09-01delivered
21132025-09-01delivered
32512025-09-03delivered
43222025-09-05delivered

Who placed order 3? Its customer_id is 2. Look that up in the customers table:

SQL
SELECT name FROM customers WHERE customer_id = 2;

Result: 1 row

Query result
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

customerscustomer_idPKnamecitycountryemailjoined_onordersorder_idPKcustomer_idFKproduct_idFKquantityorder_datestatusproductsproduct_idPKnamecategoryprice1manymany1PK = primary key Β· FK = foreign key (points to a PK)
Our shop's three tables. Each order belongs to one customer and one product; a customer or product can appear in many orders. Lines join each foreign key to the primary key it points to.

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.

  1. Run SELECT product_id FROM orders WHERE order_id = 7;.
  2. Use the id you get in a second query on products.
Show the solution
Solution
SELECT name FROM products WHERE product_id = 7;

Result: 1 row

Query result
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
Solution
SELECT customer_id, COUNT(*) AS orders
FROM orders
GROUP BY customer_id;

Result: 8 rows

Query result
customer_idorders
13
22
32
42
52
61
82
91

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. 1. In our shop, what is `orders.product_id`?

  2. 2. Why are customer details not repeated on every order row?

  3. 3. Customers and orders have a one-to-many relationship. What does that mean?

Answer all 3 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.