CREATE TRIGGER
This feature is experimental and must be enabled before use.
Syntax
Description
A trigger defines a set of SQL statements that run automatically when a specified data modification event occurs on a table or view. Triggers execute within the same transaction as the statement that fired them — if the transaction is rolled back, the trigger’s effects are also rolled back.Parameters
| Parameter | Description |
|---|---|
TEMPORARY | Creates the trigger in a temporary database. The trigger is visible only to the current connection and is dropped when the connection closes. TEMP is accepted as a synonym. |
IF NOT EXISTS | Prevents an error if a trigger with the same name already exists. |
schema-name | The name of the attached database containing the trigger. Defaults to the main database if omitted. Cannot be used with TEMPORARY. |
trigger-name | A unique name for the trigger within the database. |
BEFORE / AFTER / INSTEAD OF | When the trigger fires relative to the triggering event. Default is BEFORE. |
INSERT / UPDATE / DELETE | The data modification event that fires the trigger. |
OF column-name, ... | For UPDATE triggers only. Restricts the trigger to fire only when the specified columns are modified. |
table-name | The table (or view, for INSTEAD OF triggers) that the trigger monitors. |
FOR EACH ROW | The trigger fires once per modified row. This is the only supported mode and is the default even if omitted. |
WHEN expression | An optional condition. The trigger body executes only for rows where the expression evaluates to true. |
Trigger Timing
BEFORE Triggers
ABEFORE trigger fires before the triggering statement modifies the row. Use BEFORE triggers to validate or transform data before it is written.
- The
NEWrow reference contains the values that are about to be written. For INSERT triggers,NEWis the row being inserted. For UPDATE triggers,NEWcontains the updated values. - The
OLDrow reference is available in UPDATE and DELETE triggers and contains the current values before modification. - If a BEFORE trigger raises an error, the triggering operation is aborted for that row.
AFTER Triggers
AnAFTER trigger fires after the triggering statement has modified the row. Use AFTER triggers for logging, auditing, or cascading changes to other tables.
- The
NEWandOLDrow references are available with the same semantics as BEFORE triggers. - The row has already been written when the trigger body executes.
INSTEAD OF Triggers
AnINSTEAD OF trigger can only be created on a view. It fires in place of the triggering INSERT, UPDATE, or DELETE, allowing you to make views writable.
- The actual INSERT, UPDATE, or DELETE on the view does not execute. The trigger body is responsible for performing the desired changes on the underlying tables.
Row References
Inside a trigger body,NEW and OLD are special row references that provide access to column values.
| Reference | INSERT | UPDATE | DELETE |
|---|---|---|---|
NEW.column | Value being inserted | Updated value | Not available |
OLD.column | Not available | Value before update | Value being deleted |
WHEN Clause
The optionalWHEN clause filters which rows cause the trigger body to execute. The expression can reference NEW and OLD columns.
UPDATE OF Columns
For UPDATE triggers, you can restrict the trigger to fire only when specific columns are modified. Without theOF clause, the trigger fires on any UPDATE to the table.
RAISE Function
TheRAISE function is used inside trigger bodies (and other contexts) to interrupt execution and signal an error. It takes one of four forms:
| Form | Behavior |
|---|---|
RAISE(IGNORE) | Skips the remainder of the trigger body and the triggering statement for the current row. Processing continues with the next row. |
RAISE(ABORT, message) | Aborts the current statement and rolls back any changes made by that statement, but preserves prior changes in the transaction. This is the default error handling behavior. |
RAISE(ROLLBACK, message) | Aborts the current statement and rolls back the entire transaction. |
RAISE(FAIL, message) | Aborts the current statement. Changes already made by the statement (previous rows) are preserved, but the current row and subsequent rows are not processed. |
Multiple Statements
A trigger body can contain multiple SQL statements separated by semicolons. The statements execute in order within the same transaction.Examples
Audit Logging Trigger
Track all changes to a table with an audit log.Validation Trigger
Enforce business rules before data is written.Cascading Update Trigger
Propagate changes to related tables.See Also
- DROP TRIGGER for removing triggers
- CREATE VIEW for views that can use INSTEAD OF triggers
- INSERT, UPDATE, DELETE for the statements that fire triggers