The CREATE TRIGGER statement (create_trigger_statement) defines a trigger for a base table.
<trigger_event> ::= INSERT
| UPDATE [(<column_list>)]
<column_list> ::= <column_name> | <column_list>,<column_name>
SQL Tutorial Database Triggers
A trigger is a special type of database procedure that is assigned to a base table. A trigger cannot be executed explicitly. Instead, it is executed automatically by the database system when defined events trigger_event occur in the table.
If a schema is not specified in the table name, the current schema is accepted implicitly.
The table name must identify an existing table in the schema. The current user must be the owner of the specified table.
The trigger_event defines which event sets off the trigger. This trigger is called whenever the triggering event takes place, as long as no faults occur.
INSERT: The INSERT trigger event causes the trigger to be executed whenever a row is inserted in the table.
UPDATE: The UPDATE event causes the trigger to be executed whenever a row in the table is modified. If a column_list is specified, the trigger is only called if one of the columns in the column list is modified.
DELETE: The DELETE trigger event causes the trigger to be executed whenever a row is deleted from the table.
A maximum of one trigger may be defined for each trigger event in each table.
Please note the following information regarding the trigger routine.
Each INSERT trigger implicitly has the corresponding variable NEW.<column_name> for each column in the table. When the trigger is executed, this variable has the value of the corresponding column in the inserted row. It is only permissible to specify NEW for SQL statements specified in routine_sql_statements. Specifying NEW for other statements causes an error to occur.
Each UPDATE trigger implicitly has a corresponding variable NEW.<column name> and OLD.<column name> for each column in the table. When the trigger is executed, the OLD.<column_name> variable has the value of the corresponding column in front of and NEW.<column_name> after the row is changed. Specifying NEW and OLD is optional.
Each DELETE trigger implicitly has a corresponding variable OLD.<column_name> for each column in the table. When the trigger is executed, this variable has the value of the corresponding column in the deleted row. You can only specify OLD for SQL statements specified in routine_sql_statements. Specifying OLD for other statements causes an error to occur.
A colon must always be used with NEW and :OLD in SQL statements statement used in triggers specified as routine_sql_statements
(For example: UPDATE hotel.room SET hno = :NEW.hno WHERE hno = :OLD.hno).
In SQL statements used in triggers, which are not routine_sql_statements NEW and OLD must always be used without a colon (For example: IF NEW.hno <> OLD.hno).
If, on STOP, the trigger ends an error number other than zero, the entire SQL statement that set off the trigger fails.
The SUBTRANS statement is not allowed in a trigger.