Parts of A Trigger
Parts of A Trigger
Parts of A Trigger
Parts of a Trigger
A trigger has three basic parts:
1. A triggering event or statement:
A triggering event or statement is the SQL statement, database event or user event
that causes a trigger to be fired.
2. A trigger restriction:
A trigger restriction specifies a Boolean [logical] expression that must be TRUE
for the trigger to fire.
3. A trigger action:
A trigger action is the PL SQL block that contains the sql statements and code to
be executed when a triggering statement is issued and the trigger restriction
evaluates to TRUE.
Types of triggers
Depending upon, when trigger is fired, it may be classified as :
1) Statement Level trigger
2) Row level trigger
3) Before trigger
4) After trigger
5) Instead of Trigger
1. BEFORE TRIGGER
BEFORE trigger indicates that Oracle fires the trigger before executing the triggering
statement.BEFORE triggers may not modify tables, but they can be used to verify input
column values, and also modify column values that are inserted or updated in a table.
2. AFTER TRIGGER
AFTER trigger indicates that Oracle fires the trigger after executing the triggering
statement.
3. STATEMENT TRIGGER
A statement trigger is fired once on behalf of the triggering statement, regardless of the
number of rows in the table that the triggering statement affects, ( even if no rows are
affected). For example, if a DELETE statement deletes several rows from a table, a
statement-level DELETE trigger is fired only once.Statement triggers are useful if the
code in the trigger action does not depend on the data provided by the triggering
statement or the rows affected. For example, use a statement trigger to:
Make a complex security check on the current time or user
4. ROW TRIGGER
A row trigger is fired each time the table is affected by the triggering statement. For
example, if an UPDATE statement updates multiple rows of a table, a row trigger is fired
once for each row affected by the UPDATE statement. If a triggering statement affects no
rows, a row trigger is not executed at all.Row triggers are useful if the code in the
trigger action depends on data provided by the triggering statement or rows that are
affected.
5. INSTEAD OF TRIGGER
You can write normal INSERT, UPDATE, and DELETE statements against the view and the
INSTEAD OF trigger is fired to update the underlying tables appropriately. INSTEAD OF
triggers are activated for each row of the view that gets modified.
Using the options listed previously, you can create four types of row and statement
triggers:
Before modifying each row affected by the triggering statement and before
checking appropriate integrity constraints, the trigger action is run, if the trigger
restriction was not violated.
After modifying each row affected by the triggering statement and possibly
applying appropriate integrity constraints, the trigger action is run for the
current row provided the trigger restriction was not violated. Unlike BEFORE
row triggers, AFTER row triggers lock rows.
After executing the triggering statement and applying any deferred integrity
constraints, the trigger action is run.
You can use triggers to publish information about database events to subscribers.
Applications can subscribe to database events just as they subscribe to messages from
other applications. These database events can include:
System events
User events
Provide auditing
Modify table data when DML statements are issued against views
Publish information about database events, user events, and SQL statements to
subscribing applications