0
0
MySQLquery~5 mins

BEFORE UPDATE triggers in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AAfter a row is updated
BAfter a row is deleted
CBefore a row is inserted
DBefore a row is updated
Which keyword lets you access the new values in a BEFORE UPDATE trigger?
AOLD
BNEW
CCURRENT
DNEXT
What happens if a BEFORE UPDATE trigger raises an error?
AThe update is canceled
BThe database restarts
CThe trigger is ignored
DThe update continues anyway
Can a BEFORE UPDATE trigger modify the data before it is saved?
ANo, it can only read data
BOnly if the table is empty
CYes, it can change NEW values
DOnly for numeric columns
Which SQL statement creates a BEFORE UPDATE trigger?
ACREATE TRIGGER ... BEFORE UPDATE
BCREATE PROCEDURE ... BEFORE UPDATE
CCREATE FUNCTION ... BEFORE UPDATE
DCREATE EVENT ... 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.