菜鸟学堂:
在论坛中经常遇到变异表的问题,我收藏了一片有关变异表问题的文章,现在把他放在这里,希望能和大家分享!
avoiding mutating tables
ok, so you've just recieved the error:
ora-04091: table xxxx is mutating, trigger/function may not see it
and you want to get around that. this short article will describe and demonstrate the various methods of getting around the mutating table error.
if you are interested in why you are getting it and in what cases you will get it, please see the oracle server application developers guide (click here to read it right now -- this link is to technet.oracle.com. you need a password to access this site but you can get one right away for free).
avoiding the mutating table error is fairly easy. we must defer processing against the mutating or constrainng table until an after trigger. we will consider two cases:
hitting the ora-4091 in an insert trigger or an update trigger where you only need access to the :new values
hitting the ora-4091 in a delete trigger or an update trigger where you need to access the :old values
case 1 - you only need to access the :new values
this case is the simplest. what we will do is capture the rowids of the inserted or udpated rows. we can then use these rowids in an after trigger to query up the affected rows.
it always takes 3 triggers to work around the mutating table error. they are:
a before trigger to set the package state to a known, consistent state
an after, row level trigger to capture each rows changes
an after trigger to actually process the change.
as an example -- to show how to do this, we will attempt to answer the following question:
i have a table containing a key/status/effective date combination. when status
changes, the values are propagated by trigger to a log table recording the
status history. when no ri constraint is in place everything works fine.
when an ri trigger enforces a parent-child relationship, the status change
logging trigger fails because the parent table is mutating. propagating the
values to the log table implicitly generates a lookup back to the parent table
to ensure the ri constraint is satisfied.
i do not want to drop the ri constraint. i realize that the status is
denormalized. i want it that way. what is a good way to maintain the log?
here is the implementation:
sql> create table parent
2 ( thekey int primary key,
3 status varchar2(1),
4 effdate date
5 )
6 /
table created.
sql> create table log_table
2 ( thekey int references parent(thekey),
3 status varchar2(1),
4 effdate date
5 )
6 /
table created.
sql> rem this package is used to maintain our state. we will save the rowids of newly
sql> rem inserted / updated rows in this package. we declare 2 arrays -- one will
sql> rem hold our new rows rowids (newrows). the other is used to reset this array,
sql> rem it is an 'empty' array
sql> create or replace package state_pkg
2 as
3 type ridarray is table of rowid index by binary_integer;
4
4 newrows ridarray;
5 empty ridarray;
6 end;
7 /
package created.
sql> rem we must set the state of the above package to some known, consistent state
sql> rem before we being processing the row triggers. this trigger is mandatory,
sql> rem we *cannot* rely on the after trigger to reset the package state. this
sql> rem is because during a multi-row insert or update, the row trigger may fire
sql> rem but the after tirgger does not have to fire -- if the second row in an update
sql> rem fails due to some constraint error -- the row trigger will have fired 2 times
sql> rem but the after trigger (which we relied on to reset the package) will never fire.
sql> rem that would leave 2 erroneous rowids in the newrows array for the next insert/update
sql> rem to see. therefore, before the insert / update takes place, we 'reset'
sql> create or replace trigger parent_bi
2 before insert or update on parent
3 begin
4 state_pkg.newrows := state_pkg.empty;
5 end;
6 /
trigger created.
sql> rem this trigger simply captures the rowid of the affected row and
sql> rem saves it in the newrows array.
sql> create or replace trigger parent_aifer
2 after insert or update of status on parent for each row
3 begin
4 state_pkg.newrows( state_pkg.newrows.count+1 ) := :new.rowid;
5 end;
6 /
trigger created.
sql> rem this trigger processes the new rows. we simply loop over the newrows
sql> rem array processing each newly inserted/modified row in turn.
sql> create or replace trigger parent_ai
2 after insert or update of status on parent
3 begin
4 for i in 1 .. state_pkg.newrows.count loop
5 insert into log_table
6 select thekey, status, effdate
7 from parent where rowid = state_pkg.newrows(i);
8 end loop;
9 end;
10 /
trigger created.
sql> rem this demonstrates that we can process single and multi-row inserts/updates
sql> rem without failure (and can do it correctly)
sql> insert into parent values ( 1, 'a', sysdate-5 );
1 row created.
sql> insert into parent values ( 2, 'b', sysdate-4 );
1 row created.
sql> insert into parent values ( 3, 'c', sysdate-3 );
1 row created.
sql> insert into parent select thekey+6, status, effdate+1 from parent;
3 rows created.
sql> select * from log_table;
thekey s effdate
---------- - ---------
1 a 04-aug-99
2 b 05-aug-99
3 c 06-aug-99
7 a 05-aug-99
8 b 06-aug-99
9 c 07-aug-99
6 rows selected.
sql> update parent set status = chr( ascii(status)+1 ), effdate = sysdate;
6 rows updated.
sql> select * from log_table;
thekey s effdate
---------- - ---------
1 a 04-aug-99
2 b 05-aug-99
3 c 06-aug-99
7 a 05-aug-99
8 b 06-aug-99
9 c 07-aug-99
1 b 09-aug-99
2 c 09-aug-99
3 d 09-aug-99
7 b 09-aug-99
8 c 09-aug-99
9 d 09-aug-99
12 rows selected.
case 2 - you need to access the :old values
this one is a little more involved but the concept is the same. we'll save the actual old values in an array (as opposed to just the rowids of the new rows). using tables of records this is fairly straightforward. lets say we wanted to implement a flag delete of data -- that is, instead of actually deleting the record, you would like to set a date field to sysdate and keep the record in the table (but hide it from queries). we need to 'undo' the delete.
in oracle8.0 and up, we could use "instead of" triggers on a view to do this, but in 7.3 the implementation would look like this:
sql> rem this is the table we will be flag deleting from.
sql> rem no one will ever access this table directly, rather,
sql> rem they will perform all insert/update/delete/selects against
sql> rem a view on this table..
sql> create table delete_demo ( a int,
2 b date,
3 c varchar2(10),
4 hidden_date date default to_date( '01-01-0001', 'dd-mm-yyyy' ),
5 primary key(a,hidden_date) )
6 /
table created.
sql> rem this is our view. all dml will take place on the view, the table
sql> rem will not be touched.
sql> create or replace view delete_demo_view as
2 select a, b, c from delete_demo where hidden_date = to_date( '01-01-0001', 'dd-mm-yyyy' )
3 /
view created.
sql> grant all on delete_demo_view to public
2 /
grant succeeded.
sql> rem here is the state package again. this time the array is of
sql> rem table%rowtype -- not just a rowid
sql> create or replace package delete_demo_pkg
2 as
3 type array is table of delete_demo%rowtype index by binary_integer;
4
4 oldvals array;
5 empty array;
6 end;
7 /
package created.
sql> rem the reset trigger...
sql> create or replace trigger delete_demo_bd
2 before delete on delete_demo
3 begin
4 delete_demo_pkg.oldvals := delete_demo_pkg.empty;
5 end;
6 /
trigger created.
sql> rem here, instead of capturing the rowid, we must capture the before image
sql> rem of the row.
sql> rem we cannot really undo the delete here, we are just capturing the deleted
sql> rem data
sql> create or replace trigger delete_demo_bdfer
2 before delete on delete_demo
3 for each row
4 declare
5 i number default delete_demo_pkg.oldvals.count+1;
6 begin
7 delete_demo_pkg.oldvals(i).a := :old.a;
8 delete_demo_pkg.oldvals(i).b := :old.b;
9 delete_demo_pkg.oldvals(i).c := :old.c;
10 end;
11 /
trigger created.
sql> rem now, we can put the deleted data back into the table. we put sysdate
sql> rem in as the hidden_date field -- that shows us when the record was deleted.
sql> create or replace trigger delete_demo_ad
2 after delete on delete_demo
3 begin
4 for i in 1 .. delete_demo_pkg.oldvals.count loop
5 insert into delete_demo ( a, b, c, hidden_date )
6 values
7 ( delete_demo_pkg.oldvals(i).a, delete_demo_pkg.oldvals(i).b,
8 delete_demo_pkg.oldvals(i).c, sysdate );
9 end loop;
10 end;
11 /
trigger created.
sql> rem now, to show it at work...
sql> insert into delete_demo_view values ( 1, sysdate, 'hello' );
1 row created.
sql> insert into delete_demo_view values ( 2, sysdate, 'goodbye' );
1 row created.
sql> select * from delete_demo_view;
a b c
---------- --------- ----------
1 09-aug-99 hello
2 09-aug-99 goodbye
sql> delete from delete_demo_view;
2 rows deleted.
sql> select * from delete_demo_view;
no rows selected
sql> select * from delete_demo;
a b c hidden_da
---------- --------- ---------- ---------
1 09-aug-99 hello 09-aug-99
2 09-aug-99 goodbye 09-aug-99