creating a trigger that updates a table when a column in a different table is updated

戏子无情 提交于 2019-12-11 14:32:36

问题


For some reason I'm having a hard time fully understanding triggers. For my homework assignment I need to create a table that holds product id, total sales, and total quantity sold for each product (these columns are already in two different tables). Then I create a trigger that updates this table when the orderplaced column from a different table is updated to 1. Not exactly sure where to start. Since the table I created is empty would I do an UPDATE table as the assignment suggests or an INSERT since the columns are empty? If anyone can put me in the right direction I would really appreciate it..

CREATE TABLE bb_sales_sum (
    idProduct number(2) NOT NULL,
    total number(6,2),
    quantity number); 

CREATE OR REPLACE TRIGGER BB_SALESUM_TRG
    AFTER UPDATE OF orderplaced on bb_basket 
    FOR EACH ROW
    WHEN (NEW.orderplaced = 1)
DECLARE 
    lv_count Number;
BEGIN   
    if :new.orderplaced = 1 then 
        for item in 
            (select idproduct, (quantity * price) AS total, quantity
            from bb_basketitem
            where idbasket = :old.idbasket)
    loop
        select count(*)
        into lv_count
        from bb_sales_sum where idProduct = item.idproduct;

        if lv_count = NULL then
            INSERT INTO bb_sales_sum
            VALUES (item.idproduct, item.total, item.quantity);
        else
            update bb_sales_sum 
            set quantity = item.quantity where
            idProduct = item.idproduct;
        end if;
    end loop;
    end if;

END; 
/

回答1:


You may use a MERGE in place of update, which will create a new row if there isn't one already for a given idproduct and updates the quantity and total for those rows which are already available.

CREATE OR REPLACE TRIGGER bb_salesum_trg
    AFTER UPDATE OF orderplaced on bb_basket 
      FOR EACH ROW
    WHEN (NEW.orderplaced = 1)

BEGIN   
  MERGE INTO bb_sales_sum t USING 
  ( select :new.idproduct as idproduct ,
           :new.quantity  as quantity,
           :new.total     as total 
           from dual ) s  
     ON (s.idproduct = t.idproduct ) 
         WHEN MATCHED THEN UPDATE
            SET  quantity  = s.quantity,
                 total     = s.total
          WHEN NOT MATCHED THEN
     INSERT (
          idproduct,quantity,total)
     VALUES
          ( :new.idproduct,:new.quantity,:new.total );
END;
/

DEMO




回答2:


It goes basically like this:

You have a table that is recording an individual order. It may have a Product Id, Quantity and a Total or similar columns.

You put your trigger code on this table.

When someone inserts a new record here, you would take the quantity and or total and update the main products table. You will add the new quantity and total to the existing summarized values in the main table where the product id matches.



来源:https://stackoverflow.com/questions/55995753/creating-a-trigger-that-updates-a-table-when-a-column-in-a-different-table-is-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!