Skip to Main Content
  • Questions
  • plz explain follows trigger in oracle in simple examples

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, MA.

Asked: March 01, 2016 - 5:43 pm UTC

Last updated: March 02, 2016 - 7:07 am UTC

Version: 12c

Viewed 10K+ times! This question is

You Asked

hi tom plz explain follows trigger in oracle
using emp table


thanks&regards
-------------------

and Connor said...

In early Oracle releases, you could have 1 trigger per type of trigger per table (eg if you had a before-row-insert trigger, you could have only 1). Now there is no limit. So let's look at the following example:

SQL> drop table t1 purge;

Table dropped.

SQL>
SQL>
SQL> create table T1 ( x int, y int, z int );

Table created.

SQL>
SQL> create or replace
  2  trigger trg1
  3  before insert on t1
  4  for each row
  5  begin
  6    :new.y := :new.x;
  7  end;
  8  /

Trigger created.

SQL>
SQL>
SQL> create or replace
  2  trigger trg2
  3  before insert on t1
  4  for each row
  5  begin
  6    :new.z := :new.y;
  7  end;
  8  /

Trigger created.


Seems simple enough...Copy 'x' into 'y', and then copy 'y' into 'z'. So lets see what happens

SQL>
SQL> insert into t1 (x) values (1);

1 row created.

SQL>
SQL> select * from t1;

         X          Y          Z
---------- ---------- ----------
         1          1



Where did "Z" go ? What happened was TRG2 fired first, and then TRG1 fired. The order is *indeterminate*. We can use the FOLLOWS command to dictate the order in which triggers must fire.

SQL>
SQL> create or replace
  2  trigger trg2
  3  before insert on t1
  4  for each row
  5  follows trg1
  6  begin
  7    :new.z := :new.y;
  8  end;
  9  /

Trigger created.

SQL>
SQL> insert into t1 (x) values (1);

1 row created.

SQL>
SQL> select * from t1;

         X          Y          Z
---------- ---------- ----------
         1          1
         1          1          1

SQL>
SQL>


Hope this helps.










































































































































Rating

  (2 ratings)

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

Comments

Early release ?

Rajeshwaran, Jeyabal, March 02, 2016 - 5:17 am UTC

In early Oracle releases, you could have 1 trigger per type of trigger per table

Team - if you could provide the version of Oracle, that could be fine here, so that we could be aware(or test if possible) this is how triggers behave in that version of oracle or thing got changed from that version on-wards.
Connor McDonald
March 02, 2016 - 7:07 am UTC

I cant remember exactly, but somewhere around Oracle 7.3. We had to add support for more than 1 trigger per trigger type, so we could have our own snapshot triggers without interfering with those written by customers.

thanks it iv very help full for me

MA NAIDU, March 02, 2016 - 5:24 am UTC

hi tom
thanks for giving reply for questions
it iv very helpfull for me

Connor McDonald
March 02, 2016 - 7:07 am UTC

Glad we could help

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