Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:59:07+00:00 2026-06-01T12:59:07+00:00

This is a very common thing in web applications. If I have a user

  • 0

This is a very common thing in web applications. If I have a user table and I want to keep track of all the changes made to the user table, I can use a database insert and update triggers to save those changes in the user_history table.

But what if I have user_products table, where I have user_id , product_id, cost. When I add a user in the system lets say I have two products associated with that user. So my user_products table will have two rows for that user.

user_id product_id cost
1       10         1000
2       20         2000

Now if I go to the edit user page and delete product 1 , add product 3 , change cost for product 2 from 2000 to 3000.

so normally I delete all records for user_id 1 from user_product table and then do a new insert for the new products.

So its not a regular update but a delete and then insert. Hence I am not able to keep track of history changes.

Ideally I would want to know that I deleted product 1 , added product 3 , changed cost for product 2 from 2000 to 3000.

EDIT 1:-

I am not doing a update. I am doing a delete and then insert. So I am deleting record with product id 2 and cost 2000. And then again inserting record with prod id 2 but with cost 3000. So technically its delete and insert but logically only cost is changed from 2000 to 3000. If i check while executing both queries it will say i deleted product with id 2 and and then added product with id 2 which are same. But I want to be able to see that the cost has chnaged from 2000 to 3000

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:59:08+00:00Added an answer on June 1, 2026 at 12:59 pm

    One option would be to create a user_product_history table that is populated by triggers on user_product and then define a trigger that transforms the old ‘delete’ row in the history table into an update if the row is subsequently inserted.

    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 > 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;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very common scenario: displaying images in a ListView which have to
I'm sure this is a common problem for many web applications these days. What
I have some ASP.NET web services which all share a common helper class they
I have this very simple example that I am using to learn structs in
I think I have what must be a very common problem to solve and
So I know that there is a very similar question to this all over
I would like to include very common feature in my web application. I would
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
This very strange. When I resize a WinForms dialog the controls are flickering very
Given this very familiar model of prototypal construction: function Rectangle(w,h) { this.width = w;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.