Lesson 53/100

Tutorials MySQL Tutorial

Triggers — Complete Guide

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 53 of 100

Triggers

Basics ✓Advanced

Advanced · 2 — Production · ~10 min · MySQL — Stored Procedures & Triggers

What is this?

Triggers run automatic SQL when INSERT, UPDATE, or DELETE happens on a table. Timing is BEFORE or AFTER the row event; FOR EACH ROW is standard.

Why should you care?

Audit trail on orders — every total change logged without trusting every developer to remember in app code.

See it live — copy this example

Run in MySQL Workbench or the mysql CLI.

CREATE TABLE order_audit (
  audit_id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  order_id INT UNSIGNED NOT NULL,
  old_total DECIMAL(12,2),
  new_total DECIMAL(12,2),
  changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TRIGGER trg_orders_total_audit
AFTER UPDATE ON orders
FOR EACH ROW
BEGIN
  IF OLD.total_inr <> NEW.total_inr THEN
    INSERT INTO order_audit (order_id, old_total, new_total)
    VALUES (NEW.order_id, OLD.total_inr, NEW.total_inr);
  END IF;
END;

What happened?

  • After orders.total_inr updates, trigger compares OLD vs NEW and writes audit row.
  • Apps stay thin; database guarantees log on any path that touches the table.

Practice next

  1. Create order_audit and trigger.
  2. UPDATE orders SET total_inr = total_inr + 10 WHERE order_id = 1;
  3. SELECT * FROM order_audit;
  4. Add INSERT trigger logging new orders.
  5. DROP TRIGGER trg_orders_total_audit when refactoring to app audit.

Remember

Triggers react to DML automatically. OLD/NEW hold row before/after images. Use for audit and derived enforcement.

DataFlow compliance log

RBI audit asks who changed loan order amounts — order_audit answers.

Outcome: No manual logging code in twelve microservices.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain SQL queries in the context of MySQL.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using MySQL?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a MySQL application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a MySQL project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

MySQL Tutorial
Course syllabus

MySQL Tutorial

MySQL — Foundations
MySQL — Queries & Clauses
MySQL — Joins & Relationships
MySQL — Functions & Window Functions
MySQL — Transactions & Concurrency
MySQL — Stored Procedures & Triggers
MySQL — Indexing & Performance
MySQL — Advanced MySQL
MySQL — Security & Cloud MySQL
MySQL — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details