1

So I have table foo and I would like to delete other foo rows when trigger t_foo fires:

CREATE OR REPLACE TRIGGER "t_foo" AFTER INSERT OR DELETE OR UPDATE ON foo

/*delete some other records from foo that are not :NEW.* or :OLD.* \*

How would I go about doing this without getting a ORA-04091: table name is mutating, trigger/function may not see it. Is this even possible?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Shawn
  • 7,235
  • 6
  • 33
  • 45

3 Answers3

8

Why not do this in a stored procedure, where you can wrap the insert and deletes in a transaction, and can clearly document this side-effect behavior?

Tim
  • 5,371
  • 3
  • 32
  • 41
  • So you're saying put the delete logic in a stored procedure and call it from this trigger? – Shawn Dec 06 '10 at 14:07
  • No. Call the SP directly, to insert the row and do the side-effect deletes, wrapped in a transaction. The SP+transaction approach avoids the mess of mutating tables and is more self-documenting. – Tim Dec 06 '10 at 14:13
6

See Tom Kyte's definitive article on this topic.

Tony Andrews
  • 129,880
  • 21
  • 220
  • 259
2

This basically implies you have interdependent rows in your table, possibly a hierarchical structure with a self-reference from a column to the primary key. Did you think about ON DELETE CASCADE?

Benoit
  • 76,634
  • 23
  • 210
  • 236