Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular...
-
Upload
rolf-preston -
Category
Documents
-
view
215 -
download
0
Transcript of Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular...
![Page 1: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/1.jpg)
TriggerTriggerOracle PL/SQLOracle PL/SQL
![Page 2: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/2.jpg)
TriggersTriggers
Associated with a particular tableAssociated with a particular table Automatically executed when a Automatically executed when a
particular event occursparticular event occurs• InsertInsert• UpdateUpdate• DeleteDelete• OthersOthers
![Page 3: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/3.jpg)
Triggers vs. ProceduresTriggers vs. Procedures
Procedures are Procedures are explicitlyexplicitly executed by executed by a user or applicationa user or application
Triggers are Triggers are implicitlyimplicitly executed executed (fired) when the triggering event (fired) when the triggering event occursoccurs
Triggers should not be used as a lazy Triggers should not be used as a lazy way to invoke a procedure as they way to invoke a procedure as they are fired are fired everyevery time the event occurs time the event occurs
![Page 4: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/4.jpg)
TriggersTriggers
![Page 5: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/5.jpg)
TriggersTriggers
The The trigger specificationtrigger specification names the names the trigger and indicates when it will firetrigger and indicates when it will fire
The The trigger bodytrigger body contains the PL/SQL contains the PL/SQL code to accomplish whatever task(s) code to accomplish whatever task(s) need to be performedneed to be performed
![Page 6: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/6.jpg)
TriggersTriggers
![Page 7: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/7.jpg)
Triggers TimingTriggers Timing
A triggers timing has to be specified A triggers timing has to be specified firstfirst• Before (most common)Before (most common)
Trigger should be fired before the operationTrigger should be fired before the operation• i.e. before an inserti.e. before an insert
• AfterAfter Trigger should be fired after the operationTrigger should be fired after the operation
• i.e. after a delete is performed i.e. after a delete is performed
![Page 8: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/8.jpg)
Trigger EventsTrigger Events
Three types of events are availableThree types of events are available• DML eventsDML events• DDL eventsDDL events• Database eventsDatabase events
![Page 9: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/9.jpg)
DML EventsDML Events
Changes to data in a tableChanges to data in a table• InsertInsert• UpdateUpdate• DeleteDelete
![Page 10: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/10.jpg)
DDL EventsDDL Events
Changes to the definition of objectsChanges to the definition of objects• TablesTables• IndexesIndexes• ProceduresProcedures• FunctionsFunctions• OthersOthers
Include CREATE, ALTER and DROP Include CREATE, ALTER and DROP statements on these objectsstatements on these objects
![Page 11: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/11.jpg)
Database EventsDatabase Events
Server ErrorsServer Errors Users Log On or OffUsers Log On or Off Database Started or StoppedDatabase Started or Stopped
![Page 12: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/12.jpg)
Trigger DML EventsTrigger DML Events
Can specify one or more events in Can specify one or more events in the specificationthe specification• i.e. INSERT OR UPDATE OR DELETEi.e. INSERT OR UPDATE OR DELETE
Can specify one or more columns to Can specify one or more columns to be associated with a type of eventbe associated with a type of event• i.e. BEFORE UPDATE OF SID OR SNAMEi.e. BEFORE UPDATE OF SID OR SNAME
![Page 13: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/13.jpg)
Table NameTable Name
The next item in the trigger is the The next item in the trigger is the name of the table to be affectedname of the table to be affected
![Page 14: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/14.jpg)
Trigger LevelTrigger Level
Two levels for TriggersTwo levels for Triggers• Row-level triggerRow-level trigger
Requires FOR EACH ROW clauseRequires FOR EACH ROW clause• If operation affects multiple rows, trigger fires If operation affects multiple rows, trigger fires
once for each row affectedonce 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 DDL and Database triggers should not
be row-levelbe row-level
![Page 15: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/15.jpg)
Event ExamplesEvent Examples
![Page 16: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/16.jpg)
TriggersTriggers
Conditions Available So Multiple Conditions Available So Multiple Operations Can Be Dealt With In Operations Can Be Dealt With In Same TriggerSame Trigger• Inserting, Updating, DeletingInserting, Updating, Deleting
Column Prefixes Allow Identification Column Prefixes Allow Identification Of Value ChangesOf Value Changes• New, OldNew, Old
![Page 17: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/17.jpg)
Triggers ExceptionsTriggers Exceptions
EXCEPTION Data Type Allows Custom EXCEPTION Data Type Allows Custom ExceptionsExceptions
RAISE Allows An Exception To Be RAISE Allows An Exception To Be Manually OccurManually Occur
RAISE_APPLICATION_ERROR Allows RAISE_APPLICATION_ERROR Allows Termination Using A Custom Error Termination Using A Custom Error MessageMessage• Must Be Between -20000 and -20999Must Be Between -20000 and -20999• Message Can Be Up to 512 BytesMessage Can Be Up to 512 Bytes
![Page 18: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/18.jpg)
ENDEND
![Page 19: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/19.jpg)
CursorsCursors
Cursors Hold Result of an SQL Cursors Hold Result of an SQL StatementStatement
Two Types of Cursors in PL/SQLTwo Types of Cursors in PL/SQL• Implicit – Automatically Created When a Implicit – Automatically Created When a
Query or Manipulation is for a Single Query or Manipulation is for a Single RowRow
• Explicit – Must Be Declared by the UserExplicit – Must Be Declared by the User Creates a Unit of Storage Called a Result SetCreates a Unit of Storage Called a Result Set
![Page 20: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/20.jpg)
CursorsCursors
Result SetResult Set
MIS380 MIS380 DATABASE DESIGNDATABASE DESIGN 44MIS202 MIS202 INFORMATION SYSTEMS INFORMATION SYSTEMS 3 3
<Cursor<Cursor
MIS485 MIS485 MANAGING TECHNOLOGYMANAGING TECHNOLOGY 44
MIS480 MIS480 ADVANCED DATABASE ADVANCED DATABASE 44
![Page 21: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/21.jpg)
CursorsCursors
Declaring an Explicit CursorDeclaring an Explicit Cursor
CURSOR CursorName IS SelectStatement;CURSOR CursorName IS SelectStatement; Opening an Explicit CursorOpening an Explicit Cursor
OPEN CursorName;OPEN CursorName; Accessing Rows from an Explicit Accessing Rows from an Explicit
CursorCursor
FETCH CursorName INTO RowVariables;FETCH CursorName INTO RowVariables;
![Page 22: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/22.jpg)
CursorsCursors
Declaring Variables of the Proper Declaring Variables of the Proper Type with %TYPEType with %TYPEVarName TableName.FieldName%TYPE;VarName TableName.FieldName%TYPE;
Declaring Variables to Hold An Entire Declaring Variables to Hold An Entire RowRowVarName CursorName%ROWTYPE;VarName CursorName%ROWTYPE;
Releasing the Storage Area Used by Releasing the Storage Area Used by an Explicit Cursoran Explicit CursorCLOSE CursorName;CLOSE CursorName;
![Page 23: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/23.jpg)
Iterative StructuresIterative Structures
LOOP … EXIT … END LOOPLOOP … EXIT … END LOOP• EXIT with an If Avoids Infinite LoopEXIT with an If Avoids Infinite Loop
LOOP … EXIT WHEN … END LOOPLOOP … EXIT WHEN … END LOOP• Do Not Need An If to Control EXITDo Not Need An If to Control EXIT
WHILE … LOOP … END LOOPWHILE … LOOP … END LOOP• Eliminates Need for EXITEliminates Need for EXIT
FOR … IN … END LOOPFOR … IN … END LOOP• Eliminates Need for Initialization of Eliminates Need for Initialization of
CounterCounter
![Page 24: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/24.jpg)
Cursor Control With LoopsCursor Control With Loops
Need a Way to Fetch RepetitivelyNeed a Way to Fetch Repetitively Need a Way to Determine How Many Need a Way to Determine How Many
Rows to Process With a CursorRows to Process With a Cursor• Cursor AttributesCursor Attributes
CursorName%ROWCOUNT – Number of CursorName%ROWCOUNT – Number of Rows in a Result SetRows in a Result Set
CursorName%FOUND – True if a Fetch CursorName%FOUND – True if a Fetch Returns a RowReturns a Row
CursorName%NOTFOUND – True if CursorName%NOTFOUND – True if Fetch Goes Past Last RowFetch Goes Past Last Row
![Page 25: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/25.jpg)
Cursor For LoopCursor For Loop
Processing an Entire Result Set Processing an Entire Result Set CommonCommon
Special Form of FOR … IN to Manage Special Form of FOR … IN to Manage CursorsCursors
No Need for Separate OPEN, FETCH No Need for Separate OPEN, FETCH and CLOSE statementsand CLOSE statements
Requires %ROWTYPE VariableRequires %ROWTYPE Variable
![Page 26: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/26.jpg)
Creating a CursorCreating a Cursor We create a Cursor when we want to go We create a Cursor when we want to go
over a result of a query over a result of a query (like (like ResultSetResultSet in JDBC) in JDBC) Syntax Example:Syntax Example:
DECLAREDECLARE cursor c iscursor c is select * from sailors; select * from sailors;
sailorData sailors%ROWTYPE;sailorData sailors%ROWTYPE; BEGINBEGIN open c;open c; fetch c intofetch c into sailorData; sailorData;
sailorData is a variable that can hold a ROW from the sailors table
Here the first row of sailors is inserted into sailorData
![Page 27: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/27.jpg)
ExampleExample
DECLAREDECLARE Pi constant NUMBER(8,7) := 3.1415926;Pi constant NUMBER(8,7) := 3.1415926; area NUMBER(14,2);area NUMBER(14,2); cursorcursor rad_cursorrad_cursor isis select * from RAD_VALS;select * from RAD_VALS; rad_value rad_cursor%ROWTYPE;rad_value rad_cursor%ROWTYPE;
BEGINBEGIN open rad_cursoropen rad_cursor;; fetch rad_cursorfetch rad_cursor into rad_valinto rad_val;; area:=pi*power(rad_val.radius,2);area:=pi*power(rad_val.radius,2); insert into AREAS values (rad_val.radius, insert into AREAS values (rad_val.radius,
area);area); close rad_cursor;close rad_cursor;END;END;//
DECLAREDECLARE Pi constant NUMBER(8,7) := 3.1415926;Pi constant NUMBER(8,7) := 3.1415926; area NUMBER(14,2);area NUMBER(14,2); cursorcursor rad_cursorrad_cursor isis select * from RAD_VALS;select * from RAD_VALS; rad_value rad_cursor%ROWTYPE;rad_value rad_cursor%ROWTYPE;
BEGINBEGIN open rad_cursoropen rad_cursor;; fetch rad_cursorfetch rad_cursor into rad_valinto rad_val;; area:=pi*power(rad_val.radius,2);area:=pi*power(rad_val.radius,2); insert into AREAS values (rad_val.radius, insert into AREAS values (rad_val.radius,
area);area); close rad_cursor;close rad_cursor;END;END;//
radius
3
6
8
Rad_cursor
fetch
Rad_val
Radius AreaAREAS
3 28.27
RAD_VALS
![Page 28: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/28.jpg)
Explicit Cursor AttributesExplicit Cursor Attributes
Obtain status information about a Obtain status information about a cursor.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.
![Page 29: Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.](https://reader038.fdocuments.in/reader038/viewer/2022103100/56649f1a5503460f94c2f95d/html5/thumbnails/29.jpg)