The CREATE TRIGGER statement (create_trigger_statement) defines a trigger for a base table.
<create_trigger_statement> ::= CREATE TRIGGER
<trigger_name>
FOR <table_name>
AFTER <trigger_event>,... EXECUTE (<routine>)
[WHENEVER <search_condition>
]
<trigger_event> :: =
INSERT
| UPDATE [(<column_list>)]
| DELETE
<column_list> ::= <column_name> |
<column_list>,<column_name>
The trigger ensures that the hotel number in the room table is also changed when a hotel number is changed in the hotel table.
CREATE TRIGGER hotel_update FOR hotel AFTER UPDATE
EXECUTE (
TRY
IF NEW.hno <> OLD.hno
THEN UPDATE mona.room SET hno = :NEW.hno WHERE hno = :OLD.hno;
CATCH
IF $rc <> 100
THEN STOP ($rc, 'unexpected error');
)
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 assumed 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 trigger_event defines what triggers the trigger. The trigger is always invoked if the triggering event has been processed correctly.
INSERT: the INSERT trigger event causes the trigger to be executed for each row inserted in the table.
UPDATE: the UPDATE event causes the trigger to be executed for each modification made to a row in the table. If a column list column_list is specified, the trigger is only called if one of the columns in the column list was modified. With UPDATE statements, you can specify IGNORE TRIGGER so that the trigger event UPDATE is ignored.
DELETE: the DELETE trigger event causes the trigger to be executed for every row deleted from the table.
A maximum of one trigger can be defined for each trigger event in each table.
Note the following information about the trigger routine routine.
Each INSERT trigger implicitly has a 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 the other statements leads to an error.
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 change in the row. 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. It is only permissible to specify OLD for SQL statements specified in routine_sql_statements. Specifying OLD for the other statements leads to an error.
NEW and :OLD must always be used with a colon in
SQL statements statement that are used in triggers and that belong to the routine_sql_statements
(For example: UPDATE mona.room SET hno
= :NEW.hno WHERE hno = :OLD.hno).
NEW and OLD must always be used without a colon
in SQL statements that are used in triggers and that do not belong to the
routine_sql_statements
(For example: IF NEW.hno <>
OLD.hno).
If the trigger is terminated by STOP with an error number not equal to zero, the entire SQL statement that triggered the trigger fails.
The SUBTRANS statement is not allowed in a trigger.
If a WHENEVER statement is specified, the trigger is only executed if the search condition is fulfilled. The condition must not contain a subquery nor any set functions.