Sub topic 12
Foreign Keys and Deleting Parent Rows
Assume you know basic SELECT queries with WHERE, tables, rows, columns, primary keys, and foreign keys. This chapter focuses on what happens when you delete a row that other rows reference.
A foreign key connects a column in one table to a key in another table. The referenced table contains the parent row. The table containing the foreign key contains the child rows.
In this example, customers is the parent table and orders is the child table. Each order must refer to an existing customer.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL
REFERENCES customers (id),
total NUMERIC(10, 2) NOT NULL
);The foreign key prevents an order from referring to a customer that does not exist.
Why a parent delete can fail
Suppose customer 1 has orders. This statement attempts to remove the customer:
DELETE FROM customers
WHERE id = 1;The database usually rejects the statement because the orders would become orphaned rows. An orphan is a child row whose referenced parent no longer exists. Rejecting the delete is commonly called the RESTRICT or NO ACTION behavior. It protects related data by default in many database systems.
You can delete the child rows first, then delete the parent:
BEGIN;
DELETE FROM orders
WHERE customer_id = 1;
DELETE FROM customers
WHERE id = 1;
COMMIT;A transaction makes these statements act as one unit. If a later statement fails, use ROLLBACK instead of COMMIT to undo the earlier changes.
Choosing an action for parent deletion
You can define what should happen when a referenced parent is deleted.
| Action | Result when the parent is deleted |
|---|---|
| `RESTRICT` or `NO ACTION` | Reject the delete while child rows exist |
| `CASCADE` | Delete the related child rows automatically |
| `SET NULL` | Keep child rows but clear the foreign key |
For example, CASCADE fits dependent data that has no meaning without its parent:
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL
REFERENCES customers (id)
ON DELETE CASCADE,
total NUMERIC(10, 2) NOT NULL
);With this definition, deleting a customer also deletes that customer's orders. Use this carefully. A single parent delete can remove many child rows.
SET NULL requires the foreign key column to allow null values:
customer_id INTEGER REFERENCES customers (id) ON DELETE SET NULLThis can suit optional relationships, such as preserving an order after deleting an employee who handled it.
Before deleting a parent, inspect its children:
SELECT id, total
FROM orders
WHERE customer_id = 1;Then check the database's foreign key definition and estimate the number of affected rows. Different systems expose these definitions through different catalog tables, so the exact inspection query is dialect-specific.
Exercise
A customer has three orders, and the foreign key uses the default restrictive behavior. What is the safe order for permanently deleting both the customer and those orders?
Show answer
Start a transaction, delete the three orders using WHERE customer_id = the customer ID, delete the customer using WHERE id = the same ID, and COMMIT only after checking the results. Use ROLLBACK if anything is wrong.