我想建一个触发器,如果test表中某一行被更新后,把该行的flg字段置成"有更新",该如何写?自己试着写了一个,不大对!
DROP TRIGGER IF EXISTS test_trigger_update; 
CREATE test_trigger_update
AFTER update ON test
FOR EACH ROW
BEGIN
   update test set flg='有更新' where id = old.id;
END

解决方案 »

  1.   

    alter table tt add uptime timestamp on update current_timestamp;
    为它加一列字段不行吗;记录最后修改时间。你的触发器用法有问题,如果这样更新那不死循环了吗。
      

  2.   

    drop table if exists test;
    create table test(
    id int primary key,
    flg nvarchar(20)
    );
    insert into test values(1,'');
    insert into test values(2,'');drop trigger if exists trig_test_update;
    delimiter $$
    CREATE TRIGGER `trig_test_update`
    before  update  on `test` for each row
    BEGIN
      set new.flg='有更新';
    END;
    $$update test set flg='a';select * from test;
    /*
    +----+--------+
    | id | flg    |
    +----+--------+
    |  1 | 有更新 |
    |  2 | 有更新 |
    +----+--------+
    */
      

  3.   

    你更新这张表的sql语句里,加上flag=有更新 不就行了?自己触发自己有问题吧。搞不好死循环。
      

  4.   

    这块如果用出发器的话,可能真的陷入死循环,一直在执行update.但是如果加一个字段,alter table tt add uptime timestamp on update current_timestamp ,并且我在根据你的思路编写触发器的时候遇到一个问题,你这样的出发器也就是每条数据只update一次有效,如果重复update的,flg字段一直展示的有更新,那样,将不会展示其效果