My tables:
TableA (id number, state number) TableB (id number, tableAId number, state number) TableC (id number, tableBId number, state number)
Thus, the elements in TableC are children of TableB, and the elements in TableB are children of TableA. Conversely, the elements in Table A are the parents of TableB, and the elements in TableB are the parents of TableC.
I would like to monitor the state of the parent elements ... let's say, for example, that we have this data:
TableA (id, state): 1, 40 TableB (id, tableAId, state): 1, 1, 40 2, 1, 60 TableC (id, tableBId, state): 1, 1, 40 2, 1, 50 3, 2, 60 4, 2, 70
Parental status should always have the smallest state of their children. Therefore, if we now update TableC as follows:
update TableC set state = 50 where Id = 1;
my trigger should automatically update TableB (set state = 50, where id = 1) and then update also TableA (set state = 50, where id = 1)
I would like to do this using triggers (AFTER UPDATE, INSERT, DELETE) in TableA, TableB, TableC), so that after each action these steps are performed:
- get parent id
- find the smallest state of all children of the current parent
- if the smallest state of all children is greater than the parent state, then update the parent
How can I avoid the "mutating table" error? Is autonomous transaction usage maintained in this example? I saw several opinions that a mutating table error indicates flaws in the application logic - this is true and how can I change my logic to prevent this error?
thanks
EDIT: Thanks for all the great answers!
In the end, I used triggers (thanks to Vincent Malgrat who pointed out an article by Tom Keith).
EDIT: In REAL END, I used stored procedures and remote triggers :)