Before continuing: This entry is part of the constraints and keys guide. If you are new to the topic, start with CREATE TABLE.
Example
SQLite only applies these actions when foreign key enforcement is enabled on the connection. The example turns it on explicitly and uses two orders to show that the cascade only affects the parent that was deleted.
PRAGMA foreign_keys = ON;
CREATE TEMP TABLE orders_cascade (
id INTEGER PRIMARY KEY,
customer TEXT NOT NULL
);
CREATE TEMP TABLE detail_cascade (
id INTEGER PRIMARY KEY,
order_id INTEGER NOT NULL
REFERENCES orders_cascade(id) ON DELETE CASCADE,
product TEXT NOT NULL
);
INSERT INTO orders_cascade VALUES
(1, 'Amy'),
(2, 'Liam');
INSERT INTO detail_cascade VALUES
(101, 1, 'Keyboard'),
(102, 1, 'Mouse'),
(103, 2, 'Monitor');
DELETE FROM orders_cascade
WHERE id = 1;
SELECT id, order_id, product
FROM detail_cascade
ORDER BY id;Rows 101 and 102 disappear because they depended on order 1. Row 103 stays because it belongs to order 2. That precision is a direct result of the foreign key.
How it is used
A foreign key links a child table to a parent table. For orders, orders can be the parent table and order_items the child table: each line belongs to one specific order.
Direction matters: deleting a line from order_items does not delete the order. The cascade fires when the referenced parent row disappears.
What changes from case to case
When CASCADE matches the business rule
CASCADE fits when the child row cannot meaningfully exist without its parent. Order lines are a clear example: a line without its order no longer represents anything in the business.
| Scenario | Action to consider | Idea |
|---|---|---|
| The child makes no sense without the parent | CASCADE | Deleting both keeps the model consistent |
| Parent and child are independent entities | RESTRICT / NO ACTION | Forces you to resolve the relationship explicitly |
| The relationship is optional and the child can outlive the parent | SET NULL | Keep the child row and remove the link, if the column accepts NULL |
PostgreSQL recommends choosing the action based on the semantic relationship between the objects. CASCADE is convenient, but an automatic rule is only correct if it reflects how the business really works.
Do not confuse ON DELETE CASCADE with DROP ... CASCADE
Both contain the word CASCADE, but they solve different problems. ON DELETE CASCADE belongs to a FOREIGN KEY and acts on related rows. Instead, options like DROP ... CASCADE affect dependent schema objects, such as constraints or views, depending on the engine and the statement.
How to check a cascading delete before running it
A cascading delete can affect a lot of rows. Before deleting the parent, query the child rows that depend on it:
SELECT id, order_id, product
FROM order_items
WHERE order_id = 42;For sensitive operations, combine this check with a transaction: run the change, review the result and choose between COMMIT and ROLLBACK when your engine and workflow allow it.
Indexes on the child column
When a parent row is deleted, the engine has to find the child rows that reference it. PostgreSQL notes that indexing the referencing columns of the child table is usually a good idea, even though declaring a foreign key does not necessarily create that index for you.
CREATE INDEX idx_order_items_order_id
ON order_items (order_id);Do not add indexes on autopilot: check the data volume, the queries and the engine. The key point is that referential integrity and indexing strategy are related decisions, but not the same decision.