2014-09-04 79 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 
相關問題