0
0
SQLquery~5 mins

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

Choose your learning style9 modes available
Recall & Review
beginner
What is a foreign key in a database?
A foreign key is a column or set of columns in one table that refers to the primary key in another table. It creates a link between the two tables to maintain data consistency.
Click to reveal answer
beginner
What does the ON UPDATE clause do in a foreign key constraint?
The ON UPDATE clause defines what happens to the foreign key values when the referenced primary key is updated. It controls how changes in the parent table affect the child table.
Click to reveal answer
intermediate
Explain the ON UPDATE CASCADE behavior.
ON UPDATE CASCADE means that when the primary key in the parent table is updated, the foreign key values in the child table are automatically updated to match the new value.
Click to reveal answer
intermediate
What happens with ON UPDATE SET NULL?
With ON UPDATE SET NULL, if the primary key in the parent table is updated, the foreign key values in the child table are set to NULL, indicating no reference.
Click to reveal answer
advanced
Describe the difference between ON UPDATE RESTRICT and ON UPDATE NO ACTION.
ON UPDATE RESTRICT prevents the update of the primary key if there are matching foreign keys in the child table. ON UPDATE NO ACTION also prevents the update but checks the constraint at the end of the statement, which can allow deferred checks.
Click to reveal answer
What does ON UPDATE CASCADE do in a foreign key constraint?
ASets foreign keys to NULL when parent key changes
BDeletes rows in child table when parent key changes
CUpdates foreign keys in child table when parent key changes
DPrevents updates to parent key if child rows exist
If a foreign key has ON UPDATE SET NULL, what happens when the parent key is updated?
AForeign key values are deleted
BForeign key values are set to NULL
CUpdate is blocked
DForeign key values are unchanged
Which ON UPDATE option prevents the parent key from being updated if child rows exist?
ACASCADE
BSET NULL
CSET DEFAULT
DRESTRICT
What is the difference between ON UPDATE NO ACTION and ON UPDATE RESTRICT?
ANO ACTION checks constraints at statement end, RESTRICT checks immediately
BNO ACTION allows updates, RESTRICT blocks them
CNO ACTION deletes child rows, RESTRICT sets them NULL
DThey behave exactly the same
Which ON UPDATE behavior automatically updates child rows to match changes in the parent key?
ACASCADE
BSET NULL
CRESTRICT
DNO ACTION
Explain the different ON UPDATE behaviors available for foreign keys and when you might use each.
Think about how changes in the parent table should affect related child rows.
You got /5 concepts.
    Describe a real-life example where ON UPDATE CASCADE would be helpful.
    Imagine changing an ID that is used in many places.
    You got /4 concepts.