Skip to Main Content
  • Questions
  • Duplicate Triggers [not Trigger Names]

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Sree.

Asked: February 01, 2011 - 11:31 am UTC

Last updated: February 03, 2011 - 3:30 pm UTC

Version: 10g

Viewed 1000+ times

You Asked

 Tom, I am puzzled by Oracle behavior of triggers. I have created two same triggers on DEPT table and surprisingly they got compiled without errors. okay fine. then when I ran the update statement, it ran trigger 2 first and then trigger 1. 
So, how come it allows two same triggers? what is the use of that?
I also noticed that Oracle executing triggers in random order and not the order of creation [not that it is that important]

I was guessing Oracle will fail on second trigger while compiling but it did not.


SQL> desc dept;

 Name                Null?    Type
 ------              ------    ------
 DEPTNO             NOT NULL NUMBER(38)
 DNAME                       VARCHAR2(40)
 LOC                         VARCHAR2(40)


SQL> select trigger_name from user_triggers where table_name='DEPT';

no rows selected
                                                                                                                                                                                                                                                                                                                                                                                
SQL> select * from dept;

    DEPTNO DNAME                                    LOC                                                                 ---------- ---------------------------------------- ----------------------------------------                                    10 DepartmentName10                         Frankfort10                                                                 20 DepartmentName20                         Frankfort20                                                                 30 DepartmentName30                         Frankfort30                                                                 40 DepartmentName40                         Frankfort40                                                                 90 Finance                                  Lex                                                                        100 Finance                                  Lex                                                                        101 Finance                                  Lex                                                                                                                                                 

7 rows selected.


SQL>  create or replace trigger tr_bu_test1
  2  before update on dept for each row
  3  begin :new.dname:='from trigger2'; dbms_output.put_line('trigger 2'); end;
  4  /

Trigger created.

SQL> create or replace trigger tr_bu_test
  2  before update on dept for each row
  3   begin :new.dname:='from trigger1'; dbms_output.put_line('trigger 1'); end;
  4  /

Trigger created.

SQL> 
SQL> 
SQL> set serveroutput on;
SQL> update dept set dname='my name' where deptno=100;
trigger 2                                                                                                               trigger 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
1 row updated.

SQL> commit;

Commit complete.

SQL> select * from dept;

    DEPTNO DNAME                                    LOC                                                                 ---------- ---------------------------------------- ----------------------------------------                                    10 DepartmentName10                         Frankfort10                                                                 20 DepartmentName20                         Frankfort20                                                                 30 DepartmentName30                         Frankfort30                                                                 40 DepartmentName40                         Frankfort40                                                                 90 Finance                                  Lex                                                                        100 from trigger1                            Lex                                                                        101 Finance                                  Lex                                                                 
7 rows selected.



and Tom said...

We have supported multiple same type triggers since Oracle 8.0 at least... Originally in Oracle - when you used replication or just created a materialized view log (we called them snapshot logs back then) on a table - we would create a trigger to populate that materialized view log.

Since we created a trigger (after modification, for each row as I recall) - that made it so that YOU COULD NOT. We used the trigger up. So, in order to allow you to create the full set of triggers AND allow us to create a trigger as well - we started supporting multiple same type triggers. It's been that way for a long long time.

Until Oracle 11g - the order of trigger firing was not set in stone, we were free to fire them in any order we felt like, you could NOT depend on the order. Starting in 11g, you can tell us what order to fire them in if you like

http://docs.oracle.com/cd/E11882_01/appdev.112/e17126/create_trigger.htm#CJAEJAFB

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Duplicate Triggers

Sree, February 02, 2011 - 9:24 am UTC

Wow! that seems like an horrible solution from Oracle[??] If your trigger fired before my trigger, and MV logs were updated or refreshed before my trigger fired, that MV doesn't have uptodate data then. Seems so wrong..but may be I'm missing a point.

Anyways, Oralce 11G got the order clause atleast :-)

Thanks foryour response.
Tom Kyte
February 02, 2011 - 10:16 am UTC

It would not work that way - the triggers are an AFTER FOR EACH ROW trigger

By the time you get to the after triggers - the data for that row is stabilized, you can only modify the data in the BEFORE FOR EACH ROW trigger.


It worked as designed.

Duplicate triggers

Sree, February 02, 2011 - 4:03 pm UTC

Yes, you are correct. After update would have all the changes by then..missed it.
But, thanks for the explanation

Duplicate triggers

Ravi, February 03, 2011 - 5:08 am UTC

Hi ,
Order of trigger fire is the same order as it created, i had check the same example given in this question .

It is working in the order of trigger creation in oracle 10g.

Tom Kyte
February 03, 2011 - 3:30 pm UTC

unless you can show me documentation that states it is a support fact that they fire in creation order - you better NOT rely on it.

Until 11g you must assume the firing order is random and not deterministic.

All it takes is one application bug fix, one administrative operation, anything - and the order you relied on will change. Do not rely on side effects.



More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library