informix
Informix Guide to SQL: Syntax
SQL Statements

CREATE TRIGGER

Use the CREATE TRIGGER statement to create a trigger on a table.

Syntax

Element Purpose Restrictions Syntax
table Name of the table that the trigger affects The name must be different from any existing table, view, or synonym name in the current database. Database Object Name, p. 4-50
trigger Name of the trigger You can specify a trigger for the current database only. The name of the trigger must be unique. Database Object Name, p. 4-50

Usage

You can use the CREATE TRIGGER statement to define a trigger on a table. A trigger is a database object that automatically sets off a specified set of SQL statements when a specified event occurs.

You cannot create a trigger on a raw or static table. When you create a trigger on an operational table, the table cannot use light appends. For more information on light appends, see your Administrator's Guide.

Information in this statement that discusses nonlogging databases does not apply to Enterprise Decision Server. In Enterprise Decision Server, all databases are logging databases.

Rules for Triggers

You must be either the owner of the table or have the DBA status to create a trigger on a table.

For information about the relationship between the privileges of the trigger owner and the privileges of other users, see Privileges to Execute Triggered Actions.

You can use roles with triggers. Role-related statements (CREATE ROLE, DROP ROLE, and SET ROLE) and SET SESSION AUTHORIZATION statements can be triggered inside a trigger. Privileges that a user has acquired through enabling a role or through a SET SESSION AUTHORIZATION statement are not relinquished when a trigger is executed.

When you create a trigger, the name of the trigger must be unique within a database.

You can create a trigger only on a table in the current database. You cannot create a trigger on a temporary table, a view, or a system catalog table.

You cannot create a trigger inside an SPL routine if the routine is called inside a data manipulation statement. For example, in the following INSERT statement, if the sp_items procedure contains a trigger, the database server returns an error:

For a list of data manipulation statements, see Data Manipulation Statements.

You cannot use an SPL variable in a CREATE TRIGGER statement.

In DB-Access, if you want to define a trigger as part of a schema, place the CREATE TRIGGER statement inside a CREATE SCHEMA statement.

If you are embedding the CREATE TRIGGER statement in an ESQL/C program, you cannot use a host variable in the trigger specification.

Trigger Events

The trigger event specifies the type of statement that activates a trigger. The trigger event can be an INSERT, DELETE, UPDATE, or SELECT statement. Each trigger can have only one trigger event. The occurrence of the trigger event is the triggering statement.

For each table, you can define only one trigger that is activated by an INSERT statement and only one trigger that is activated by a DELETE statement. For each table, you can define multiple triggers that are activated by UPDATE statements or SELECT statements. For more information about multiple update or select triggers on the same table, see UPDATE Clause and SELECT Clause.

You cannot define a DELETE trigger event on a table with a referential constraint that specifies ON DELETE CASCADE.

You are responsible for guaranteeing that the triggering statement returns the same result with and without the triggered actions. For more information on the behavior of triggered actions, see Action Clause and Triggered Action List.

A triggering statement from an external database server can activate the trigger. As shown in the following example, an insert trigger on newtab, managed by dbserver1, is activated by an INSERT statement from dbserver2. The trigger executes as if the insert originated on dbserver1.

Trigger Events with Cursors

If the triggering statement uses a cursor, the complete trigger is activated each time the statement executes. That is, each part of the trigger (BEFORE, FOR EACH ROW, and AFTER) is activated for each row that the cursor processes.

This behavior is different from what occurs when a triggering statement does not use a cursor and updates multiple rows. In this case, the set of triggered actions executes only once. For more information on the execution of triggered actions, see Action Clause.

Privileges on the Trigger Event

You must have the appropriate Insert, Delete, Update, or Select privilege on the triggering table to execute the INSERT, DELETE, UPDATE, or SELECT statement that is the trigger event. The triggering statement might still fail, however, if you do not have the privileges necessary to execute one of the SQL statements in the action clause. When the triggered actions are executed, the database server checks your privileges for each SQL statement in the trigger definition as if the statement were being executed independently of the trigger. For information on the privileges you need to execute a trigger, see Privileges to Execute Triggered Actions.

Performance Impact of Triggers

The INSERT, DELETE, UPDATE, and SELECT statements that initiate triggers might appear to execute slowly because they activate additional SQL statements, and the user might not know that other actions are occurring.

The execution time for a triggering data manipulation statement depends on the complexity of the triggered action and whether it initiates other triggers. Obviously, the elapsed time for the triggering data manipulation statement increases as the number of cascading triggers increases. For more information on triggers that initiate other triggers, see Cascading Triggers.

UPDATE Clause

Element Purpose Restrictions Syntax
column Name of a column or columns that activate the trigger The default is all the columns in the table on which you create the trigger. The specified columns must belong to the table on which you create the trigger. If you define more than one update trigger on a table, the column lists of the triggering statements must be mutually exclusive. Identifier, p. 4-205

If the trigger event is an UPDATE statement, the trigger executes when any column in the triggering column list is updated.

If the trigger event is an UPDATE statement and you do not specify the OF column option in the definition of the trigger event, the trigger executes when any column in the triggering table is updated.

If the triggering UPDATE statement updates more than one of the triggering columns in a trigger, the trigger executes only once.

Defining Multiple Update Triggers

If you define more than one update trigger event on a table, the column lists of the triggers must be mutually exclusive. The following example shows that trig3 is illegal on the items table because its column list includes stock_num, which is a triggering column in trig1. Multiple update triggers on a table cannot include the same columns.

When an UPDATE Statement Activates Multiple Triggers

When an UPDATE statement updates multiple columns that have different triggers, the column numbers of the triggering columns determine the order of trigger execution. Execution begins with the smallest triggering column number and proceeds in order to the largest triggering column number. The following example shows that table taba has four columns (a, b, c, d):

Define trig1 as an update on columns a and c, and define trig2 as an update on columns b and d, as shown in the following example:

The triggering statement is shown in the following example:

Then trig1 for columns a and c executes first, and trig2 for columns b and d executes next. In this case, the smallest column number in the two triggers is column 1 (a), and the next is column 2 (b).

SELECT Clause

Element Purpose Restrictions Syntax
column Name of a column or columns that activate the trigger The default is all the columns in the table on which you create the trigger. The specified columns must belong to the table on which you create the trigger. If you define more than one select trigger on a table, the column lists of the triggering statements must be mutually exclusive. Identifier, p. 4-205

If the trigger event is a SELECT statement, the trigger executes when any column in the triggering column list is selected.

If the trigger event is a SELECT statement and you do not specify the OF column option in the definition of the trigger event, the trigger executes when any column in the triggering table is selected.

If the triggering SELECT statement selects more than one of the triggering columns in a trigger, the trigger executes only once.

The triggered action of a select trigger cannot include UPDATE, INSERT, or DELETE actions on the triggering table. However, the triggered action of a select trigger can include UPDATE, INSERT, and DELETE actions on tables other than the triggering table.

The following example shows how a select trigger is defined within a CREATE TRIGGER statement:

Circumstances When a Select Trigger is Activated

A SELECT statement on the triggering table activates a select trigger in the following circumstances:

For information on the conditions when a SELECT statement on the triggering table does not activate a select trigger, see Circumstances When a Select Trigger is Not Activated.

Standalone SELECT Statements

A select trigger is activated if the triggering column appears in the select list of a standalone SELECT statement. For example, assume that a select trigger was defined such that it will execute whenever column col1 of table tab1 is selected. Both of the following standalone SELECT statements will activate the select trigger.

SELECT Statements within UDRs in the Select List

A select trigger is activated by a UDR if the UDR contains a SELECT statement within its statement block and if the UDR appears in the select list of a SELECT statement. For example, assume that a UDR named my_rtn contains the following SELECT statement in its statement block:

Now suppose that the following SELECT statement invokes the my_rtn UDR in its select list:

This SELECT statement activates the select trigger defined on column col1 of table tab1 when the my_rtn UDR is executed.

UDRs Called by EXECUTE PROCEDURE and EXECUTE FUNCTION

A select trigger is activated by a UDR if the UDR contains a SELECT statement within its statement block and the UDR is called by an EXECUTE PROCEDURE or EXECUTE FUNCTION statement. For example, assume that the user-defined procedure named my_rtn contains the following SELECT statement in its statement block:

Now suppose that the following EXECUTE PROCEDURE statement invokes the my_rtn procedure:

This EXECUTE PROCEDURE statement activates the select trigger defined on column col1 of table tab1 when the SELECT statement within the statement block is executed.

Subqueries in the Select List

A select trigger is activated by a subquery if the subquery appears in the select list of a SELECT statement. For example, if a select trigger was defined on col1 of tab1, the subquery in the following SELECT statement activates the select trigger:

Select Triggers in Table Hierarchies

A subtable inherits the select triggers that are defined on its supertable. When you select from a supertable, the SELECT statement activates the select triggers on the supertable and the inherited select triggers on the subtables in the table hierarchy. For example, assume that table tab1 is the supertable and table tab2 is the subtable in a table hierarchy. If the select trigger trig1 is defined on table tab1, a SELECT statement on table tab1 activates the select trigger trig1 for the rows in table tab1 and the inherited select trigger trig1 for the rows in table tab2.

If you add a select trigger to a subtable, this select trigger can override the select trigger that the subtable inherits from its supertable. For example, if the select trigger trig1 is defined on column col1 in supertable tab1, the subtable tab2 inherits this trigger. But if you define a select trigger named trig2 on column col1 in subtable tab2, and a SELECT statement selects from col1 in supertable tab1, this SELECT statement activates trigger trig1 for the rows in table tab1 and trigger trig2 (not trigger trig1) for the rows in table tab2. In other words, the trigger that you add to the subtable overrides the trigger that the subtable inherits from the supertable.

Circumstances When a Select Trigger is Not Activated

A SELECT statement on the triggering table does not activate a select trigger in certain circumstances:

Select Triggers and FOR EACH ROW Actions

If the triggered action of a select trigger is a FOR EACH ROW action, and a row appears more than once in the result of the triggering SELECT statement, the database server executes the FOR EACH ROW action for each instance of the row. For example, the same row can appear more than once in the result of a SELECT statement that joins two tables. For more information on FOR EACH ROW actions, see FOR EACH ROW Actions.

Action Clause

The action clause defines the characteristics of triggered actions and specifies the time when these actions occur. You must define at least one triggered action, using the keywords BEFORE, FOR EACH ROW, or AFTER to indicate when the action occurs relative to the triggering statement. You can specify triggered actions for all three options on a single trigger, but you must order them in the following sequence: BEFORE, FOR EACH ROW, and AFTER. You cannot follow a FOR EACH ROW triggered action list with a BEFORE triggered action list. If the first triggered action list is FOR EACH ROW, an AFTER action list is the only option that can follow it. For more information on the action clause when a REFERENCING clause is present, see Action Clause Referencing.

BEFORE Actions

The BEFORE triggered action or actions execute once before the triggering statement executes. If the triggering statement does not process any rows, the BEFORE triggered actions still execute because the database server does not yet know whether any row is affected.

FOR EACH ROW Actions

The FOR EACH ROW triggered action or actions execute once for each row that the triggering statement affects. The triggered SQL statement executes after the triggering statement processes each row.

If the triggering statement does not insert, delete, update, or select any rows, the FOR EACH ROW triggered actions do not execute.

You cannot have FOR EACH ROW actions on tables that have globally-detached indexes.

AFTER Actions

An AFTER triggered action or actions execute once after the action of the triggering statement is complete. If the triggering statement does not process any rows, the AFTER triggered actions still execute.

Actions of Multiple Triggers

When an UPDATE statement activates multiple triggers, the triggered actions merge. Assume that taba has columns a, b, c, and d, as shown in the following example:

Next, assume that you define trig1 on columns a and c, and trig2 on columns b and d. If both triggers have triggered actions that are executed BEFORE, FOR EACH ROW, and AFTER, then the triggered actions are executed in the following sequence:

  1. BEFORE action list for trigger (a, c)
  2. BEFORE action list for trigger (b, d)
  3. FOR EACH ROW action list for trigger (a, c)
  4. FOR EACH ROW action list for trigger (b, d)
  5. AFTER action list for trigger (a, c)
  6. AFTER action list for trigger (b, d)

The database server treats the triggers as a single trigger, and the triggered action is the merged-action list. All the rules governing a triggered action apply to the merged list as one list, and no distinction is made between the two original triggers.

Guaranteeing Row-Order Independence

In a FOR EACH ROW triggered-action list, the result might depend on the order of the rows being processed. You can ensure that the result is independent of row order by following these suggestions:

The database server does not enforce rules to prevent these situations because doing so would restrict the set of tables from which a triggered action can select. Furthermore, the result of most triggered actions is independent of row order. Consequently, you are responsible for ensuring that the results of the triggered actions are independent of row order.

REFERENCING Clause for Insert

Element Purpose Restrictions Syntax
correlation Name that you assign to a new column value so that you can refer to it within the triggered action The new column value in the triggering table is the value of the column after execution of the triggering statement. The correlation name must be unique within the CREATE TRIGGER statement. Identifier, p. 4-205

Once you assign a correlation name, you can use it only inside the FOR EACH ROW triggered action. See Action Clause Referencing.

To use the correlation name, precede the column name with the correlation name, followed by a period. For example, if the new correlation name is post, refer to the new value for the column fname as post.fname.

If the trigger event is an INSERT statement, using the old correlation name as a qualifier causes an error because no value exists before the row is inserted. For the rules that govern how to use correlation names, see Using Correlation Names in Triggered Actions.

You can use the INSERT REFERENCING clause only if you define a FOR EACH ROW triggered action.

The following example illustrates the use of the INSERT REFERENCING clause. This example inserts a row into backup_table1 for every row that is inserted into table1. The values that are inserted into col1 and col2 of backup_table1 are an exact copy of the values that were just inserted into table1.

As the preceding example shows, the advantage of the INSERT REFERENCING clause is that it allows you to refer to the data values that the trigger event in your triggered action produces.

REFERENCING Clause for Delete

Element Purpose Restrictions Syntax
correlation Name that you assign to an old column value so that you can refer to it within the triggered action The old column value in the triggering table is the value of the column before execution of the triggering statement. The correlation name must be unique within the CREATE TRIGGER statement. Identifier, p. 4-205

Once you assign a correlation name, you can use it only inside the FOR EACH ROW triggered action. See Action Clause Referencing.

You use the correlation name to refer to an old column value by preceding the column name with the correlation name and a period (.). For example, if the old correlation name is pre, refer to the old value for the column fname as pre.fname.

If the trigger event is a DELETE statement, using the new correlation name as a qualifier causes an error because the column has no value after the row is deleted. For the rules governing the use of correlation names, see Using Correlation Names in Triggered Actions.

You can use the DELETE REFERENCING clause only if you define a FOR EACH ROW triggered action.

The OLD correlation value cannot be a byte or text value. That is, you cannot refer to a byte or text column.

REFERENCING Clause for Update

Element Purpose Restrictions Syntax
correlation Name that you assign to an old or new column value so that you can refer to it within the triggered action The old column value in the triggering table is the value of the column before execution of the triggering statement. The new column value in the triggering table is the value of the column after executing the triggering statement. You can specify a correlation name for an old column value only (OLD option), for a new column value only (NEW option), or for both the old and new column values. Each correlation name you specify must be unique within the CREATE TRIGGER statement. Identifier, p. 4-205

Once you assign a correlation name, you can use it only inside the FOR EACH ROW triggered action. See Action Clause Referencing.

Use the correlation name to refer to an old or new column value by preceding the column name with the correlation name and a period (.). For example, if the new correlation name is post, you refer to the new value for the column fname as post.fname.

If the trigger event is an UPDATE statement, you can define both old and new correlation names to refer to column values before and after the triggering update. For the rules that govern the use of correlation names, see Using Correlation Names in Triggered Actions.

You can use the UPDATE REFERENCING clause only if you define a FOR EACH ROW triggered action.

The OLD correlation value cannot be a byte or text value. That is, you cannot refer to a byte or text column.

REFERENCING CLAUSE FOR Select

Element Purpose Restrictions Syntax
correlation Name that you assign to an old column value so that you can refer to it within the triggered action The correlation name must be unique within the CREATE TRIGGER statement. Identifier, p. 4-205

Once you assign a correlation name, you can use it only inside the FOR EACH ROW triggered action. See Action Clause Referencing.

You use the correlation name to refer to an old column value by preceding the column name with the correlation name and a period (.). For example, if the old correlation name is pre, refer to the old value for the column fname as pre.fname.

If the trigger event is a SELECT statement, using the new correlation name as a qualifier causes an error because the column does not have a new value after the column is selected. For the rules governing the use of correlation names, see Using Correlation Names in Triggered Actions.

You can use the SELECT REFERENCING clause only if you define a FOR EACH ROW triggered action.

The OLD correlation value cannot be a byte or text value. That is, you cannot refer to a byte or text column.

Action Clause Referencing

If the CREATE TRIGGER statement contains an INSERT REFERENCING clause, a DELETE REFERENCING clause, an UPDATE REFERENCING clause, or a SELECT REFERENCING clause, you must include a FOR EACH ROW triggered-action list in the action clause. You can also include BEFORE and AFTER triggered-action lists, but they are optional. For information on the BEFORE, FOR EACH ROW, and AFTER triggered-action lists, see Action Clause.

You cannot have FOR EACH ROW actions on tables that have globally-detached indexes.

Triggered Action List

The triggered action consists of an optional WHEN condition and the action statements. Database objects that are referenced in the triggered action, that is, tables, columns, and UDRs, must exist when the CREATE TRIGGER statement is executed. This rule applies only to database objects that are referenced directly in the trigger definition.

Warning: When you specify a date expression in the WHEN condition or in an action statement, make sure to specify 4 digits instead of 2 digits for the year. When you specify a 4-digit year, the DBCENTURY environment variable has no effect on how the database server interprets the date expression. When you specify a 2-digit year, the DBCENTURY environment variable can affect how the database server interprets the date expression, so the triggered action might produce unpredictable results. See the "Informix Guide to SQL: Reference" for more information on the DBCENTURY environment variable.

WHEN Condition

The WHEN condition lets you make the triggered action dependent on the outcome of a test. When you include a WHEN condition in a triggered action, if the triggered action evaluates to true, the actions in the triggered action list execute in the order in which they appear. If the WHEN condition evaluates to false or unknown, the actions in the triggered action list are not executed. If the triggered action is in a FOR EACH ROW section, its search condition is evaluated for each row.

For example, the triggered action in the following trigger executes only if the condition in the WHEN clause is true:

An SPL routine that executes inside the WHEN condition carries the same restrictions as a UDR that is called in a data-manipulation statement.That is, the called SPL routine cannot contain certain SQL statements. For information on which statements are restricted, see Restrictions on an SPL Routine Called in a Data Manipulation Statement.

Action Statements

The triggered-action statements can be INSERT, DELETE, UPDATE, EXECUTE FUNCTION, or EXECUTE PROCEDURE statements. If a triggered-action list contains multiple statements, these statements execute in the order in which they appear in the list.

UDRs as Triggered Actions

You can use both user-defined functions and user-defined procedures as triggered actions.

Use the EXECUTE FUNCTION statement to execute any user-defined function. Use the EXECUTE PROCEDURE statement to execute any user-defined procedure.

Use the EXECUTE PROCEDURE statement to execute any SPL routine.

For restrictions that apply to using SPL routines as triggered actions, see Rules for SPL Routines.

Achieving a Consistent Result

To guarantee that the triggering statement returns the same result with and without the triggered actions, make sure that the triggered actions in the BEFORE and FOR EACH ROW sections do not modify any table referenced in the following clauses:

Using Reserved Words

If you use the INSERT, DELETE, UPDATE, or EXECUTE reserved words as an identifier in any of the following clauses inside a triggered action list, you must qualify them by the owner name, the table name, or both:

You get a syntax error if these keywords are not qualified when you use these clauses inside a triggered action.

If you use the keyword as a column name, it must be qualified by the table name-for example, table.update. If both the table name and the column name are keywords, they must be qualified by the owner name-for example, owner.insert.update. If the owner name, table name, and column name are all keywords, the owner name must be in quotes-for example, 'delete'.insert.update. The only exception is when these keywords are the first table or column name in the list, and you do not have to qualify them. For example, delete in the following statement does not need to be qualified because it is the first column listed in the INTO clause:

The following statements show examples in which you must qualify the column name or the table name:

Using Correlation Names in Triggered Actions

The following rules apply when you use correlation names in triggered actions:

When to Use Correlation Names

In an SQL statement in a FOR EACH ROW triggered action, you must qualify all references to columns in the triggering table with either the old or new correlation name, unless the statement is valid independent of the triggered action.

In other words, if a column name inside a FOR EACH ROW triggered action list is not qualified by a correlation name, even if it is qualified by the triggering table name, it is interpreted as if the statement is independent of the triggered action. No special effort is made to search the definition of the triggering table for the non-qualified column name.

For example, assume that the following DELETE statement is a triggered action inside the FOR EACH ROW section of a trigger:

For the statement to be valid, both col_c and col_c2 must be columns from tab1. If col_c2 is intended to be a correlation reference to a column in the triggering table, it must be qualified by either the old or the new correlation name. If col_c2 is not a column in tab1 and is not qualified by either the old or new correlation name, you get an error.

When a column is not qualified by a correlation name, and the statement is valid independent of the triggered action, the column name refers to the current value in the database. In the triggered action for trigger t1 in the following example, mgr in the WHERE clause of the correlated subquery is an unqualified column from the triggering table. In this case, mgr refers to the current column value in empsal because the INSERT statement is valid independent of the triggered action.

In a triggered action, an unqualified column name from the triggering table refers to the current column value, but only when the triggered statement is valid independent of the triggered action.

Qualified Versus Unqualified Value

The following table summarizes the value retrieved when you use the column name qualified by the old correlation name and the column name qualified by the new correlation name.

Trigger Event old.col new.col
INSERT No value (error) Inserted value
UPDATE (column updated) Original value Current value (N)
UPDATE (column not updated) Original value Current value (U)
DELETE Original value No value (error)

Refer to the following key when you read the table.

Term Purpose
Original value Value before the triggering statement
Current value Value after the triggering statement
(N) Cannot be changed by triggered action
(U) Can be updated by triggered statements; value may be different from original value because of preceding triggered actions

Outside a FOR EACH ROW triggered-action list, you cannot qualify a column from the triggering table with either the old correlation name or the new correlation name; it always refers to the current value in the database.

Reentrancy of Triggers

In some cases a trigger can be reentrant. In these cases the triggered action can reference the triggering table. In other words, both the trigger event and the triggered action can operate on the same table. The following list summarizes the situations in which triggers can be reentrant and the situations in which triggers cannot be reentrant:

Reentrancy and Cascading Triggers

The cases when a trigger cannot be reentrant apply recursively to all cascading triggers, which are considered part of the initial trigger. In particular, this rule means that a cascading trigger cannot update any columns in the triggering table that were updated by the original triggering statement, including any nontriggering columns affected by that statement. For example, assume the following UPDATE statement is the triggering statement:

Then in the cascading triggers shown in the following example, trig2 fails at runtime because it references column b, which is updated by the triggering UPDATE statement:

Now consider the following SQL statements. When the final UPDATE statement is executed, column a is updated and the trigger trig1 is activated. The triggered action again updates column a with an EXECUTE PROCEDURE INTO statement.

In Enterprise Decision Server, to recreate this example use the CREATE PROCEDURE statement instead of the CREATE FUNCTION statement.

Several questions arise from this example of cascading triggers. First, should the update of column a activate trigger trig1 again? The answer is no. Because the trigger was activated, it is stopped from being activated a second time. Whenever the triggered action is an EXECUTE PROCEDURE INTO statement or EXECUTE FUNCTION INTO statement, the only triggers that are activated are those that are defined on columns that are mutually exclusive from the columns in that table updated until then (in the cascade of triggers). Other triggers are ignored.

Another question that arises from the example is whether trigger trig2 should be activated. The answer is yes. The trigger trig2 is defined on column e. Until now, column e in table temp1 has not been modified. Trigger trig2 is activated.

A final question that arises from the example is whether triggers trig1 and trig2 should be activated after the triggered action in trig2 is performed. The answer is no. Neither trigger is activated. By this time columns a and e have been updated once, and triggers trig1 and trig2 have been executed once. The database server ignores these triggers instead of firing them.

For more information about cascading triggers, see Cascading Triggers.

Rules for SPL Routines

In addition to the rules listed in Reentrancy of Triggers, the following rules apply to an SPL routine that is used as a triggered action:

When you use an SPL routine as a triggered action, the database objects that the routine references are not checked until the routine is executed.

Privileges to Execute Triggered Actions

If you are not the trigger owner, but the privileges of the trigger owner include the WITH GRANT OPTION privilege, you inherit the privileges of the owner as well as the WITH GRANT OPTION privilege for each triggered SQL statement. You have these privileges in addition to your privileges.

If the triggered action is a UDR you must have the Execute privilege on the UDR or the owner of the trigger must have the Execute privilege and the WITH GRANT OPTION privilege.

While executing the UDR, you do not carry the privileges of the trigger owner; instead you receive the privileges granted with the UDR, as follows:

  1. Privileges for a DBA UDR
  2. When a UDR is registered with the CREATE DBA keywords and you are granted the Execute privilege on the UDR, the database server automatically grants you temporary DBA privileges while the UDR executes. These DBA privileges are available only when you are executing the UDR.

  3. Privileges for a UDR without DBA restrictions
  4. If the UDR owner has the WITH GRANT OPTION right for the necessary privileges on the underlying database objects, you inherit these privileges when you are granted the Execute privilege. In this case, all the non-qualified database objects that the UDR references are qualified by the name of the UDR owner.

    If the UDR owner does not have the WITH GRANT OPTION right, you have your original privileges on the underlying database objects when the UDR executes.

For more information on privileges on SPL routines, refer to the Informix Guide to SQL: Tutorial.

Creating a Triggered Action That Anyone Can Use

To create a trigger that is executable by anyone who has the privileges to execute the triggering statement, you can ask the DBA to create a DBA-privileged UDR and grant you the Execute privilege with the WITH GRANT OPTION right. You then use the DBA-privileged UDR as the triggered action. Anyone can execute the triggered action because the DBA-privileged UDR carries the WITH GRANT OPTION right. When you activate the UDR, the database server applies privilege-checking rules for a DBA.

Cascading Triggers

The database server allows triggers other than select triggers to cascade, meaning that the triggered actions of one trigger can activate another trigger. For further information on the restriction against cascading select triggers, see Circumstances When a Select Trigger is Not Activated.

The maximum number of triggers in a cascading sequence is 61; the initial trigger plus a maximum of 60 cascading triggers. When the number of cascading triggers in a series exceeds the maximum, the database server returns error number -748, as the following example shows:

The following example illustrates a series of cascading triggers that enforce referential integrity on the manufact, stock, and items tables in the stores_demo database. When a manufacturer is deleted from the manufact table, the first trigger, del_manu, deletes all the items from that manufacturer from the stock table. Each delete in the stock table activates a second trigger, del_items, that deletes all the items from that manufacturer from the items table. Finally, each delete in the items table triggers the SPL routine log_order, which creates a record of any orders in the orders table that can no longer be filled.

When you are not using logging, referential integrity constraints on both the manufact and stock tables prohibit the triggers in this example from executing. When you use logging, however, the triggers execute successfully because constraint checking is deferred until all the triggered actions are complete, including the actions of cascading triggers. For more information about how constraints are handled when triggers execute, see Constraint Checking.

The database server prevents loops of cascading triggers by not allowing you to modify the triggering table in any cascading triggered action, except an UPDATE statement, which does not modify any column that the triggering UPDATE statement updated, or an INSERT statement. INSERT trigger statements can have UPDATE triggered actions on the same table.

Constraint Checking

When you use logging, the database server defers constraint checking on the triggering statement until after the statements in the triggered-action list execute. The database server effectively executes a SET CONSTRAINTS ALL DEFERRED statement before it executes the triggering statement. After the triggered action is completed, it effectively executes a SET CONSTRAINTS constraint IMMEDIATE statement to check the constraints that were deferred. This action allows you to write triggers so that the triggered action can resolve any constraint violations that the triggering statement creates. For more information, see SET Database Object Mode.

Consider the following example, in which the table child has constraint r1, which references the table parent. You define trigger trig1 and activate it with an INSERT statement. In the triggered action, trig1 checks to see if parent has a row with the value of the current cola in child; if not, it inserts it.

When you insert a row into a table that is the child table in a referential constraint, the row might not exist in the parent table. The database server does not immediately return this error on a triggering statement. Instead, it allows the triggered action to resolve the constraint violation by inserting the corresponding row into the parent table. As the previous example shows, you can check within the triggered action to see whether the parent row exists, and if so, bypass the insert.

For a database without logging, the database server does not defer constraint checking on the triggering statement. In this case, it immediately returns an error if the triggering statement violates a constraint.

You cannot use the SET Transaction Mode statement in a triggered action. The database server checks this restriction when you activate a trigger, because the statement could occur inside a UDR.

Rows that cause constraint violations might appear in the violations table even if a later trigger action corrects the violation.

Preventing Triggers from Overriding Each Other

When you activate multiple triggers with an UPDATE statement, a trigger can possibly override the changes that an earlier trigger made. If you do not want the triggered actions to interact, you can split the UPDATE statement into multiple UPDATE statements, each of which updates an individual column. As another alternative, you can create a single update trigger for all columns that require a triggered action. Then, inside the triggered action, you can test for the column being updated and apply the actions in the desired order. This approach, however, is different than having the database server apply the actions of individual triggers, and it has the following disadvantages:

Client/Server Environment

The statements inside the triggered action can affect tables in external databases. The following example shows an update trigger on dbserver1, which triggers an update to items on dbserver2:

If a statement from an external database server initiates the trigger, however, and the triggered action affects tables in an external database, the triggered actions fail. For example, the following combination of triggered action and triggering statement results in an error when the triggering statement executes:

Logging and Recovery

You can create triggers for databases, with and without logging. However, when the database does not have logging, you cannot roll back when the triggering statement fails. In this case, you are responsible for maintaining data integrity in the database.

If the trigger fails and the database has transactions, all triggered actions and the triggering statement are rolled back because the triggered actions are an extension of the triggering statement. The rest of the transaction, however, is not rolled back.

The row action of the triggering statement occurs before the triggered actions in the FOR EACH ROW section. If the triggered action fails for a database without logging, the application must restore the row that was changed by the triggering statement to its previous value.

When you use a UDR as a triggered action, if you terminate the UDR in an exception-handling section, any actions that modify data inside that section are rolled back along with the triggering statement. In the following partial example, when the exception handler traps an error, it inserts a row into the table logtab:

When the RAISE EXCEPTION statement returns the error, however, the database server rolls back this insert because it is part of the triggered actions. If the UDR is executed outside a triggered action, the insert is not rolled back.

The UDR that implements a triggered action cannot contain any BEGIN WORK, COMMIT WORK, or ROLLBACK WORK statements. If the database has logging, you must either begin an explicit transaction before the triggering statement, or the statement itself must be an implicit transaction. In any case, another transaction-related statement cannot appear inside the UDR.

You can use triggers to enforce referential actions that the database server does not currently support. For any database without logging, you are responsible for maintaining data integrity when the triggering statement fails.

Trigger Modes

Use the trigger-modes to enable or disable a trigger when you create it.

You can create triggers in the following modes.

Mode Purpose
DISABLED When a trigger is created in disabled mode, the database server does not execute the triggered action when the trigger event (an insert, delete, select, or update operation) takes place. In effect, the database server ignores the trigger even though its catalog information is maintained.
ENABLED When a trigger is created in enabled mode, the database server executes the triggered action when the trigger event (an insert, delete, select, or update operation) takes place.

Specifying Modes for Triggers

You must observe the following rules when you specify the mode for a trigger in the CREATE TRIGGER statement:

Related Information

Related statements: DROP TRIGGER, CREATE PROCEDURE, EXECUTE PROCEDURE, and SET Database Object Mode

For a task-oriented discussion of triggers, see the Informix Guide to SQL: Tutorial.

For performance implications of triggers, see your Performance Guide.


Informix Guide to SQL: Syntax, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved