Hi, I know that such questions were often raised, but I still can’t fully understand this topic. Upade table on which there is a trigger
We have 2 tables:
Sale_income :
| item |income |create_user |create_date |last_update_user | update_time| |------|-------|------------|-------------------|-----------------|------------| | 1 | 100 |duck |05-19-2016 |human |05-19-2016 | | 2 | 250 |dog |05-19-2016 |human |05-19-2016 | | 3 | 210 |cat |05-20-2016 |human |05-19-2016 |Sale_income_audit :
| item |change_id|last_inc|new_inc |user_update|update_date|operation | |------|---------|--------|----------|-----------|-----------|------------| | 1 | 1 |null |05-19-2016|duck |05-19-2016 |I | | 2 | 2 |null |05-19-2016|dog |05-19-2016 |I | | 3 | 33 |null |05-20-2016|cat |05-19-2016 |I |
When someone makes an INSERT , DELETE or UPDATE the Sale_income table, the corresponding field is added to the Sale_income_audit table with the corresponding operation ( operation column). But at the same time, the fields in the main table ( last_update ) should also change. And here we meet the problem of the mutated table.
I decided this as follows: I created a package with three variables and two triggers. In the trigger on the Sale_income update, the time and user data are entered into variables. And after the update in another trigger, the required fields are updated.
Package:
create or replace package Sale_income_var as v_old_income BINARY_INTEGER := null; v_new_income BINARY_INTEGER := null; v_item BINARY_INTEGER := null; END Sale_income_var; Triggers:
The first:
create or replace trigger audit_income_IUD after insert or update or delete on Sale_income for each row begin . . elsif updating then Sale_income_var.v_old_income := :old.income; Sale_income_var.v_new_income := :new.income; if Sale_income_var.v_item is null then Sale_income_var.v_item := :old.item; DBMS_OUTPUT.PUT_LINE(Sale_income_var.v_item); end if; . . end if; end audit_income_IUD;Second:
create or replace trigger sale_income_au after update of income on Sale_income begin update Sale_income set last_update_user = user, last_update_date = sysdate where item = Sale_income_var.v_item; INSERT into Sale_income_audit (item, change_id, last_income, new_income, user_update, update_date, operation) VALUES (Sale_income_var.v_item, auto_incr.NEXTVAL, Sale_income_var.v_old_income, Sale_income_var.v_new_income, user, sysdate, 'U'); Sale_income_var.v_item := null; end sale_income_au;
It works, only I feel that this is not the right decision. And this magic with variables is not good either, am I right?
What would you fix here and how would you get out of this situation? Thanks for attention :)