Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One option would be to create a <code>user_product_history</code> table that is populated by triggers on <code>user_product</code> and then define a trigger that transforms the old 'delete' row in the history table into an <code>update</code> if the row is subsequently inserted.</p> <pre><code>CREATE TABLE user_product_history ( user_id number, product_id number, cost number, operation_type varchar2(1), operation_date date ); CREATE TRIGGER trg_user_product_history AFTER INSERT OR UPDATE OR DELETE ON user_product FOR EACH ROW DECLARE l_cnt integer; BEGIN IF( deleting ) THEN insert into user_product_history( user_id, product_id, cost, operation_type, operation_date ) values( :old.user_id, :old.product_id, :old.cost, 'D', sysdate ); ELSIF( updating ) THEN insert into user_product_history( user_id, product_id, cost, operation_type, operation_date ) values( :new.user_id, :new.product_id, :new.cost, 'U', sysdate ); ELSIF( inserting ) THEN select count(*) into l_cnt from user_product_history where operation_type = 'D' and user_id = :new.user_id and product_id = :new.product_id; if( l_cnt &gt; 0 ) then update user_product_history set operation_type = 'U', operation_date = sysdate, cost = :new.cost where operation_type = 'D' and user_id = :new.user_id and product_id = :new.product_id; else insert into user_product_history( user_id, product_id, cost, operation_type, operation_date ) values( :new.user_id, :new.product_id, :new.cost, 'I', sysdate ); end if; END IF; END; </code></pre> <p>From an efficiency standpoint, however, doing deletes and inserts rather than updates is going to mean that you're putting far more load on your database than is necessary. You'll do substantially more I/O than necessary. And you'll end up with much more complicated code for handling changes. You'll almost certainly be better served figuring out what has changed and then just updating those rows.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload