- Home
- Interview Questions
- SQL
- How do ON DELETE CASCADE, SET NULL and RESTRICT work, and wh…
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: Referential actions decide what happens to child rows when a referenced parent row is deleted or its key is updated. CASCADE deletes or updates the children, SET NULL clears the foreign key, and RESTRICT or NO ACTION block the change. Cascades become dangerous when they silently remove large amounts of data, cause heavy locking, or run on unindexed foreign keys.
Detailed explanation
A foreign key can declare ON DELETE and ON UPDATE actions:
- CASCADE: Deleting the parent deletes the matching child rows. Updating the parent key updates the child foreign key values.
- SET NULL: Child foreign key columns are set to NULL. These columns must be nullable.
- SET DEFAULT: Child columns are set to their default value, and that value must exist in the parent. InnoDB parses this option but rejects it.
- RESTRICT: The parent change fails if any child rows exist.
- NO ACTION: Also blocks the change, and it is the default. In PostgreSQL, NO ACTION is checked at the end of the statement and can be deferred to commit if the constraint is DEFERRABLE, while RESTRICT is checked immediately. In MySQL the two behave the same.
Support varies by product. Oracle supports only ON DELETE CASCADE and ON DELETE SET NULL, with no ON UPDATE actions. SQL Server supports NO ACTION, CASCADE, SET NULL and SET DEFAULT, but has no RESTRICT keyword. It also rejects a foreign key that would create multiple cascade paths or cycles to the same table.
Why cascades can be dangerous:
- Hidden mass deletion: Cascades chain through tables. Deleting one customer can remove orders, then order items, then payments and shipments. A single mistaken DELETE can wipe audit-relevant data.
- Locking and performance: All cascaded deletes run in one transaction. Deleting a parent with millions of children holds locks for a long time, grows the transaction log, and can block other users.
- Unindexed foreign keys: For every parent delete, the database must find the matching child rows. If the child foreign key column has no index, this means a full scan of the child table. MySQL InnoDB creates this index automatically. PostgreSQL, SQL Server and Oracle do not, and in Oracle an unindexed foreign key also causes wider table-level locking on the child.
- Side effects you do not see: In MySQL, rows removed by a cascade do not fire triggers on the child table. Any audit logic in those triggers is silently skipped.
Rule of thumb: Use CASCADE for true composition, where the child has no meaning without its parent, such as order items under an order. Use RESTRICT or NO ACTION for important business entities like customers and invoices, and handle their removal through soft deletes or explicit, batched cleanup.
Example
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id)
REFERENCES customers(customer_id)
ON DELETE RESTRICT -- never lose orders by accident
);
CREATE TABLE order_items (
item_id INT PRIMARY KEY,
order_id INT NOT NULL,
CONSTRAINT fk_items_order FOREIGN KEY (order_id)
REFERENCES orders(order_id)
ON DELETE CASCADE -- items cannot exist without the order
);
CREATE TABLE tickets (
ticket_id INT PRIMARY KEY,
assignee_id INT NULL,
CONSTRAINT fk_tickets_emp FOREIGN KEY (assignee_id)
REFERENCES employees(emp_id)
ON DELETE SET NULL -- ticket stays, becomes unassigned
);
-- Index the child FK so parent deletes do not scan the table
CREATE INDEX ix_order_items_order_id ON order_items(order_id);Common mistakes
- Adding ON DELETE CASCADE everywhere so that deletes simply work, without mapping out the full chain of tables it reaches.
- Assuming a foreign key automatically creates an index on the child column. Only some engines, such as InnoDB, do this.
- Using SET NULL on a column declared NOT NULL. The constraint definition or the delete will fail.
- Treating RESTRICT and NO ACTION as identical in every database, without knowing the deferred-checking difference in PostgreSQL.
Follow-up questions
- How would you safely delete a parent row that has millions of child rows?
- What are deferrable constraints, and when would you use them?
- Would you choose soft deletes or cascading hard deletes for a customer table, and why?
Interview tip: Always pair your cascade answer with the point about indexing child foreign key columns. Interviewers treat that as the mark of someone who has run these deletes in production.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More hard SQL questions
Related Questions
- What is the difference between a view and a materialized view, and when would you use each? · SQL · 1 ans
- How does BCNF differ from 3NF, and why can BCNF decomposition lose a dependency? · SQL · 1 ans
- What are insertion, update and deletion anomalies, and how does normalization prevent them? · SQL · 1 ans
- What is the difference between a primary key and a unique key in SQL? · SQL · 1 ans
- What are the constraints in Java generics and why are they used? · Java · 1 ans
- Why does WHERE column = NULL return no rows, and how should you filter NULLs correctly? · SQL · 1 ans
- Why can wrapping an indexed column in a function in WHERE make a query slow? · SQL · 1 ans
- How does a recursive CTE execute step by step, and how do you stop it looping forever? · SQL · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)