08-08-2012, 01:27 PM
DB2 Trigger
Trigger.pdf (Size: 914.27 KB / Downloads: 62)
What’s Trigger?
A trigger defines a set of action and can be activated
when a specified SQL operation -- a DELETE, INSERT or
UPDATE -- occurs on a table.
Triggers, unlike referential and check constraints, can
even be used to update other tables.
Why Triggers ?!
Validate input data
Note that validation of non-transitional data is usually better
handled by check and referential constraints. By contrast,
triggers are appropriate for validation of transitional data,
that is, validations which require comparisons between the
value before and after an update operation.
Automatically generate values for newly inserted
rows
Read from other tables for cross-referencing purposes
Write to other tables for audit-trail purposes
Support alerts (for example, through e-mail messages)
Benefits of Triggers
Faster application development
Because triggers are stored in the relational database, the
actions performed by triggers do not have to be coded in
each application.
Global enforcement of business rules
A trigger only has to be defined once, and then it can be
used for any application that changes the table.
Easier maintenance
If a business policy changes, only the corresponding
trigger needs to change instead of each application
program.
DB2 Trigger
Notes for Trigger Action
I comandi SQL sono eseguiti come operazioni atomiche:
se uno dei comandi fallisce allora viene effettuato il
rollback dell’operazione che ha attivato il trigger e di tutti i
comandi nell’azione. (Non viene effettuato il rollback della
transazione contenente l’operazione che ha attivato il
trigger.)
Per effettuare il rollback della transazione contenente
l’operazione che ha attivato il trigger, si può utilizzare
l’istruzione SIGNAL.