A SQL TRIGGER is a named database object bound to a table or view that fires automatically when its triggering event happens. Events include data-change operations (INSERT, UPDATE, DELETE) as well as, in some dialects, DDL or logon actions. The trigger executes the statements defined in its body without explicit invocation from client code, enabling automated validation, auditing, cascading changes, and enforcement of complex business rules.Timing determines when the trigger fires: BEFORE triggers run prior to the row alteration, allowing modification of NEW/OLD records or aborting the operation; AFTER triggers execute only when the triggering statement completes successfully, ideal for logging or interaction with other tables. INSTEAD OF triggers (view-only in most systems) replace the triggering action entirely.Triggers can be row-level (fired once per affected row) or statement-level (fired once per SQL statement), and may include conditional logic via WHEN clauses. Because triggers run implicitly, they add hidden complexity: poorly written triggers can introduce performance bottlenecks, recursion, and debugging challenges. Always document trigger behavior, test for side effects, and keep logic idempotent.Standard SQL first specified triggers in SQL:1999, but each vendor adds proprietary syntax, limitations, and security rules (e.g., MySQL only allows one BEFORE and one AFTER trigger per table per event).
trigger_name STRING
- Identifier for the triggerBEFORE
(AFTER) - INSTEAD OF KEYWORD|||Timing relative to the triggering eventINSERT
(UPDATE) - DELETE KEYWORD|||Event that causes the trigger to firetable_name IDENTIFIER
- Table or view the trigger is attached toFOR EACH ROW
(STATEMENT KEYWORD) - Granularity at which the trigger firesWHEN (condition) EXPRESSION
- Optional Boolean expression that must evaluate to TRUE for the trigger to executefunction_name(argument_list) IDENTIFIER
- Stored procedure or inline block that runs when triggeredSQL:1999
INSERT, UPDATE, DELETE are universal. Some systems add TRUNCATE, DDL, LOGIN, or custom events.
Yes in PostgreSQL, SQL Server, and Oracle. MySQL allows only one BEFORE and one AFTER trigger per table per event.
Use RAISE NOTICE/PRINT statements, write to log tables, or attach a debugger if the database supports it. Always test in a staging environment.
Yes. Triggers were formally added in SQL:1999, but each vendor extends the syntax and feature set.