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?
✗ Incorrect
ON UPDATE CASCADE automatically updates the foreign key values in the child table to match the new primary key value in the parent table.
If a foreign key has ON UPDATE SET NULL, what happens when the parent key is updated?
✗ Incorrect
ON UPDATE SET NULL sets the foreign key values to NULL when the referenced primary key is updated.
Which ON UPDATE option prevents the parent key from being updated if child rows exist?
✗ Incorrect
ON UPDATE RESTRICT blocks the update of the parent key if there are matching foreign keys in the child table.
What is the difference between ON UPDATE NO ACTION and ON UPDATE RESTRICT?
✗ Incorrect
ON UPDATE NO ACTION defers constraint checking until the end of the statement, while RESTRICT checks immediately and blocks the update if violated.
Which ON UPDATE behavior automatically updates child rows to match changes in the parent key?
✗ Incorrect
CASCADE updates child rows automatically when the parent key changes.
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.