What is a Trigger?

A trigger is a special kind of stored operation that is automatically executed in response to specific changes (like INSERT, UPDATE, or DELETE) on a table.

Triggers help enforce rules, log changes, validate data, and perform automatic updates without needing manual action from a user or application.

Types of Triggers:

In MySQL, triggers are defined based on two factors:

  • Timing: When it runs (BEFORE or AFTER)
  • Event: What triggers it (INSERT, UPDATE, DELETE)

So, there are six types of triggers:

  1. BEFORE INSERT
  2. AFTER INSERT
  3. BEFORE UPDATE
  4. AFTER UPDATE
  5. BEFORE DELETE
  6. AFTER DELETE

Limitations of Triggers:

  • A table can have only one trigger per timing and event (e.g., only one BEFORE INSERT).
  • Triggers can’t call other triggers or start/commit transactions.
  • No nested triggers (one trigger can’t fire another).

Too many triggers can affect performance.