Trigger created.
Now, there are many things wrong with this trigger. But the firstobvious clue that something was seriously wrong was their need to use anautonomous transaction. They did that because without it, an update wouldproduce the following result:
SQL< update currencies
2 set primary_currency = 'Y';
update currencies
*
ERROR at line 1:
ORA-04091: table OPS$TKYTE.CURRENCIES Is mutating, trigger/functionmay
not see it
ORA-06512: at "OPS$TKYTE.CURRENCIES_TRIGGER”, line 4
ORA-04088: error during execution of
trigger 'OPS$TKYTE.CURRENCIES_TRIGGER'
That is not really an error but more of a warning. Basically it issaying, “You are doing something so fundamentally wrong in your trigger thatOracle Database is just not going to permit you to do that.” If the database allowedthe trigger to read the table it was defined on, as the update was proceeding,the trigger would see the table partially updated. If five rows were beingupdated, the row trigger would see the table with one of the rows modified,then two, then three, and so on. It would see the table in a manner in whichthe table never should be seen.
By way of example, suppose the CURRENCIES table, above, was in placewith the sample data provided in the question and the trigger was permitted toread the table while it was changing. Now issue the following command:
update currencies
set primary_currency =
decode(currency, 'USD','N', 'USN', 'Y')
where country = 'US'
and currency in ( 'USD', 'USN');
That should be OK, because it moves the primary currency flag fromUSD to USN. After the statement finishes, there will be only one primarycurrency row. But what if the rows get updated in order of first USN and thenUSD. Well, when the trigger fires the first time, it will see USN withPRIMARY_CURRENCY=‘Y’ and USD with PRIMARY_CURRENCY=‘Y’. The trigger will failthe statement, but the statement is supposed to succeed. On the other hand,what if the data is processed in the order of first USD and then USN? In thatcase, the trigger will fire and find zero PRIMARY_CURRENCY=‘Y’ rows and thenfire again, see only one, and be done.
So, for this trigger, the update will work for some data, sometimes.For other bits of data, the update will not work, sometimes. Two databases withthe same data will fail on different sets of rows and succeed on others. Itwill all depend on how the data happens to be organized on disk and in whatorder it is processed. And that is unacceptable (not to mention veryconfusing).
That, in a nutshell, is why the mutating table constraint exists: toprotect us from ourselves. But unfortunately, the developers asking thisquestion found a way around the mutating table constraint: the autonomoustransaction. That “feature” permits developers to query the table the triggeris firing on yet query it as if it were in another session/transactionaltogether. The trigger will not see its own modifications to the table, andthat is the huge flaw with this thinking: the trigger is attempting to validatethe very modifications it cannot see. The trigger’s sole purpose is to validatethe modifications to the data, but the trigger is reading the data before themodifications take place. It therefore cannot work!
Note that in Oracle Database 11g, there is a newfeature, the compound trigger that can be used to solve this issue.
