Sunday 18 August 2013

Detail Introduction of Triggers

Triggers provide a procedural technique to specify and maintain integrity constraints.
Triggers even allow users to specify more complex integrity constraints since a trigger essentially is a PL/SQL procedure. Such a procedure is associated with a table and is automatically called by the database system whenever a certain modification (event) occurs on that table.
Modifications on a table may include insert, update, and delete operations.
Structure of Triggers
A trigger definition consists of the following (optional) components:
• trigger name create [or replace] trigger
• trigger time point before | after
• triggering event(s) insert or update [of ] or delete on
• trigger type (optional) for each row
• trigger restriction (only for for each row triggers !) when ()
• trigger body
The clause replace re-creates a previous trigger definition having the same . The name of a trigger can be chosen arbitrarily, but it is a good programming style to use a trigger name that reflects the table and the event(s), e.g., upd ins EMP. A trigger can be invoked before or after the triggering event. The triggering event specifies before (after) which operations on the table the trigger is executed. A single event is an insert, an update, or a delete; events can be combined using the logical connective or. If for an update trigger no columns are specified, the trigger is executed after (before) is updated. If the trigger should only be executed when certain columns are updated, these columns must be
specified after the event update. If a trigger is used to maintain an integrity constraint, the triggering events typically correspond to the operations that can violate the integrity constraint.
it is essential to understand the difference between a row level trigger and a statement level trigger. A row level trigger is defined using the clause for each row. If this clause is not given, the trigger is assumed to be a statement trigger. A row trigger executes once for each row after (before) the event. A statement trigger is executed once after (before) the event, independent of how many rows are affected by the event. For example, a row trigger with the event specification after update is executed once for each row affected by the update. Thus, if the update affects 20 tuples, the trigger is executed 20 times, for each row at a time. In contrast, a statement trigger is only executed once. When combining the different types of triggers, there are twelve possible trigger configurations that can be defined for a table:
trigger time point trigger type event before after statement row
insert X X X X
update X X X X
delete X X X X
Row triggers have some special features that are not provided by statement triggers:
Only with a row trigger it is possible to access the attribute values of a tuple before and after the modification (because the trigger is executed once for each tuple). For an update trigger, the old attribute value can be accessed using :o ld. and the new attribute value can be accessed using :new.. For an insert trigger, only :new. can be used, and for a delete trigger only :o ld. can be used (because there exists no old, respectively, new value of the tuple). In these cases, :new. refers to the attribute value of of the inserted tuple, and :o ld. refers to the attribute value of of the deleted tuple. In a row trigger thus it is possible to specify comparisons between old and new attribute values in the PL/SQL block, e.g., “if :old.SAL < :new.SAL then . . . ”. If for a row trigger the trigger time point before is specified, it is even possible to
modify the new values of the row, e.g., :new.SAL := :new.SAL _ 1.05 or :new.SAL := :o ld.SAL.
Such modifications are not possible with after row triggers. In general, it is advisable to use a after row trigger if the new row is not modified in the PL/SQL block. Oracle then can process these triggers more efficiently. Statement level triggers are in general only used in combination with the trigger time point after.
In a trigger definition the when clause can only be used in combination with a for each row trigger. The clause is used to further restrict when the trigger is executed. For the specification of the condition in the when clause, the same restrictions as for the check clause hold. The only exceptions are that the functions sysdate and user can be used, and that it is possible to refer to the old/new attribute values of the actual row. In the latter case, the colon “:” must not be used, i.e., only old. and new..
The trigger body consists of a PL/SQL block. All SQL and PL/SQL commands except the two statements commit and rollback can be used in a trigger’s PL/SQL block.
if constructs allow to execute certain parts of the PL/SQL block depending on the
triggering event. For this, the three constructs if inserting, if updating[(’’)], and
if deleting exist. They can be used as shown in the following example:
create or replace trigger emp check after insert or delete or update on EMP
for each row begin
if inserting then
end if ;
if updating then
end if ;
if deleting then
end if ;
end;
It is important to understand that the execution of a trigger’s PL/SQL block builds a part of the transaction that contains the triggering event. Thus, for example, an insert statement in a PL/SQL block can cause another trigger to be executed. Multiple triggers and modifications thus can lead to a cascading execution of triggers. Such a sequence of triggers terminates successfully if (1) no exception is raised within a PL/SQL block, and (2) no declaratively specified integrity constraint is violated. If a trigger raises an exception in a PL/SQL block, all modifications up to the beginning of the transaction are rolled back. In the PL/SQL block of a trigger, an exception can be raised using the statement raise application error. This statement causes an implicit rollback. In combination with a row trigger, raise application error can refer to old/new values of modified rows:
raise application error(−20020, ’Salary increase from ’ || to char(:old.SAL) || ’ to ’
to char(:new.SAL) || ’ is too high’); or
raise application error(−20030, ’Employee Id ’ || to char(:new .EMPNO) || ’ does not exist.’);

No comments:

Post a Comment