Sub topic 1
How Tables Relate: Primary Keys and Foreign Keys
Relational databases store information in tables. A table contains rows, and each row represents one thing, such as a customer or an order. Tables are useful separately, but related data is often split across several tables to avoid repeating the same values.
This chapter assumes you know basic SELECT, FROM, WHERE, and ORDER BY syntax. You should also know what a table, row, and column are.
Prerequisites
You should understand these terms:
- A primary key is a column, or group of columns, that uniquely identifies each row in a table.
- A foreign key is a column, or group of columns, that refers to a key in another table.
- A relationship describes how rows in two tables are connected.
Consider a database with customers and orders:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (id),
order_date DATE NOT NULL,
total DECIMAL(10, 2) NOT NULL
);The customers.id column is the primary key. Each customer must have a different id. The orders.id column is also a primary key, so each order has its own identity.
The orders.customer_id column is a foreign key. Its value can refer to customers.id. This creates a connection between an order and its customer.
One-to-many relationships
One customer can place many orders, but each order normally belongs to one customer. This is a one-to-many relationship:
- One row in
customerscan match many rows inorders. - Each row in
orderscan match at most one customer throughcustomer_id.
The foreign key usually stores the key from the parent table. Here, customers is the parent table, and orders is the child table. The child table stores customer_id so that it does not need to repeat the customer's name in every order row.
| Table | Key column | Purpose |
|---|---|---|
| `customers` | `id` | Uniquely identifies a customer |
| `orders` | `id` | Uniquely identifies an order |
| `orders` | `customer_id` | Refers to the customer connected to an order |
Valid and missing relationships
These rows create two customers and three orders:
INSERT INTO customers (id, name)
VALUES (1, 'Ada'), (2, 'Ben');
INSERT INTO orders (id, customer_id, order_date, total)
VALUES
(101, 1, '2026-01-05', 50.00),
(102, 1, '2026-02-10', 75.00),
(103, 2, '2026-02-11', 20.00);Orders 101 and 102 refer to Ada because their customer_id is 1. Order 103 refers to Ben. Since Ada has two orders, her customer data would appear twice in a result that combines customers with orders.
A foreign key can also be nullable unless the column is declared NOT NULL. A NULL customer_id means that an order has no customer value. It does not match any customer key. An inner join therefore excludes that order, while an outer join can preserve it. Outer joins are covered later.
Exercise
In the sample data, how many order rows can match customer id = 1, and why?
Show answer
Two rows, orders 101 and 102, because both rows have customer_id = 1.