Recall & Review
beginner
What is the purpose of the OLD and NEW keywords in an UPDATE trigger?
OLD refers to the row data before the update, and NEW refers to the row data after the update. They help compare or use values during the update operation.
Click to reveal answer
intermediate
In an UPDATE trigger, can you modify the NEW values? Why or why not?
Yes, you can modify NEW values in a BEFORE UPDATE trigger to change what will be saved. In AFTER UPDATE triggers, NEW is read-only.
Click to reveal answer
intermediate
Write a simple SQL UPDATE trigger that logs old and new values of a column named 'price'.
CREATE TRIGGER log_price_update BEFORE UPDATE ON products FOR EACH ROW BEGIN INSERT INTO price_log (old_price, new_price) VALUES (OLD.price, NEW.price); END;
Click to reveal answer
beginner
What happens if you try to access OLD in an INSERT trigger?
OLD is not available in INSERT triggers because there is no previous row before insertion.
Click to reveal answer
beginner
Why are UPDATE triggers useful in real-life applications?
They help track changes, enforce rules, or update related data automatically when a row changes, like auditing price changes or updating timestamps.
Click to reveal answer
In an UPDATE trigger, what does NEW represent?
✗ Incorrect
NEW holds the new values that will be saved after the update.
Can you use OLD in an INSERT trigger?
✗ Incorrect
OLD is not available in INSERT triggers because there is no previous row.
Which trigger timing allows you to modify NEW values before they are saved?
✗ Incorrect
BEFORE UPDATE triggers let you change NEW values before saving.
What is a common use of UPDATE triggers with OLD and NEW?
✗ Incorrect
UPDATE triggers often log old and new values to track changes.
If you want to prevent a price from being set below zero during an update, where would you check NEW.price?
✗ Incorrect
BEFORE UPDATE triggers let you validate or change NEW values before saving.
Explain how OLD and NEW work in an UPDATE trigger and give a simple example of their use.
Think about what data you have before and after the update.
You got /4 concepts.
Describe a real-life scenario where an UPDATE trigger using OLD and NEW would be helpful.
Imagine you want to keep a history of changes automatically.
You got /4 concepts.