'return false;'

Pages

Thursday, December 30, 2010

Triggers

                                                      Triggers*
Triggers are actions defined on tables.
1. Why are triggers required?
If we want to perform certain actions whenever an insert/update/delete happens on a table, we could achieve this by using a trigger. Remember, only these 3 DMLs - Insert/Update/delete could fire a trigger.
Truncate or drop or create or any other commands could not fire a trigger on a particular table.
2. Different types of Classification for Triggers:
First type of classification ::
a. Row Level Trigger:
For each row affected by the Insert/Update/Delete operation, the trigger is fired and thus the action associate with it.
Delete from emp where deptno=30; -- deletes 10 records
For eg: If you want to log all the Employee Ids if they gets deleted, you could create an After Delete Row Level Trigger such that for each ID getting deleted they could be inserted into an Audit Table. So, for the above delete operation the trigger is fired 10 times.
b. Statement Level Trigger:
The trigger will be fired only once for the Insert/Update/Delete operation happened on a table.
For eg:   Update emp set sal=sal+1050 where deptno=10;

This update statement updates 30 records in a single update operation. But if the Trigger on table Emp is statement level, the update action would fire the trigger only once!
Second type of classification :: Before and After triggers
Before Trigger : The trigger gets fired before an Insert/Update/Delete operation happens on the table
After Trigger : The trigger gets fired after an Insert/Update/Delete operation happens on the table
Based on the 2 types of classifications, we could have 12 types of triggers.
  •  Before Insert row level
  •  After Insert row level
  •  Before Delete row level
  •  After Delete row level
  •  Before Update row level
  •  After Update row level
  •  Before Insert Statement Level
  •  After Insert Statement Level
  •  Before Delete Statement Level
  •  After Delete Statement Level
  •  Before Update Statement Level
  •  After Update Statement Level

Frequently Asked Questions:
1.What is a Mutating table error (MTE)?
Oracle manages read consistent view of data. The error occurs when a row-level trigger accesses the same tables on which it is based while executing.
2. How to get rid of Mutating table error?
In the Row level trigger we cannot update, insert or select from the mutating table. So we create a package with a collection type in it. Then in the Row level trigger store the values (to be used) into the Collection type. Then add a Statement level trigger to the same table which calls the Package uses the stored data to do the data manipulation.
Eg: create package pack1
is
arr VARRAY(100);
end;
create or replace trigger row_trg1
before update on table1
for each row
begin
pack1.arr(0):= :old.col1;
---
---
end;
create or replace trigger stmt_trg1
before update on table1
declare
v_col number;
begin
v_col:= pack1.arr(0);
---

end;
3. Maximum Trigger size in Oracle : 32k (but functions, procedures etc could be called inside)
4. Can we create trigger on views?
No. But we can use INSTEAD OF Triggers
Eg: create or replace trigger trg_logon
Instead of update on view_v1
For each row
Begin
-- can call a procedure or a function too.
End;
/
5. Can we do commit or rollback in a trigger?
No
Exception : If the trigger is an AUTONOMOUS TRANSACTION we can commit or rollback
6. When a table or view is dropped, what happens to the associated triggers?
Triggers associated to the dropped Tables, Views also gets DROPPED
7. What is an INSTEAD OF Trigger in Oracle?
Views cannot have triggers. So, when someone tries to update a view, you can re-direct the data to the underlying table using an INSTEAD OF trigger.
Eg: create or replace trigger ins_trigger
INSTEAD OF update on emp_view
For each row
Begin
if :new.sal<:old.sal then
update emp set sal=:old.sal where empno=:new.empno;
else
update emp set sal=:new.sal where empno=:new.empno;
end if;
End;
*When INSTEAD OF trigger is written, even if the view is directly updateable, it updates using the trigger only.
8. Does Truncate on a table fires a Trigger?
No. A Truncate table operation wont fire the trigger even if it is defined as a delete trigger . Only a
delete operation could fire a trigger and when the Trigger is a Delete trigger .

No comments: