0
0
SQLquery~5 mins

UPDATE trigger with OLD and NEW in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe row data before the update
BThe table schema
CThe row data after the update
DThe database user
Can you use OLD in an INSERT trigger?
ANo, OLD is not available in INSERT triggers
BOnly in BEFORE INSERT triggers
COnly if the table has a primary key
DYes, always
Which trigger timing allows you to modify NEW values before they are saved?
ABEFORE UPDATE
BAFTER DELETE
CAFTER UPDATE
DBEFORE DELETE
What is a common use of UPDATE triggers with OLD and NEW?
ACreating new tables
BLogging changes to data
CDeleting rows automatically
DChanging database users
If you want to prevent a price from being set below zero during an update, where would you check NEW.price?
AIn a BEFORE DELETE trigger
BIn an AFTER UPDATE trigger
CIn an AFTER INSERT trigger
DIn a BEFORE UPDATE trigger
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.