📖 Scenario: You manage a small library database. You want to keep track of every time a book's price changes. To do this, you will create a trigger that records the old price and the new price whenever an update happens.
🎯 Goal: Build an UPDATE trigger on the books table that uses OLD and NEW to log price changes into a price_changes table.
📋 What You'll Learn
Create a books table with columns id, title, and price.
Create a price_changes table with columns book_id, old_price, and new_price.
Create an UPDATE trigger on books that inserts a record into price_changes whenever the price changes.
Use OLD.price and NEW.price inside the trigger to get the old and new prices.
💡 Why This Matters
🌍 Real World
Tracking changes in data is important for audit logs, history, and debugging in real-world databases.
💼 Career
Database developers and administrators often write triggers to automate tasks and maintain data integrity.
Progress0 / 4 steps
1
Create the books table
Write SQL to create a table called books with columns: id as an integer primary key, title as text, and price as a decimal number.
SQL
Hint
Use CREATE TABLE books and define the columns exactly as specified.
2
Create the price_changes table
Write SQL to create a table called price_changes with columns: book_id as integer, old_price as decimal, and new_price as decimal.
SQL
Hint
Define the price_changes table with the exact columns and types.
3
Start creating the UPDATE trigger
Write SQL to create a trigger named log_price_change that fires AFTER UPDATE on the books table.
SQL
Hint
Use CREATE TRIGGER with AFTER UPDATE ON books and FOR EACH ROW.
4
Complete the trigger to log price changes using OLD and NEW
Inside the trigger body, write SQL to insert into price_changes the book_id as NEW.id, old_price as OLD.price, and new_price as NEW.price. Only insert if the price has changed (compare OLD.price and NEW.price).
SQL
Hint
Use an IF statement to check if prices differ, then insert the old and new prices using OLD.price and NEW.price.
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