Recall & Review
beginner
What is a BEFORE UPDATE trigger in MySQL?
A BEFORE UPDATE trigger is a special procedure that runs automatically before a row in a table is updated. It lets you check or change data before the update happens.
Click to reveal answer
beginner
When does a BEFORE UPDATE trigger execute?
It executes right before the database changes the data in a row during an UPDATE operation.
Click to reveal answer
intermediate
Can you modify the new values in a BEFORE UPDATE trigger?
Yes, you can change the new values using the NEW keyword inside the trigger before they are saved to the table.
Click to reveal answer
intermediate
What happens if a BEFORE UPDATE trigger signals an error?
The update operation is stopped and the error is returned. No changes are made to the row.
Click to reveal answer
advanced
Write a simple BEFORE UPDATE trigger that prevents changing a user's email to an empty string.
CREATE TRIGGER prevent_empty_email BEFORE UPDATE ON users FOR EACH ROW BEGIN IF NEW.email = '' THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Email cannot be empty'; END IF; END;
Click to reveal answer
When does a BEFORE UPDATE trigger run in MySQL?
✗ Incorrect
A BEFORE UPDATE trigger runs before the database updates a row.
Which keyword lets you access the new values in a BEFORE UPDATE trigger?
✗ Incorrect
The NEW keyword refers to the new values that will be saved.
What happens if a BEFORE UPDATE trigger raises an error?
✗ Incorrect
Raising an error in a BEFORE UPDATE trigger stops the update.
Can a BEFORE UPDATE trigger modify the data before it is saved?
✗ Incorrect
BEFORE UPDATE triggers can change the NEW values before saving.
Which SQL statement creates a BEFORE UPDATE trigger?
✗ Incorrect
Triggers are created with CREATE TRIGGER and specify BEFORE UPDATE.
Explain what a BEFORE UPDATE trigger does and give an example use case.
Think about checking or changing data before it is saved.
You got /3 concepts.
Describe how you would prevent invalid data from being saved using a BEFORE UPDATE trigger.
Use conditional logic and SIGNAL to block bad data.
You got /3 concepts.