in oracle. a pl/sql block stored in the database and fired in response to a specified event ◦ dml...
TRANSCRIPT
TriggersIn Oracle
A PL/SQL block stored in the database and fired in response to a specified event◦ DML statements : insert , update, delete◦ DDL statements : create, alter, drop◦ Startup or shutdown◦ An error◦ A user logon or logoff
What is a trigger?
Create or replace trigger <trigger_name>{before|after|Instead of}{Insert | Update | Delete}[of <col_name>]On <table_name>[referencing old as o new as n][for each row]When (<condition>)BeginSql statementsEnd;
Syntax for creating a trigger
Triggering SQL statement Trigger action Trigger restriction
Components of a trigger
DML triggers◦ Defined on a table and fired in response to an
insert, delete or update DDL triggers
◦ Defined in response to events such as logon or logoff, create alter drop etc.
Instead of triggers◦ Used for non updateable views
Types of triggers
DML trigger definition consists of:◦ Event that fires the trigger◦ Database table on which the event occurs◦ Optional condition controlling when trigger
executes Types:
◦ Row level Trigger fired or each row updated , inserted or
deleted◦ Statement level
Trigger fired for each sql statement executed
DML Triggers
create or replace trigger before_update_empbefore update on empbegininsert into empcheck values('Before update','Statement', sysdate);dbms_output.put_line('Some updation done on Emp');end;/Update emp set sal=sal+100;Some updation done on emp14 rows updatedSelect * from empcheck;EVENT LEVEL_CHECK DATE_CHEC-------------------- -------------------------------- ------------------------------Before update Statement 13-SEP-13
Statement level before update
create or replace trigger before_update_row_emp_salbefore update of sal on empFor each rowbegininsert into empcheck values('Before update',’Row', sysdate);dbms_output.put_line('Some updation done on Emp Sal column');end;/SQL> update emp set sal=sal+10 where deptno=10;Some updation done on EmpSome updation done on Emp Sal columnSome updation done on Emp Sal columnSome updation done on Emp Sal column
3 rows updated.
SQL> select * from empcheck;
EVENT LEVEL_CHECK DATE_CHEC-------------------- ------------------------------ ---------Before update Statement 13-SEP-13Before update Row 13-SEP-13Before update Row 13-SEP-13Before update Row 13-SEP-13
Row level before update
create or replace trigger display_salary_changesbefore delete or update or insert on empfor each rowdeclaresal_diff number;beginsal_diff:=:new.sal-:old.sal;dbms_output.put_line('Old salary: '||:old.sal);dbms_output.put_line('New salary: '||:new.sal);dbms_output.put_line('Salary Difference: '||sal_diff);end;/
Using :old and :new
create trigger trig_alter_scottbefore alter on scott.schemabeginraise_application_error(-20000,'Cannot alter
any table in scott');end;/alter table emp drop primary key;ERROR at line 1:ORA-00604: error occurred at recursive SQL level 1ORA-20000: Cannot alter any table in scott
DDL trigger
Help to make non updateable views updateable
Called so because unlike other types of triggers, oracle fires the trigger instead of executing the triggering statement
Exist only on views
Instead of Triggers
Create a viewcreate or replace view v as select empno,ename ,sal from emp
where sal>2000;Create a trigger(Instead of) create or replace trigger emp_triginstead of insert on vbeginif :new.sal >2000 theninsert into emp (empno,ename,sal)
values(:new.empno,:new.ename,:new.sal);elseraise_application_error(-20001,'Insertion from this view
invalid');end if;end;/
Instead of Triggers
Creation of a non updateable viewCreate view vtest as select deptno,sum(sal)
as total from emp group by deptno; Updation through the view not possibleupdate vtest set deptno=20 where
deptno=10;ORA-01732: data manipulation operation not
legal on this view
Instead of trigger example
Creation of Instead of triggercreate or replace trigger emp_trig2 instead of update on vtest begin if updating then update emp set deptno=:new.deptno; end if; end; /Updation using triggerupdate vtest set deptno=20 where deptno=10;1 row updated.
Instead of trigger
Mutating table exceptions occur triggering table is referenced from within the row level trigger code
This error is raised when a trigger attempts to execute a DML statement on the same table that the trigger is defined on
Triggers and Mutating table
create or replace trigger trig_up_empafter update on empfor each rowdeclarei number;beginselect sum(sal) into i from emp;dbms_output.put_line('The sum of salary of employees is '||i);end;/ERROR at line 1:ORA-04091: table SCOTT.EMP is mutating, trigger/function may not
see itORA-06512: at "SCOTT.TRIG_UP_EMP", line 4ORA-04088: error during execution of trigger 'SCOTT.TRIG_UP_EMP'
Triggers and Mutating table
create or replace trigger trig_up_emp after update on emp for each row declare i number; pragma autonomous_transaction; begin select sum(sal) into i from emp; dbms_output.put_line('The sum of salary of employees is
'||i); commit; end; /
Solving Mutating table error
View ALL_TRIGGERS, USER_TRIGGERS
Alter trigger tigger_name enable;Alter trigger tigger_name disable;
Drop trigger trigger_name;
Alter table table_name disable all triggers;Alter table table_name enable all triggers;
Listing, Enabling,Disabling,Dropping
Refers to SQL statements that are constructed and executed at run time
Execute DDL statements Manage Ad hoc query and update requirements of
web based applications
create or replace procedure dynsql_eg(ddl_string varchar2)is beginexecute immediate ddl_string;
end;
Dynamic SQL