2. 2 Triggers A trigger is a stored procedure that defines an action that the database automatically initiates when some database related event such as INSERT , UPDATE or DELETE occurs. when occurs fires a The main difference between a procedure and a trigger is that a procedure is executed explicitly from another block via a procedure call with arguments, while a trigger is executed implicitly when the triggering event like the occurrence of a DML-statement as INSERT ,UPDATE or DELETE occurs. Also a trigger does not accept arguments. The Automatic execution of triggers is known as firing of trigger.
3. 3 Need and Types of Triggers Need for triggers: Maintaining complex integrity constraints. Auditing the information in a table by recording the change. Automatically signaling another program about an occurred event. Enforcing complex business rules.
4. 4 Need and Types of Triggers Types of triggers: Row trigger: Fired once for every row that is affected. Statement trigger : Fired only once per statement regardless of the number of row affected. Before trigger: Fired before the triggering event occurs. After trigger: Fired after the triggering event occurs.
5. 5 Syntax for Triggers SYNTAX : CREATE OR REPLACE TRIGGER trigger_name {BEFORE / AFTER } { INSERT / UPDATE / DELETE} ON object_name FOR { EACH ROW / EACH STATEMENT} BEGIN Statement1… ….. …... END;
6. 6 Example for triggers SYNTAX : CREATE OR REPLACE TRIGGER trig AFTER DELETE ON InfoTable FOR EACH ROW BEGIN INSERT INTO InfoTable VALUES(:old name,NULL,NULL); END; Using this trigger , whenever a row from the InfoTable will be deleted the trigger will be fired and the row containing the deleted row’s name will be inserted with other values as NULL which will help us identify later which rows were deleted.
7. 7 NEED FOR CURSORS We cannot use sub-programs of PL/SQL with a simple select statement to retrieve more than one row. If a select statement in a procedure returns more than one row ,Oracle returns an error message since PL/SQL requires a special compatibility to retrieve and process more than one row. Oracle processes procedure without error Oracle returns error. SELECT statement in procedure returning one row row SELECT statement in procedure returning more than one row row row row
8. 8 CURSOR A PL/SQL cursor is a mechanism that provides a way to select multiple rows of data from the database and then process each row individually inside a PL/SQL program. A cursor is basically an area of memory containing SQL statements and information for processing those statements. A CURSOR is a pointer to the context area. CURSOR is pointing at this row1 CURSOR is pointing at this row2 now The cursor first points at row1 and once it is processed it then advances to row2 and so on. row1 row1 row2 row2 row3 row3 row4 row4
9. 9 Types of CURSORS There are two types of cursors: Implicit cursor: Created and used by Oracle for all DML and PL/SQL select statements including those returning only one row. Explicit cursor : Created and used by the programmer for queries that return more than one row.
10. 10 Types of CURSORS SYNTAX: DECLARE CURSOR cursor_name AS Cursor Declaration SELECT statement; -- Variable declaration if any; BEGIN OPEN cursor_name; FETCH <cursor_name> INTO <record_list> Cursor Body -- data fetched into active data set. CLOSE cursor_name; END;
11. 11 EXAMPLE OF CURSOR DECLARE CURSOR cus AS SELECT * FROM InfoTable; customer InfoTable%ROWTYPE; declare a rowtype variable BEGIN OPEN cus; LOOP FETCH cus into customer; Fetch the row into the variable and advance cursor. DBMS_OUTPUT.PUT_LINE(‘name:’||customer.name); DBMS_OUTPUT.PUT_LINE(‘age:’||customer.age); DBMS_OUTPUT.PUT_LINE(‘phone:’||customer.phone); EXIT WHEN cus%NOTFOUND; Returns true when all rows have been fetched. END LOOP; CLOSE cus; END;
12. THANK YOU 12 THANK YOU FOR VIEWING THIS PRESENTATION FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING , please visit: www.dataminingtools.net