Triggers
Triggers are active database objects associated with a subject table. A trigger essentially consists of a stored SQL statement or a block of SQL statements.
Triggers execute when an INSERT, UPDATE, DELETE, or MERGE modifies a specified column or columns in the subject table.
Typically, a stored trigger performs an UPDATE, INSERT, DELETE, MERGE, or other SQL operation on one or more tables, which may possibly include the subject table.
Triggers in Teradata Database conform to the ANSI/ISO SQL:2008 standard, and also provide some additional features.
Triggers have two types of granularity:
You can create, alter, and drop triggers.
IF you want to … |
THEN use … |
define a trigger |
CREATE TRIGGER. |
|
ALTER TRIGGER. Disabling a trigger stops the trigger from functioning, but leaves the trigger definition in place as an object. This allows utility operations on a table that are not permitted on tables with enabled triggers. Enabling a trigger restores its active state. |
remove a trigger from the system permanently |
DROP TRIGGER. |
For details on creating, dropping, and altering triggers, see SQL Data Definition Language.
Process Flow
Note that this is a logical flow, not a physical re-enactment of how Teradata Database processes a trigger.
1 The triggering event occurs on the subject table.
2 A determination is made as to whether triggers defined on the subject table are to become active upon a triggering event.
3 Qualified triggers are examined to determine the trigger action time, whether they are defined to fire before or after the triggering event.
4 When multiple triggers qualify, then they fire normally in the ANSI-specified order of creation timestamp.
To override the creation timestamp and specify a different execution order of triggers, you can use the ORDER clause, a Teradata extension.
Even if triggers are created without the ORDER clause, you can redefine the order of execution by changing the trigger creation timestamp using the ALTER TRIGGER statement.
5 The triggered SQL statements (triggered action) execute.
If the trigger definition uses a REFERENCING clause to specify that old, new, or both old and new data for the triggered action is to be collected under a correlation name (an alias), then that information is stored in transition tables or transition rows:
6 The trigger passes control to the next trigger, if defined, in a cascaded sequence. The sequence can include recursive triggers.
Otherwise, control passes to the next statement in the application.
7 If any of the actions involved in the triggering event or the triggered actions abort, then all of the actions are aborted.
Restrictions
Most Teradata load utilities cannot access a table that has an active trigger.
An application that uses triggers can use ALTER TRIGGER to disable the trigger and enable the load. The application must be sure that loading a table with disabled triggers does not result in a mismatch in a user defined relationship with a table referenced in the triggered action.
Other restrictions on triggers include:
Archiving Triggers
Triggers are archived and restored as part of a database archive and restoration. Individual triggers can be archived or restored using the ARCHIVE or RESTORE statements of the ARC utility.
Related Topics
For detailed information on … |
See … |
|
CREATE TRIGGER in SQL Data Definition Language. |
|
ALTER TRIGGER in SQL Data Definition Language. |
permanently removing triggers from the system |
DROP TRIGGER in SQL Data Definition Language. |