2014-09-04 34 views
0

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

回答

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 
相关问题