Why split data into tables?
Imagine an online store. If every order included the customer's full name, address, and email, that information would be repeated over and over. If a customer moved, you would have to update hundreds of order records.
Instead, the store keeps one Customers table and one Orders table. Each order stores a small reference to the customer, not the whole customer record. This saves space and prevents mistakes.
Foreign keys
A foreign key is a column in one table that points to the primary key in another table. It is how tables relate to each other.
For example, the Orders table might have a customer_id column. Each value in customer_id matches an id in the Customers table. That connection says, "This order belongs to that customer."
One-to-many and one-to-one
Relationships have different shapes:
- One-to-one: one row in table A matches exactly one row in table B. This is less common.
- One-to-many: one row in table A can match many rows in table B. One customer can have many orders.
- Many-to-many: rows in table A can match many rows in table B, and vice versa. Students and classes are a common example. These usually need a third linking table.
Joins
A join is a way to combine rows from two tables into one result. It lets you ask questions that span multiple tables. For example, you can list every order along with the customer's name, even though the name is in a different table.
SELECT Customers.name, Orders.total
FROM Customers
JOIN Orders ON Customers.id = Orders.customer_id;
This query finds matching pairs of customers and orders using the foreign key, then returns the name and total together.
Real-world example: A music app has a Songs table and an Artists table. Each song has an artist_id. A join lets the app show the artist's name next to every song.
Link tables demo
Pick a customer, then click Show orders to see all the orders connected by the customer_id foreign key.
Customers
| id | name |
|---|---|
| 1 | Maya Chen |
| 2 | Leo Ramos |
| 3 | Sofia Kim |
Orders
| order_id | customer_id | total |
|---|---|---|
| 101 | 1 | $42.00 |
| 102 | 2 | $18.50 |
| 103 | 1 | $7.25 |
| 104 | 3 | $120.00 |
| 105 | 2 | $33.75 |
Relating tables in Python
Databases use keys to connect tables. This Python code links students, courses, and enrollments the same way a SQL JOIN does.
Check yourself
Think of a school scheduling app. It has Students, Classes, and Enrollments tables. Why might Enrollments need its own table instead of just storing a list of classes inside each student row?
Your answer:
Sample answer: Many students can take many classes. A separate Enrollments table links student IDs to class IDs. This avoids repeating class information and makes it easy to add or remove enrollments.
Quick quiz
1. What does a foreign key do?
2. Which relationship is one-to-many?
3. What is a join?
Keep learning
You understand how tables connect. Next, see how databases power real apps and why data safety matters.
Part of the Data and Databases series.