Sub topic 12
CASCADE and RESTRICT
Prerequisites
This chapter assumes that you know primary keys, foreign keys, views, and basic CREATE TABLE and DROP TABLE statements. A dependency is an object that uses another object. For example, a foreign key in orders depends on the customers table.
Why dependencies matter
Suppose orders.customer_id references customers.id. The database prevents you from dropping customers while the foreign key still exists. Otherwise, the constraint would refer to a table that no longer exists.
A view can also depend on a table:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id) REFERENCES customers (id)
);
CREATE VIEW customer_order_counts AS
SELECT customer_id, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id;The dependency structure is:
RESTRICT: refuse unsafe removal
RESTRICT tells the database to reject the operation if dependent objects exist. In PostgreSQL, this is the default behavior for DROP TABLE:
DROP TABLE customers RESTRICT;This statement fails because the foreign key depends on customers. The view may also be reported as a dependency, depending on the database's dependency checks.
A failed DROP TABLE does not remove the table or its dependencies. You can inspect the error, decide which objects are still needed, and remove or change them deliberately.
RESTRICT is the safer choice when a mistake could cause data loss or break application code.
CASCADE: remove dependencies automatically
CASCADE tells the database to remove dependent objects as part of the operation:
DROP TABLE customers CASCADE;In PostgreSQL, this drops the customers table, the foreign key constraint on orders, and dependent views. It does not drop the orders table itself merely because its constraint was removed. The table remains, but customer_id is no longer protected by that foreign key.
This behavior can be surprising. A cascade can remove objects that are not visible in the command itself.
Choosing between RESTRICT and CASCADE
| Option | Behavior | Typical use |
|---|---|---|
| `RESTRICT` | Fails when dependencies exist | Protect important schema objects |
| `CASCADE` | Removes the target and supported dependencies | Deliberate teardown or controlled migrations |
Prefer RESTRICT while discovering a schema. The error reveals that more planning is needed. Use CASCADE when the dependent objects are temporary, disposable, or explicitly included in the migration plan.
Do not confuse this operation with a cascading foreign-key action such as ON DELETE CASCADE. That action deletes child rows when a parent row is deleted. DROP TABLE ... CASCADE changes the schema by removing objects or constraints.
Dialect differences
Dependency behavior and syntax vary by database system. PostgreSQL supports RESTRICT and CASCADE for many schema operations. SQL Server commonly requires you to drop foreign keys and dependent objects explicitly. MySQL and SQLite have their own rules and may treat some cascade options differently.
Check the documentation for the exact database and version before running a migration. Never assume that a statement's behavior is portable just because its SQL syntax is accepted.
Exercise
Why might DROP TABLE customers RESTRICT be preferable to DROP TABLE customers CASCADE during an investigation?
Show answer
RESTRICT stops when dependencies exist, allowing you to inspect them before anything is removed. CASCADE may remove dependent constraints, views, or other objects automatically.