ON DELETE CASCADE vs SET NULL
Fast recall
Quick revision
Core answer
Understanding of referential integrity options and their practical impact on data consistency and safety
Using ON DELETE CASCADE everywhere without thinking through the consequences, leading to accidental large-scale data deletion
NO ACTION is the SQL default, while RESTRICT is another blocking action; exact timing differs by database, and CASCADE chains must be reviewed
Interview-ready guide
Interview answer
ON DELETE CASCADE and ON DELETE SET NULL control child rows when a referenced parent is deleted.
CASCADE removes the matching children. SET NULL preserves them and clears the relationship, which requires nullable foreign-key columns.
For example, deleting a post may cascade to comments that belong only to that post. editor_id` to NULL so the articles remain. SQL uses NO ACTION when no action is specified; RESTRICT is also a blocking choice, with database-specific timing and support.
I choose from the real ownership and retention rule, then trace any cascade chain and test the delete in a transaction. JPA orphanRemoval is a separate ORM lifecycle feature and should not be confused with the database's referential action.
ON DELETE CASCADE vs SET NULL
Direct Answer
ON DELETE CASCADE automatically deletes child rows when the parent row is deleted. ON DELETE SET NULL sets the foreign key column to NULL instead, preserving the child row. Use CASCADE when the child has no independent meaning without the parent (e.g., order items without an order). Use SET NULL when the child can exist independently (e.g., an order after a customer is deleted).
Code Example
-- CASCADE: deleting an order automatically deletes its order_items
CREATE TABLE order_items (
id INT PRIMARY KEY,
order_id INT NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE
);
-- SET NULL: deleting an author keeps the posts but nulls their author_id
CREATE TABLE posts (
id INT PRIMARY KEY,
author_id INT, -- must be nullable for SET NULL
FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE SET NULL
);
Use CASCADE when children cannot exist without the parent; use SET NULL when the child should survive as an orphan.
Common Mistakes & Pitfalls
- Using CASCADE without tracing the graph: one parent delete can remove many owned rows through several foreign keys. Review retention and recovery first.
- Combining SET NULL with a required relationship: the referencing columns must accept NULL, and the application must define what the missing relationship means. A database may reject the DDL or the delete depending on the product and schema.
- Choosing actions table by table without a lifecycle rule: different relationships may correctly need different actions, but the reason should follow ownership, audit, and retention requirements.
- Treating soft delete as automatic protection: queries, unique constraints, cascades, and restoration still need an explicit design.
Check your understanding
Answer these without looking back at the article.
- What is the difference between ON DELETE RESTRICT and ON DELETE NO ACTION in PostgreSQL?
- How do you handle the case where you want to soft-delete a parent without breaking child references?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.