CREATE TRIGGER
Go Up to Statement and Function Reference (Language Reference Guide)
Creates a trigger, including when it fires, and what actions it performs. Available in DSQL, and isql
.
CREATE TRIGGER name FOR table
[ACTIVE | INACTIVE]
{BEFORE | AFTER}
{DELETE | INSERT | UPDATE}
[POSITION number]
AS trigger_body ;
trigger_body = [variable_declaration_list] block
variable_declaration_list =
DECLARE VARIABLE variable data_type;
[DECLARE VARIABLE variable data_type; …]
block =
BEGIN
compound_statement
[compound_statement …]
END
data_type = SMALLINT
| INTEGER
| FLOAT
| DOUBLE PRECISION
| {DECIMAL | NUMERIC} [(precision [, scale])]
| {DATE | TIME | TIMESTAMP)
| {CHAR | CHARACTER | CHARACTER VARYING | VARCHAR}
[(int)] [CHARACTER SET charname]
| {NCHAR | NATIONAL CHARACTER | NATIONAL CHAR} [VARYING] [(int)]
| BOOLEAN
compound_statement = block | statement;
Argument | Description |
---|---|
<name> |
Name of the trigger; must be unique in the database. |
<table> |
Name of the table or view that causes the trigger to fire when the specified operation occurs on the table or view. |
|
Optional. Specifies trigger action at transaction end:
|
|
Required. Specifies whether the trigger fires:
Associated operations are |
|
Specifies the table operation that causes the trigger to fire. |
|
Specifies the firing order for triggers before the same action or after the same action; <number> must be an integer between 0 and 32,767, inclusive.
|
|
Declares local variables used only in the trigger. Each declaration must be preceded by
|
<statement> |
Any single statement in InterBase procedure and trigger language; each statement except |
Description: CREATE TRIGGER
defines a new trigger to a database. A trigger is a self-contained program associated with a table or view that automatically performs an action when a row in the table or view is inserted, updated, or deleted.
A trigger is never called directly. Instead, when an application or user attempts to INSERT
, UPDATE
, or DELETE
a row in a table, any triggers associated with that table and operation automatically execute, or fire. Triggers defined for UPDATE
on non-updatable views fire even if no update occurs.
A trigger is composed of a header and a body.
The trigger header contains:
- A trigger name, unique within the database, that distinguishes the trigger from all others.
- A table name, identifying the table with which to associate the trigger.
- Statements that determine when the trigger fires.
The trigger body contains:
- An optional list of local variables and their data types.
- A block of statements in InterBase procedure and trigger language, bracketed by
BEGIN
andEND
. These statements are performed when the trigger fires. A block can itself include other blocks, so that there may be many levels of nesting.
A trigger is associated with a table. The table owner and any user granted privileges to the table automatically have rights to execute associated triggers.
Triggers can be granted privileges on tables, just as users or procedures can be granted privileges. Use the GRANT
statement, but instead of using TO
<username>, use TO
TRIGGER
<trigger_nam>e. Triggers privileges can be revoked similarly using REVOKE
.
When a user performs an action that fires a trigger, the trigger will have privileges to perform its actions if one of the following conditions is true:
- The trigger has privileges for the action.
- The user has privileges for the action.
InterBase procedure and trigger language is a complete programming language for stored procedures and triggers. It includes:
- SQL data manipulation statements:
INSERT
,UPDATE
,DELETE
, and singletonSELECT
. - SQL operators and expressions, including generators and UDFs that are linked with the calling application.
- Powerful extensions to SQL, including assignment statements, control-flow statements, context variables, event-posting statements, exceptions, and error-handling statements.
The following table summarizes language extensions for triggers. For a complete description of each statement, see Procedures and Triggers.
Statement | Description |
---|---|
|
Defines a block of statements that executes as one.
|
<variable> = <expression> |
Assignment statement that assigns the value of <expression> to <variable>, a local variable, input parameter, or output parameter. |
/* <comment_text> */ |
Programmer’s comment, where <comment_text> can be any number of lines of text. |
|
Raises the named exception; an exception is a user-defined error that returns an error message to the calling application unless handled by a |
|
Executes the stored procedure, <proc_name>, with the listed input arguments.
|
|
Jumps to the final |
|
Repeats the statement or block following |
<select_statement> |
A normal |
<compound_statement> |
Either a single statement in procedure and trigger language or a block of statements bracketed by |
|
Tests <condition>, and if it is |
<condition> |
A Boolean expression ( |
|
New context variable that indicates a new column value in an |
|
Old context variable that indicates a column value before an |
|
Posts the event, <event_name>, or uses the value in <col> as an event name. |
|
While condition is
|
|
Error-handling statement. When one of the specified errors occurs, performs <compound_statement>.
|
<error> |
|
The stored procedure and trigger language does not include many of the statement types available in DSQL or gpre
. The following statement types are not supported in triggers or stored procedures:
- Data definition language statements:
CREATE
,ALTER
,DROP
,DECLARE
EXTERNAL FUNCTION
, andDECLARE FILTER
- Transaction control statements:
SET TRANSACTION
,COMMIT
,ROLLBACK
- Dynamic SQL statements:
PREPARE
,DESCRIBE
,EXECUTE
CONNECT/DISCONNECT
, and sending SQL statements to another databaseGRANT/REVOKE
SET GENERATOR
EVENT INIT/WAIT
BEGIN/END DECLARE SECTION
BASED ON
WHENEVER
DECLARE CURSOR
OPEN
FETCH
Examples: The following trigger, SAVE_SALARY_CHANGE
, makes correlated updates to the SALARY_HISTORY
table when a change is made to an employee’s salary in the EMPLOYEE
table:
CREATE TRIGGER SAVE_SALARY_CHANGE FOR EMPLOYEE
AFTER UPDATE AS
BEGIN
IF (OLD.SALARY <> NEW.SALARY) THEN
INSERT INTO SALARY_HISTORY
(EMP_NO, CHANGE_DATE, UPDATER_ID, OLD_SALARY, PERCENT_CHANGE)
VALUES (OLD.EMP_NO, 'now', USER, OLD.SALARY,
(NEW.SALARY - OLD.SALARY) * 100 / OLD.SALARY);
END ;
The following trigger, SET_CUST_NO
, uses a generator to create unique customer numbers when a new customer record is inserted in the CUSTOMER
table.
CREATE TRIGGER SET_CUST_NO FOR CUSTOMER
BEFORE INSERT AS
BEGIN
NEW.CUST_NO = GEN_ID(CUST_NO_GEN, 1);
END ;
The following trigger, POST_NEW_ORDER
, posts an event named “new_order” whenever a new record is inserted in the SALES
table.
CREATE TRIGGER POST_NEW_ORDER FOR SALES
AFTER INSERT AS
BEGIN
POST_EVENT 'new_order';
END ;
The following four fragments of trigger headers demonstrate how the POSITION
option determines trigger firing order:
CREATE TRIGGER A FOR accounts
BEFORE UPDATE
POSITION 5 … /*Trigger body follows*/
CREATE TRIGGER B FOR accounts
BEFORE UPDATE
POSITION 0 … /*Trigger body follows*/
CREATE TRIGGER C FOR accounts
AFTER UPDATE
POSITION 5 … /*Trigger body follows*/
CREATE TRIGGER D FOR accounts
AFTER UPDATE
POSITION 3 … /*Trigger body follows*/
When this update takes place:
UPDATE accounts SET account_status = 'on_hold'
WHERE account_balance < 0;
The triggers fire in this order:
- Trigger B fires.
- Trigger A fires.
- The update occurs.
- Trigger D fires.
- Trigger C fires.
See Also
- ALTER EXCEPTION
- ALTER TRIGGER
- CREATE EXCEPTION
- CREATE PROCEDURE
- DROP EXCEPTION
- DROP TRIGGER
- EXECUTE PROCEDURE
- Data Definition Guide
- Procedures and Triggers