Creates a trigger which is invoked by data changes in a named table or view.
A trigger is created on a table or view (table reference).
For a complete description of triggers, see the Mimer SQL Programmer's Manual, Triggers.
trigger-nameshould follow the normal rules for naming database objects, see Naming Objects.
trigger-nameis specified in its unqualified form, the trigger will be created in the schema which has the same name as the current ident.
trigger-nameis specified in its fully qualified form (i.e.
schema-name.trigger-name) the trigger will be created in the named schema (in this case, the current ident must be the creator of the specified schema).
trigger-actionwill be executed when the data manipulation operation specified by
search-conditionspecified in the
WHENclause of the
trigger-actionevaluates to true.
There are two types of triggers, row triggers and statement triggers. A row trigger is executed once for each row that is modified by a data manipulation operation. A statement trigger is invoked once for a data manipulation operation.
A row trigger is defined by specifying for each row in the trigger definition. If for each statement is specified or the for each clause is omitted, the trigger will be a statement trigger. Note that a statement trigger will always be invoked, regardless of if any rows are modified by the data manipulation operation. A row trigger will only be executed if any row is affected by the data manipulation.
The trigger time specifies when a trigger is executed. For a more detailed description of this, see Mimer SQL Programmer's Manual, Triggers.
It is possible to create multiple triggers for the same event and time and if so the triggers will be executed in the order they are created.
In a statement trigger it is possible to refer to temporary tables that contains the data affected by the data manipulation operation. These tables are named in the referencing clause and are commonly referred to as the old and new table. These tables are read only.
The old table shows the data as it were before the data manipulation operation and the new table shows the data after the statement has taken place.
The old table can be used if the trigger event is delete or update. The new table can be used if the trigger event is update or insert. The temporary tables will only be created if there is at least one statement trigger that references the old or new table.
In a row trigger it is possible to refer to the row being affected by the data manipulation operation. The old and new row variables can be seen as implicit parameters for the triggers. The old row variable is read only in all cases but the new row variable can be modified if the trigger time is before. The old and new row are defined as records where each field corresponds to a column in the table reference. To refer to individual fields a dot notation is used. See example below.
If the trigger time is INSTEAD OF the table reference must be a view. This is the only trigger time that can be specified for a trigger defined on a view.
If there is an INSTEAD OF trigger defined for a view this means that the data manipulation operation for a view will not be performed, but the trigger will be executed instead. In the trigger it is possible to do data manipulations on the tables on which the view is defined. Thus it is possible to make any view updatable by creating an instead of trigger. An instead of trigger may also use the old and new tables to access the data affected by the data manipulation operation that caused the trigger to be executed.
The trigger and
table-referencemust belong to the same schema.
Two triggers with the same name cannot belong to the same schema.
If the trigger time is
INSTEAD OF, then
table-referencemust be the name of a view.
OLD TABLE and NEW TABLE may each be specified only once in the alias-list and the same alias-name must not appear twice in the list.
OLD ROW and NEW ROW may each be specified only once in the alias-list and the same alias-name must not appear twice in the list.
OLD ROW and NEW ROW may only be specified if FOR EACH ROW is specified.
OLD TABLE or OLD ROW may not be specified if the
NEW TABLE or NEW ROW may not be specified if the
AFTER and INSTEAD OF are currently not supported for row triggers.
If the trigger time is BEFORE and FOR EACH STATEMENT is specified, the REFERENCING keyword and alias-list must not be specified.
If the procedural-sql-statement of the trigger-action is a COMPOUND STATEMENT, it must be ATOMIC.
The creator of the trigger must hold the appropriate access rights, with grant option, for all operations performed in the trigger action.
The trigger-action must not contain a COMMIT or ROLLBACK statement.
If the trigger time is BEFORE, the following restrictions apply to the trigger-action:
- the trigger-action must not contain any SQL statement that performs data update (i.e. DELETE, INSERT and UPDATE statements are not permitted)
- a routine which possibly MODIFIES SQL DATA may not be invoked from within the trigger-action.
A trigger can be created on tables that have columns defined as LARGE OBJECT data type, with the restrictions that it is not possible to refer to such columns in the new table in an instead of trigger and that it is not possible to modify such fields in the new row variable.
trigger-actionis always executed in the transaction started for the data manipulation operation which caused the trigger to be invoked. Thus, if the data manipulation operation is subject to a rollback, all operations performed in the
trigger-actionwill also be undone and an unhandled error occurring in the
trigger-actionwill be treated like an error in the triggering data manipulation statement. Situations like this can be handled using condition handlers. (See DECLARE HANDLER.)
During the execution of the
trigger-action, the effect of changes made in the transaction are visible.
The scope of the
trigger-actionis the optional
WHENclause and the
The tables specified by using
NEW TABLEin the
alias-listare temporary and are local to scope of the
trigger-action. It is not possible to perform any data change operations on either table and the data contained in each will not otherwise change during the time it exists.
Data manipulation operations performed in the
trigger-actionmay cause the trigger to be invoked recursively. Trigger execution in a recursive situation will proceed normally in every respect.
If the body of the trigger contains operations on tables located in a databank with work option, these operations will not be part of the atomic statement that constitute the trigger execution.
procedural-sql-statementin a trigger contains a compound statement, it is possible to declare condition handlers for handling errors that may occur in the trigger code. See DECLARE HANDLER.
ExamplesCREATE TRIGGER mimer_store_book.titles_after_insert AFTER INSERT ON mimer_store_book.titles REFERENCING NEW TABLE AS btl BEGIN ATOMIC ... ... END -- of trigger mimer_store_book.titles_after_insert CREATE TABLE versions(document_id int, version_date date); CREATE TRIGGER set_version_date BEFORE UPDATE ON versions REFERENCING NEW ROW AS new_version OLD ROW AS old_version FOR EACH ROW IF old_version.version_date = new_version.version_date THEN SET new_version.version_date = current_date; END IF;
For more information, see the Mimer SQL Programmer's Manual, Triggers.
Mimer Information Technology AB
Voice: +46 18 780 92 00
Fax: +46 18 780 92 40