Triggers are a special type of stored procedure used to maintain database integrity.
Like stored procedures, triggers also contain Java code (embedded in a CREATE TRIGGER statement) and use FairCom DB SQL Java classes. However, triggers are automatically invoked (“fired”) by certain FairCom DB SQL operations (an INSERT, UPDATE, or DELETE operation) on the trigger’s target table.
This chapter provides a general description of triggers and discusses in detail where trigger procedures differ from stored procedures. Unless otherwise noted, much of the material in Using Stored Procedures also applies to triggers.
Triggers are identical to stored procedures in many respects. There are three main differences:
The automatic nature of triggers make them well-suited for enforcing referential integrity. In this regard, they are like constraints, since both triggers and constraints can help insure that a value stored in the foreign key of a table must either be null or be equal to some value in the matching unique or primary key of another table.
However, triggers differ from constraints in the following ways:
Typical uses for triggers include combinations of the following:
Cascading deletes
A delete operation on one table causes additional rows to be deleted from other tables that are related to the first table by key values. This is an active way of enforcing referential integrity that a table constraint enforces passively.
Cascading updates
An update operation on one table causes additional rows to be updated in other tables that are related to the first table by key values. These updates are commonly limited to the key fields themselves. This is an active way of enforcing referential integrity that a table constraint enforces passively.
Summation updates
An update operation in one table causes a value in a row of another table to be updated by being increased or decreased.
Automatic archiving
A delete operation on one table creates an identical row in an archive table that is not otherwise used by the database.
Triggers can be created only on user tables. An attempt to create triggers on system tables (systrigger, systables, sysprocedures etc.) results in an error: (error (20158) : Can’t create triggers on system tables).