0
0
SQLquery~5 mins

Foreign key ON DELETE behavior in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the ON DELETE CASCADE option do in a foreign key constraint?
It automatically deletes all rows in the child table that reference the deleted row in the parent table. This keeps data consistent by removing dependent records.
Click to reveal answer
beginner
Explain the ON DELETE SET NULL behavior in foreign keys.
When a referenced row in the parent table is deleted, the foreign key column in the child table is set to NULL. This means the child row no longer points to a parent row.
Click to reveal answer
intermediate
What happens if a foreign key has ON DELETE RESTRICT and you try to delete a parent row?
The deletion is blocked if there are any child rows referencing the parent. You must first remove or update child rows before deleting the parent.
Click to reveal answer
advanced
Describe the ON DELETE NO ACTION behavior.
It prevents deletion of a parent row if child rows exist, similar to RESTRICT. The difference is in timing: NO ACTION checks constraints at the end of the statement or transaction.
Click to reveal answer
beginner
What is the default behavior if ON DELETE is not specified in a foreign key?
The default is usually NO ACTION, meaning the database will prevent deleting a parent row if child rows exist referencing it.
Click to reveal answer
What does ON DELETE CASCADE do when a parent row is deleted?
ADeletes all child rows referencing the parent row
BSets child foreign keys to NULL
CPrevents deletion of the parent row
DDoes nothing automatically
Which ON DELETE option sets the foreign key in child rows to NULL when the parent is deleted?
ACASCADE
BRESTRICT
CSET NULL
DNO ACTION
If a foreign key uses ON DELETE RESTRICT, what happens when you try to delete a parent row with children?
ADeletion is blocked until children are removed
BParent row is deleted and children updated
CChildren are deleted automatically
DForeign keys are set to NULL
What is the main difference between NO ACTION and RESTRICT in ON DELETE behavior?
ANO ACTION deletes children, RESTRICT does not
BThey behave exactly the same
CNO ACTION sets foreign keys to NULL, RESTRICT blocks deletion
DRESTRICT checks constraints immediately, NO ACTION checks at statement end
What is the default ON DELETE behavior if none is specified?
ASET NULL
BNO ACTION
CCASCADE
DRESTRICT
Describe the different ON DELETE behaviors available for foreign keys and when you might use each.
Think about how deleting a parent row affects related child rows.
You got /5 concepts.
    Explain why ON DELETE CASCADE can be useful and what risks it might have.
    Consider a real-life example like deleting a customer and their orders.
    You got /4 concepts.