Sub topic 7
Foreign keys: linking tables
Prerequisites: I assume you can read a table and understand that a database table has rows and columns. You should also recognize a primary key as a value that identifies one row.
A foreign key is a column, or group of columns, that refers to a key in another table. It creates a declared link between related data. The referenced key is usually a primary key.
For example, one customer can place many orders. The customers table stores each customer once. The orders table stores each order and keeps the customer’s identifier in customer_id.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL
REFERENCES customers (customer_id),
order_date DATE NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL
CHECK (total_amount >= 0)
);
INSERT INTO customers (customer_id, name, email)
VALUES (1, 'Ada Lovelace', '[email protected]');
INSERT INTO orders (order_id, customer_id, order_date, total_amount)
VALUES (100, 1, '2026-01-15', 59.90);The REFERENCES customers (customer_id) clause makes orders.customer_id a foreign key. Because the column is also NOT NULL, every order must name an existing customer. An attempt to insert an order with customer_id = 999 fails if customer 999 does not exist.
| Table | Key column | Meaning |
|---|---|---|
| `customers` | `customer_id` primary key | Identifies one customer |
| `orders` | `order_id` primary key | Identifies one order |
| `orders` | `customer_id` foreign key | Points to the order’s customer |
Reading related rows
A foreign key does not copy the customer’s name into every order. It stores only the key value. A join combines the tables when you need information from both.
SELECT c.name, o.order_id, o.order_date, o.total_amount
FROM customers AS c
JOIN orders AS o
ON o.customer_id = c.customer_id
ORDER BY o.order_date;The join condition compares the foreign key with the referenced primary key. An inner JOIN returns only orders that have a matching customer. With a correctly enforced foreign key, every stored order should have such a customer.
Relationship types
A foreign key from orders to customers represents a one-to-many relationship: one customer can be related to many orders, while each order belongs to one customer.
Many-to-many relationships need a separate junction table. For example, an order can contain many products, and a product can appear in many orders.
CREATE TABLE order_items (
order_id INTEGER NOT NULL REFERENCES orders (order_id),
product_id INTEGER NOT NULL REFERENCES products (product_id),
quantity INTEGER NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id)
);The composite primary key prevents the same product from appearing twice in one order. The two foreign keys connect the junction row to both parent tables.
Common errors and delete behavior
A foreign key must reference a primary key or another column with a uniqueness guarantee. The two sides should use compatible data types. Forgetting NOT NULL allows an optional relationship: the foreign-key column may contain NULL, which means “no related value,” rather than an invalid identifier.
Deleting a referenced customer also needs a policy. The database may reject the delete when orders still refer to that customer. ON DELETE CASCADE deletes dependent orders automatically, while ON DELETE SET NULL keeps the orders but removes their reference. Choose carefully because these actions can remove or change many rows.
Exercise
Why can an order table store customer_id instead of the customer's name?
Show answer
The foreign key identifies the customer, and a join can retrieve the current name without duplicating it in every order.