In SQL, CASCADE means “automatically apply this action to related records” when a change happens in a parent table.
It’s mainly used with foreign key constraints.
Why CASCADE exists
Databases often have parent–child relationships.
Example:
Users(parent)Orders(child → referencesUsers.UserId)- CASCADE helps keep data consistent without writing extra queries.
Common CASCADE types
1. ON DELETE CASCADE
When a row in the parent table is deleted, all related rows in the child table are deleted automatically.
CREATE TABLE Orders (
OrderId INT PRIMARY KEY,
UserId INT,
FOREIGN KEY (UserId)
REFERENCES Users(UserId)
ON DELETE CASCADE
);
What happens:
- Delete a user
- All their orders are deleted automatically
Good for:
- Logs
- History tables
- Dependent data that should not exist without the parent
2. ON UPDATE CASCADE
When the parent key value changes, the child table is updated automatically.
FOREIGN KEY (UserId)
REFERENCES Users(UserId)
ON UPDATE CASCADE
What happens:
Users.UserIdchangesOrders.UserIdupdates automatically
Less common because:
- Primary keys usually don’t change
CASCADE vs other options
| Option | Behavior |
|---|---|
CASCADE |
Automatically updates/deletes child rows |
RESTRICT / NO ACTION |
Prevents delete/update if child rows exist |
SET NULL |
Sets child foreign key to NULL |
SET DEFAULT |
Sets child foreign key to default value |
Example:
ON DELETE SET NULL
Real-world example
DELETE FROM Users WHERE UserId = 10;
With ON DELETE CASCADE:
- User 10 → deleted
- Orders of User 10 → deleted automatically
Without CASCADE:
- Error: Cannot delete because related records exist
Important cautions
- CASCADE can delete large amounts of data unintentionally
- Always understand relationships before using it
- Avoid CASCADE on critical business data unless required
Simple one-line definition
CASCADE in SQL automatically applies delete or update operations from a parent table to its related child records.