12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Overview of Triggers■■Use a BEFORE row trigger to modify the row before the row data is written to disk.Use an AFTER row trigger to obtain, and perform operations, using the row Id.Note: BEFORE row triggers are slightly more efficient than AFTERrow triggers. With AFTER row triggers, affected data blocks must beread (logical read, not physical read) once for the trigger and thenagain for the triggering statement. Alternatively, with BEFORE rowtriggers, the data blocks must be read only once for both the triggeringstatement and the trigger.If an UPDATE or DELETE statement detects a conflict with a concurrent UPDATE, then<strong>Oracle</strong> performs a transparent ROLLBACK and restarts the update. This can occurmany times before the statement completes successfully. Each time the statement isrestarted, the BEFORE statement trigger is fired again. The rollback does not undochanges to any package variables referenced in the trigger. Your package shouldinclude a counter variable to detect this situation.9.1.4.2 Firing Triggers With the FOR EACH ROW OptionThe FOR EACH ROW option determines whether the trigger is a row trigger or astatement trigger. If you specify FOR EACH ROW, then the trigger fires once for each rowof the table that is affected by the triggering statement. These triggers are referred to asrow-level triggers.The absence of the FOR EACH ROW option indicates that the trigger fires only once foreach applicable statement, but not separately for each row affected by the statement.These triggers are referred to as statement-level triggers and are useful for performingvalidation checks for the entire statement.9.1.4.3 Firing Triggers Based on Conditions (WHEN Clause)An optional trigger restriction can be included in the definition of a row trigger byspecifying a Boolean <strong>SQL</strong> expression in a WHEN clause.If included, then the expression in the WHEN clause is evaluated for each row that thetrigger affects. If the expression evaluates to TRUE for a row, then the trigger body isfired on behalf of that row.The expression in a WHEN clause must be a <strong>SQL</strong> expression, and it cannot include asubquery. You cannot use a PL/<strong>SQL</strong> expression (including user-defined functions) inthe WHEN clause. A WHEN clause cannot be included in the definition of a statementtrigger.9.1.5 Accessing Column Values in Row TriggersWithin a trigger body of a row trigger, the PL/<strong>SQL</strong> code and <strong>SQL</strong> statements haveaccess to the old and new column values of the current row affected by the triggeringstatement. Two correlation names exist for every column of the table being modified:one for the old column value, and one for the new column value. These columns in thetable are identified by :OLD.colum_name and :NEW.column_name.Depending on the type of triggering statement, certain correlation names might nothave any meaning.■A trigger fired by an INSERT statement has meaningful access to new columnvalues only. Because the row is being created by the INSERT, the old values arenull.Triggers: Usage Information 9-3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!