【问题标题】:Can't update other table in trigger无法更新触发器中的其他表
【发布时间】:2014-09-04 18:41:50
【问题描述】:

我们有 2 个存储用户名和密码的表,以及两个用户登录的网站(我知道这是个坏主意,但我无法控制)。

因此,当成员在一个表上更新他们的密码时,我希望触发器也更新另一个表上的密码。我遇到了问题,因为它给了我一个错误。

查询:

update authenticate set password = md5('superman') where member_id = 108649

错误:

ERROR 1442 (HY000) at line 3: Can't update table 'authenticate' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

这里有两个触发器:

-- When update runs on the authenticate table
CREATE TRIGGER `authenticate_after_update` AFTER UPDATE ON `authenticate` FOR EACH ROW BEGIN
    IF new.password != old.password THEN 
        update auth set passwd = new.password where member_id = new.member_id;
    end if;
END

-- When update runs on the auth table
CREATE TRIGGER `auth_after_update` AFTER UPDATE ON `auth` FOR EACH ROW BEGIN
    if new.passwd != old.passwd then
        update authenticate set password = new.passwd where member_id = new.member_id;
    end if;
END

【问题讨论】:

    标签: mysql triggers sql-update


    【解决方案1】:

    我在这个帖子的帮助下得到了它:How to avoid circular Trigger dependencies in MySQL

    这是我想出的结果:

    -- When update runs on the authenticate table
    CREATE TRIGGER `authenticate_after_update` AFTER UPDATE ON `authenticate` FOR EACH ROW BEGIN
        if @updating is null then
            set @updating = 1;
            update auth set passwd = new.password where member_id = new.member_id;
            set @updating = null;
        end if;
    END
    
    -- When update runs on the auth table
    CREATE TRIGGER `auth_after_update` AFTER UPDATE ON `auth` FOR EACH ROW BEGIN
        if @updating is null then
            set @updating = 1;
            update authenticate set password = new.passwd where member_id = new.member_id;
            set @updating = null;
        end if;
    END
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2015-11-27
      • 2021-07-15
      相关资源
      最近更新 更多