Trigger Best Practices

Pitfalls

  • Hidden logic: developers reading application code do not see triggers, so behaviour can surprise them.
  • Performance: a trigger runs inside every affected write; a slow trigger slows every insert.
  • Cascades: triggers that fire other triggers are hard to reason about and can loop.
  • Bulk loads: some tools bypass triggers (e.g. TRUNCATE, some bulk import modes).

Guidelines

  1. Prefer constraints (CHECK, FOREIGN KEY) when they can express the rule.
  2. Keep triggers short and set-based; never call external services from them.
  3. Name them clearly (trg_table_event_purpose) and document them.
  4. Use them for cross-cutting concerns (auditing, timestamps), not core business workflows.

Drop a Trigger

-- MySQL, SQLite, SQL Server, Oracle
DROP TRIGGER IF EXISTS trg_salary_audit;

-- PostgreSQL also needs the table name
DROP TRIGGER IF EXISTS trg_salary_audit ON Employees;