What if your database could instantly react every time data changes, without you lifting a finger?
Why AFTER UPDATE triggers in MySQL? - Purpose & Use Cases
Imagine you have a big spreadsheet tracking sales. Every time a sale amount changes, you need to update a summary sheet and send a notification email manually.
Doing this by hand is slow and easy to forget. You might miss updating the summary or delay sending notifications, causing mistakes and unhappy customers.
AFTER UPDATE triggers automatically run code right after a record changes. This means updates to summaries or notifications happen instantly and without mistakes.
Update sales record; Then manually update summary; Then send email
CREATE TRIGGER after_update_sales AFTER UPDATE ON sales FOR EACH ROW BEGIN UPDATE summary SET total = total + NEW.amount - OLD.amount; CALL send_notification(NEW.id); END;
It enables automatic reactions to data changes, keeping everything in sync without extra work.
When a customer's order status changes, an AFTER UPDATE trigger can automatically update inventory and notify the shipping team.
Manual updates are slow and error-prone.
AFTER UPDATE triggers run code automatically after data changes.
This keeps data consistent and actions timely without manual effort.
Practice
AFTER UPDATE trigger in MySQL?Solution
Step 1: Understand trigger timing
AnAFTER UPDATEtrigger runs after a row in a table is updated.Step 2: Identify trigger purpose
Its purpose is to perform extra actions automatically after the update, like logging or updating related data.Final Answer:
To execute actions automatically after a row is updated in a table -> Option AQuick Check:
AFTER UPDATE triggers run after update [OK]
- Confusing AFTER UPDATE with BEFORE UPDATE
- Thinking triggers prevent updates
- Mixing trigger types like INSERT or DELETE
AFTER UPDATE trigger on a table named employees?Solution
Step 1: Check trigger timing and event
The trigger must be AFTER UPDATE on theemployeestable.Step 2: Verify syntax correctness
CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW BEGIN END; uses correct syntax:CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW BEGIN END;Final Answer:
CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW BEGIN END; -> Option BQuick Check:
Correct syntax includes 'AFTER UPDATE ON table' [OK]
- Omitting ON keyword before table name
- Using BEFORE instead of AFTER
- Using wrong event like INSERT
products:
CREATE TRIGGER trg_after_update AFTER UPDATE ON products FOR EACH ROW BEGIN INSERT INTO audit_log (product_id, old_price, new_price) VALUES (OLD.id, OLD.price, NEW.price); END;What happens when a product's price is updated?
Solution
Step 1: Understand trigger action
The trigger inserts a row intoaudit_logafter a product update.Step 2: Analyze inserted values
It recordsOLD.id,OLD.price, andNEW.priceto log price changes.Final Answer:
A new row is added to audit_log with old and new prices -> Option DQuick Check:
AFTER UPDATE triggers can log changes [OK]
- Thinking trigger changes original update
- Confusing OLD and NEW values
- Assuming trigger blocks update
CREATE TRIGGER trg AFTER UPDATE ON orders FOR EACH ROW BEGIN SET NEW.status = 'processed'; END;What is the problem?
Solution
Step 1: Understand trigger timing restrictions
In AFTER UPDATE triggers, NEW values are read-only and cannot be changed.Step 2: Identify correct timing to modify NEW
To modify NEW values, use BEFORE UPDATE triggers instead.Final Answer:
You cannot modify NEW values in an AFTER UPDATE trigger -> Option CQuick Check:
AFTER UPDATE triggers cannot change NEW row [OK]
- Trying to change NEW in AFTER UPDATE triggers
- Confusing BEFORE and AFTER triggers
- Incorrect SET syntax blamed
users. Which trigger code correctly updates a email_update_count column after each email change?Solution
Step 1: Identify when to modify the count
The count should increase only if the email changes, so check ifOLD.email != NEW.email.Step 2: Choose correct trigger timing to modify NEW
To changeemail_update_countin the same row, use a BEFORE UPDATE trigger and setNEW.email_update_count.Step 3: Analyze options
CREATE TRIGGER trg_email_update BEFORE UPDATE ON users FOR EACH ROW BEGIN IF OLD.email != NEW.email THEN SET NEW.email_update_count = OLD.email_update_count + 1; END IF; END;correctly uses BEFORE UPDATE, compares emails, and sets NEW value.CREATE TRIGGER trg_email_update AFTER UPDATE ON users FOR EACH ROW BEGIN IF OLD.email != NEW.email THEN UPDATE users SET email_update_count = email_update_count + 1 WHERE id = NEW.id; END IF; END;tries to update the same table in AFTER UPDATE, causing recursion.CREATE TRIGGER trg_email_update AFTER UPDATE ON users FOR EACH ROW BEGIN SET NEW.email_update_count = OLD.email_update_count + 1; END;
tries to set NEW in AFTER UPDATE (not allowed).CREATE TRIGGER trg_email_update AFTER UPDATE ON users FOR EACH ROW BEGIN IF OLD.email = NEW.email THEN UPDATE users SET email_update_count = email_update_count + 1 WHERE id = NEW.id; END IF; END;updates count when emails are equal (wrong logic).Final Answer:
BEFORE UPDATE trigger that sets NEW.email_update_count if OLD.email != NEW.email -> Option AQuick Check:
Modify NEW in BEFORE UPDATE triggers [OK]
- Trying to modify NEW in AFTER UPDATE triggers
- Updating the same table inside AFTER UPDATE causing recursion
- Wrong condition logic for email change
Ready to prove it?
Pass the test and this pattern moves unknown
