MySQL防止触发链

时间:2019-01-31 22:29:48

标签: mysql sql triggers

我遇到了一个独特的问题。我有一张桌子,premium_users。用户,他们所属的服务器等都充满了用户。我在DELETEINSERT上都有此表的触发器,它会更新partner_servers表。

现在,我正在尝试向partner_servers添加触发器,以便在删除服务器时,它会从premium_users删除属于该服务器的所有用户。但这最终试图触发我在第一段中提到的那个。

我得到了错误:

ERROR 1442: 1442: Can't update table 'partner_servers' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

如何避免这种情况?我尝试将触发器设置为AFTER DELETE ON partner_servers,并在premium_users触发器中设置以下if语句:

IF (SELECT COUNT(*) FROM partner_servers WHERE OLD.Server_ID) THEN
    UPDATE partner_servers SET Total_Premium = (
        SELECT COUNT(*) FROM premium_users WHERE Server_ID = OLD.Server_ID
    ) WHERE Server_ID = OLD.Server_ID;
END IF;

0 个答案:

没有答案