Complete the code to specify the action when the referenced key is updated.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id) ON UPDATE [1]
);The ON UPDATE CASCADE option means that when the referenced key in the parent table changes, the foreign key in the child table updates automatically to match.
Complete the code to prevent updates to the referenced key if child rows exist.
CREATE TABLE enrollments (
enrollment_id INT PRIMARY KEY,
student_id INT,
FOREIGN KEY (student_id) REFERENCES students(id) ON UPDATE [1]
);The ON UPDATE RESTRICT option prevents updating the referenced key if there are matching rows in the child table. It stops the update to keep data consistent.
Fix the error in the foreign key definition to correctly handle updates by setting child keys to NULL.
CREATE TABLE payments (
payment_id INT PRIMARY KEY,
order_id INT,
FOREIGN KEY (order_id) REFERENCES orders(id) ON UPDATE [1]
);The ON UPDATE SET NULL option sets the foreign key in the child table to NULL when the referenced key in the parent table is updated. This requires the foreign key column to allow NULL values.
Fill both blanks to create a foreign key that updates child keys on parent update and deletes child rows on parent delete.
CREATE TABLE comments ( comment_id INT PRIMARY KEY, post_id INT, FOREIGN KEY (post_id) REFERENCES posts(id) ON UPDATE [1] ON DELETE [2] );
Using ON UPDATE CASCADE updates child keys when the parent key changes. Using ON DELETE CASCADE deletes child rows when the parent row is deleted. This keeps data consistent automatically.
Fill all three blanks to define a foreign key that restricts updates, sets child keys to NULL on delete, and uses NO ACTION on update.
CREATE TABLE subscriptions ( subscription_id INT PRIMARY KEY, user_id INT, FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE [1] ON DELETE [2] ON UPDATE [3] );
The foreign key uses ON UPDATE RESTRICT to block updates if child rows exist, ON DELETE SET NULL to set child keys to NULL when parent rows are deleted, and ON UPDATE NO ACTION to defer update checks until the end of the transaction.