Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.

Similar presentations


Presentation on theme: "Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs."— Presentation transcript:

1 Trigger Oracle PL/SQL

2 Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs Automatically executed when a particular event occurs InsertInsert UpdateUpdate DeleteDelete OthersOthers

3 Triggers vs. Procedures Procedures are explicitly executed by a user or application Procedures are explicitly executed by a user or application Triggers are implicitly executed (fired) when the triggering event occurs Triggers are implicitly executed (fired) when the triggering event occurs Triggers should not be used as a lazy way to invoke a procedure as they are fired every time the event occurs Triggers should not be used as a lazy way to invoke a procedure as they are fired every time the event occurs

4 Triggers

5 Triggers The trigger specification names the trigger and indicates when it will fire The trigger specification names the trigger and indicates when it will fire The trigger body contains the PL/SQL code to accomplish whatever task(s) need to be performed The trigger body contains the PL/SQL code to accomplish whatever task(s) need to be performed

6 Triggers

7 Triggers Timing A triggers timing has to be specified first A triggers timing has to be specified first Before (most common)Before (most common) Trigger should be fired before the operation Trigger should be fired before the operation i.e. before an inserti.e. before an insert AfterAfter Trigger should be fired after the operation Trigger should be fired after the operation i.e. after a delete is performedi.e. after a delete is performed

8 Trigger Events Three types of events are available Three types of events are available DML eventsDML events DDL eventsDDL events Database eventsDatabase events

9 DML Events Changes to data in a table Changes to data in a table InsertInsert UpdateUpdate DeleteDelete

10 DDL Events Changes to the definition of objects Changes to the definition of objects TablesTables IndexesIndexes ProceduresProcedures FunctionsFunctions OthersOthers Include CREATE, ALTER and DROP statements on these objects Include CREATE, ALTER and DROP statements on these objects

11 Database Events Server Errors Server Errors Users Log On or Off Users Log On or Off Database Started or Stopped Database Started or Stopped

12 Trigger DML Events Can specify one or more events in the specification Can specify one or more events in the specification i.e. INSERT OR UPDATE OR DELETEi.e. INSERT OR UPDATE OR DELETE Can specify one or more columns to be associated with a type of event Can specify one or more columns to be associated with a type of event i.e. BEFORE UPDATE OF SID OR SNAMEi.e. BEFORE UPDATE OF SID OR SNAME

13 Table Name The next item in the trigger is the name of the table to be affected The next item in the trigger is the name of the table to be affected

14 Trigger Level Two levels for Triggers Two levels for Triggers Row-level triggerRow-level trigger Requires FOR EACH ROW clause Requires FOR EACH ROW clause If operation affects multiple rows, trigger fires once for each row affectedIf operation affects multiple rows, trigger fires once for each row affected Statement-level triggerStatement-level trigger DML triggers should be row-levelDML triggers should be row-level DDL and Database triggers should not be row-levelDDL and Database triggers should not be row-level

15 Event Examples

16 Triggers Conditions Available So Multiple Operations Can Be Dealt With In Same Trigger Conditions Available So Multiple Operations Can Be Dealt With In Same Trigger Inserting, Updating, DeletingInserting, Updating, Deleting Column Prefixes Allow Identification Of Value Changes Column Prefixes Allow Identification Of Value Changes New, OldNew, Old

17 Triggers Exceptions EXCEPTION Data Type Allows Custom Exceptions EXCEPTION Data Type Allows Custom Exceptions RAISE Allows An Exception To Be Manually Occur RAISE Allows An Exception To Be Manually Occur RAISE_APPLICATION_ERROR Allows Termination Using A Custom Error Message RAISE_APPLICATION_ERROR Allows Termination Using A Custom Error Message Must Be Between -20000 and -20999Must Be Between -20000 and -20999 Message Can Be Up to 512 BytesMessage Can Be Up to 512 Bytes

18 END

19 Cursors Cursors Hold Result of an SQL Statement Cursors Hold Result of an SQL Statement Two Types of Cursors in PL/SQL Two Types of Cursors in PL/SQL Implicit – Automatically Created When a Query or Manipulation is for a Single RowImplicit – Automatically Created When a Query or Manipulation is for a Single Row Explicit – Must Be Declared by the UserExplicit – Must Be Declared by the User Creates a Unit of Storage Called a Result Set Creates a Unit of Storage Called a Result Set

20 Cursors Result Set MIS380 DATABASE DESIGN4 MIS202 INFORMATION SYSTEMS 3 <Cursor MIS485 MANAGING TECHNOLOGY4 MIS480 ADVANCED DATABASE 4

21 Cursors Declaring an Explicit Cursor Declaring an Explicit Cursor CURSOR CursorName IS SelectStatement; Opening an Explicit Cursor Opening an Explicit Cursor OPEN CursorName; Accessing Rows from an Explicit Cursor Accessing Rows from an Explicit Cursor FETCH CursorName INTO RowVariables;

22 Cursors Declaring Variables of the Proper Type with %TYPE Declaring Variables of the Proper Type with %TYPE VarName TableName.FieldName%TYPE; Declaring Variables to Hold An Entire Row Declaring Variables to Hold An Entire Row VarName CursorName%ROWTYPE; Releasing the Storage Area Used by an Explicit Cursor Releasing the Storage Area Used by an Explicit Cursor CLOSE CursorName;

23 Iterative Structures LOOP … EXIT … END LOOP LOOP … EXIT … END LOOP EXIT with an If Avoids Infinite LoopEXIT with an If Avoids Infinite Loop LOOP … EXIT WHEN … END LOOP LOOP … EXIT WHEN … END LOOP Do Not Need An If to Control EXITDo Not Need An If to Control EXIT WHILE … LOOP … END LOOP WHILE … LOOP … END LOOP Eliminates Need for EXITEliminates Need for EXIT FOR … IN … END LOOP FOR … IN … END LOOP Eliminates Need for Initialization of CounterEliminates Need for Initialization of Counter

24 Cursor Control With Loops Need a Way to Fetch Repetitively Need a Way to Fetch Repetitively Need a Way to Determine How Many Rows to Process With a Cursor Need a Way to Determine How Many Rows to Process With a Cursor Cursor AttributesCursor Attributes CursorName%ROWCOUNT – Number of Rows in a Result Set CursorName%ROWCOUNT – Number of Rows in a Result Set CursorName%FOUND – True if a Fetch Returns a Row CursorName%FOUND – True if a Fetch Returns a Row CursorName%NOTFOUND – True if Fetch Goes Past Last Row CursorName%NOTFOUND – True if Fetch Goes Past Last Row

25 Cursor For Loop Processing an Entire Result Set Common Processing an Entire Result Set Common Special Form of FOR … IN to Manage Cursors Special Form of FOR … IN to Manage Cursors No Need for Separate OPEN, FETCH and CLOSE statements No Need for Separate OPEN, FETCH and CLOSE statements Requires %ROWTYPE Variable Requires %ROWTYPE Variable

26 Creating a Cursor We create a Cursor when we want to go over a result of a query (like ResultSet in JDBC) We create a Cursor when we want to go over a result of a query (like ResultSet in JDBC) Syntax Example: Syntax Example: DECLARE DECLARE cursor c is select * from sailors; cursor c is select * from sailors; sailorData sailors%ROWTYPE; sailorData sailors%ROWTYPE; BEGIN BEGIN open c; open c; fetch c into sailorData; fetch c into sailorData; sailorData is a variable that can hold a ROW from the sailors table Here the first row of sailors is inserted into sailorData

27 Example DECLARE Pi constant NUMBER(8,7) := 3.1415926; Pi constant NUMBER(8,7) := 3.1415926; area NUMBER(14,2); area NUMBER(14,2); cursor rad_cursor is select * from RAD_VALS; cursor rad_cursor is select * from RAD_VALS; rad_value rad_cursor%ROWTYPE; rad_value rad_cursor%ROWTYPE;BEGIN open rad_cursor; open rad_cursor; fetch rad_cursor into rad_val; fetch rad_cursor into rad_val; area:=pi*power(rad_val.radius,2); area:=pi*power(rad_val.radius,2); insert into AREAS values (rad_val.radius, area); insert into AREAS values (rad_val.radius, area); close rad_cursor; close rad_cursor;END;/DECLARE Pi constant NUMBER(8,7) := 3.1415926; Pi constant NUMBER(8,7) := 3.1415926; area NUMBER(14,2); area NUMBER(14,2); cursor rad_cursor is select * from RAD_VALS; cursor rad_cursor is select * from RAD_VALS; rad_value rad_cursor%ROWTYPE; rad_value rad_cursor%ROWTYPE;BEGIN open rad_cursor; open rad_cursor; fetch rad_cursor into rad_val; fetch rad_cursor into rad_val; area:=pi*power(rad_val.radius,2); area:=pi*power(rad_val.radius,2); insert into AREAS values (rad_val.radius, area); insert into AREAS values (rad_val.radius, area); close rad_cursor; close rad_cursor;END;/ radius 3 6 8 Rad_curs or fetchfetch Rad_val RadiusArea AREAS 328.27 RAD_VALS

28 Explicit Cursor Attributes Obtain status information about a cursor. Attribute Type Description %ISOPEN Boolean Evaluates to TRUE if the cursor is open. %NOTFOUND Boolean Evaluates to TRUE if the most recent fetch does not return a row. %FOUND Boolean Evaluates to TRUE if the most recent fetch returns a row; complement of %NOTFOUND %ROWCOUNT Number Evaluates to the total number of rows returned so far.

29


Download ppt "Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs."

Similar presentations


Ads by Google