如何使字段的數據同步??
數據庫中有 字段1,字段2,字段3
想將  字段1-字段2 的值插入字段3,
並且使字段3的值与 字段1,字段2 同步:如果字段1,字段2的值改變,則字段3自動改變
如何實現????????????

解决方案 »

  1.   


    create table tb(
           col_1 number(5,2),
           col_2 number(5,2),
           col_3 number(5,2))
    --
    create or replace trigger tri_col_3
    before insert or update on tb
    for each row
    begin
         select :new.col_1-:new.col_2 into :new.col_3 from dual;
    end tri_col_3;
    --
    insert into tb(col_1,col_2,col_3)
    values(123,25.23,589.11);
    insert into tb(col_1,col_2)
    values(300.60,500);
    --
    //可以看到,使用触发器,即使我们为col_3指定值,触发器也将col_1-col_2的值存储到col_3中
    //值得注意的是,col_3能存下col_1-col_2得到的值。
    select * from tb;
      COL_1   COL_2   COL_3
    ------- ------- -------
     123.00   25.23   97.77
     300.60  500.00 -199.40
      

  2.   

    create table tb(
           col_1 number(5,2),
           col_2 number(5,2),
           col_3 number(5,2))
    --
    create or replace trigger tri_col_3
    before insert or update on tb
    for each row
    begin
         select :new.col_1-:new.col_2 into :new.col_3 from dual;
    end tri_col_3;
    --
    insert into tb(col_1,col_2,col_3)
    values(123,25.23,589.11);
    insert into tb(col_1,col_2)
    values(300.60,500);
    --
    //可以看到,使用触发器,即使我们为col_3指定值,触发器也将col_1-col_2的值存储到col_3中
    //值得注意的是,col_3能存下col_1-col_2得到的值。
    select * from tb;
      COL_1   COL_2   COL_3
    ------- ------- -------
     123.00   25.23   97.77
     300.60  500.00 -199.40
    经典