AFTER Triggers — Complete Guide
AFTER Triggers — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.
On this page
MySQL Tutorial · Lesson 55 of 100
AFTER Triggers
Basics ✓ → Advanced
Advanced · 2 — Production · ~10 min · MySQL — Stored Procedures & Triggers
What is this?
AFTER triggers run once the row change succeeded. Use them for side effects: audit inserts, queue tables, or summary updates — not to veto the change.
Why should you care?
When order status flips to SHIPPED, warehouse notification row must appear — AFTER INSERT on status_history is reliable.
See it live — copy this example
Run in MySQL Workbench or the mysql CLI.
CREATE TRIGGER trg_orders_shipped_notify
AFTER UPDATE ON orders
FOR EACH ROW
BEGIN
IF NEW.status = 'SHIPPED' AND OLD.status <> 'SHIPPED' THEN
INSERT INTO shipment_queue (order_id, queued_at)
VALUES (NEW.order_id, NOW());
END IF;
END;
What happened?
- Trigger watches status transition to SHIPPED.
- Writes shipment_queue row for downstream worker.
- AFTER timing means order row already committed when queue insert runs (same txn).
Practice next
- ADD status VARCHAR(20) to orders; CREATE shipment_queue.
- CREATE AFTER UPDATE trigger.
- UPDATE status to SHIPPED; check queue.
- Log cancelled orders to cancel_audit on status CANCELLED.
- Combine with BEFORE trigger for validate + notify pattern.
Remember
AFTER = post-change side effects. Compare OLD/NEW for meaningful transitions. Keep trigger body fast.
DataFlow ship webhook
Worker polls shipment_queue populated by AFTER trigger — courier API called.
Outcome: Shipping starts even if Node app forgot hook.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!