无法更新触发器中的另一个表

时间:2014-09-04 18:41:50

标签: mysql triggers sql-update

我们有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

1 个答案:

答案 0 :(得分:0)

我能够在这个帖子的帮助下得到它: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