Please enable JavaScript to use CodeHS

Data and Databases

Table relationships

Real apps rarely store everything in one table. Orders belong to customers, posts belong to users, and classes belong to teachers. Relational databases use foreign keys to connect tables without repeating data.

Relations ~10 min Middle / High School
The Big Idea

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.


Key Concept

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."


Vocabulary

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.

How It Works

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.


Interactive

Link tables demo

Pick a customer, then click Show orders to see all the orders connected by the customer_id foreign key.

Customers

idname
1Maya Chen
2Leo Ramos
3Sofia Kim

Orders

order_idcustomer_idtotal
1011$42.00
1022$18.50
1031$7.25
1043$120.00
1052$33.75

Code

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.


Your Turn

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.


Check Yourself

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.