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
✗ Incorrect
NEW holds the new values that will be saved after the update.
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
✗ 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?
ABEFORE UPDATE
BAFTER DELETE
CAFTER UPDATE
DBEFORE DELETE
✗ Incorrect
BEFORE UPDATE triggers let you change NEW values before saving.
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
✗ 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?
AIn a BEFORE DELETE trigger
BIn an AFTER UPDATE trigger
CIn an AFTER INSERT trigger
DIn a BEFORE UPDATE trigger
✗ 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.
Practice
(1/5)
1. What do the OLD and NEW keywords represent in an UPDATE trigger?
easy
A. OLD and NEW both represent the data before update
B. OLD is the data after update; NEW is the data before update
C. OLD and NEW both represent the data after update
D. OLD is the data before update; NEW is the data after update
Solution
Step 1: Understand trigger timing
In an UPDATE trigger, OLD holds the row data before the update happens.
Step 2: Understand data after update
NEW holds the row data after the update is applied.
Final Answer:
OLD is the data before update; NEW is the data after update -> Option D
Quick Check:
OLD = before, NEW = after [OK]
Hint: OLD = before update, NEW = after update [OK]
Common Mistakes:
Confusing OLD and NEW roles
Thinking both OLD and NEW hold same data
Assuming OLD is after update
2. Which of the following is the correct syntax to access the new value of a column named salary inside an UPDATE trigger?
easy
A. NEW.salary
B. OLD.salary
C. SELECT salary FROM NEW;
D. UPDATE salary SET NEW;
Solution
Step 1: Identify how to reference NEW values in triggers
Inside an UPDATE trigger, you access new column values using NEW.column_name.
Step 2: Match syntax for column salary
So, the correct way is NEW.salary.
Final Answer:
NEW.salary -> Option A
Quick Check:
Use NEW.column_name to get updated value [OK]
Hint: Use NEW.column_name to get updated column value [OK]
Common Mistakes:
Using OLD.salary to get new value
Trying to SELECT from NEW as a table
Incorrect UPDATE syntax inside trigger
3. Given this trigger code snippet inside an UPDATE trigger:
IF NEW.status = 'active' AND OLD.status = 'inactive' THEN
INSERT INTO log_table(action) VALUES('Activated');
END IF;
What happens when a row's status changes from 'inactive' to 'active'?
medium
A. No action is taken because OLD and NEW are the same
B. A new row with action 'Activated' is inserted into log_table
C. An error occurs because you cannot compare OLD and NEW
D. The row update is canceled automatically
Solution
Step 1: Understand the condition in the trigger
The condition checks if the new status is 'active' and the old status was 'inactive'.
Step 2: Effect of condition being true
If true, it inserts a row into log_table with action 'Activated'.
Final Answer:
A new row with action 'Activated' is inserted into log_table -> Option B
Quick Check:
Condition true triggers insert [OK]
Hint: Trigger runs code when OLD and NEW differ as condition says [OK]
Common Mistakes:
Thinking OLD and NEW cannot be compared
Assuming no insert happens on status change
Believing update cancels automatically
4. You wrote this trigger code:
CREATE TRIGGER trg_update BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < OLD.salary THEN
SET NEW.salary = OLD.salary;
END IF;
END;
But when you try to update salary to a lower value, it still changes. What is the problem?
medium
A. The trigger syntax is missing a delimiter or terminator
B. You cannot assign values to NEW in a BEFORE UPDATE trigger
C. The trigger should be AFTER UPDATE, not BEFORE UPDATE
D. The trigger logic is correct; the issue is elsewhere
Solution
Step 1: Check trigger syntax completeness
In many SQL systems, triggers require a delimiter change to define the body properly.
Step 2: Missing delimiter causes trigger body to be incomplete
Without proper delimiters, the trigger may not compile or behave as expected.
Final Answer:
The trigger syntax is missing a delimiter or terminator -> Option A
Quick Check:
Triggers need delimiters to parse body correctly [OK]
Hint: Check delimiters when trigger body has multiple statements [OK]
Common Mistakes:
Assuming AFTER triggers can modify NEW
Thinking NEW cannot be assigned in BEFORE triggers
Ignoring delimiter requirements in SQL triggers
5. You want to create an UPDATE trigger that prevents lowering a product's price. If the new price is less than the old price, the trigger should reset the price to the old value and log the attempt. Which trigger code correctly implements this?
hard
A.
CREATE TRIGGER trg_price_check BEFORE UPDATE ON products
FOR EACH ROW
BEGIN
IF OLD.price < NEW.price THEN
SET NEW.price = OLD.price;
INSERT INTO price_log(product_id, old_price, attempted_price) VALUES (OLD.id, OLD.price, NEW.price);
END IF;
END;
B.
CREATE TRIGGER trg_price_check AFTER UPDATE ON products
FOR EACH ROW
BEGIN
IF NEW.price < OLD.price THEN
UPDATE products SET price = OLD.price WHERE id = OLD.id;
INSERT INTO price_log(product_id, old_price, attempted_price) VALUES (OLD.id, OLD.price, NEW.price);
END IF;
END;
C.
CREATE TRIGGER trg_price_check BEFORE UPDATE ON products
FOR EACH ROW
BEGIN
IF NEW.price < OLD.price THEN
INSERT INTO price_log(product_id, old_price, attempted_price) VALUES (OLD.id, OLD.price, NEW.price);
SET NEW.price = OLD.price;
END IF;
END;
D.
CREATE TRIGGER trg_price_check BEFORE UPDATE ON products
FOR EACH ROW
BEGIN
IF NEW.price > OLD.price THEN
SET NEW.price = OLD.price;
INSERT INTO price_log(product_id, old_price, attempted_price) VALUES (OLD.id, OLD.price, NEW.price);
END IF;
END;
Solution
Step 1: Identify correct trigger timing and condition
Use a BEFORE UPDATE trigger to modify NEW values before update. Condition should check if NEW.price is less than OLD.price.
Step 2: Correctly reset price and log attempt
If condition true, set NEW.price = OLD.price to prevent lowering, then insert a log record with OLD and attempted NEW price.
Final Answer:
BEFORE UPDATE trigger that prevents lowering price and logs the attempt -> Option C