在 mysql 中进行查询而不调用触发器(如何禁用触发器)
我有 2 个表:comments
和 comments_likes
。
评论
id
message
likes
触发:
删除后
DELETE FROM comments_likes WHERE comment_id = OLD.id;
comments_likes
id
comment_id
触发:
插入后 em>
UPDATE comments SET likes = likes + 1 WHERE comments.id = NEW.comment_id;
删除后
UPDATE comments SET likes = likes - 1 WHERE comments.id = OLD.comment_id;
更新后
**omited code, updates comments**
所以问题是,我可以在从另一个触发器激活触发器时禁用触发器吗?
我想要做的是这样的事情:
删除后
IF NOT called_from_another_trigger() THEN
UPDATE comments SET likes = likes - 1 WHERE comments.id = OLD.comment_id;
END IF;
[编辑]
一个非优化的解决方案是(非常慢的查询......对每个 LIKE 寄存器进行查询):
BEGIN
IF (SELECT id FROM comments WHERE comments.id = OLD.comment_id) THEN
UPDATE comments SET comments.cache_likes = comments.cache_likes - 1 WHERE comments.id = OLD.comment_id;
END IF;
END
UPDATE LOW PRIORITY
和 IGNORE
不起作用。
[编辑2]
我有另一个想法,是否可以在第一个触发器中设置全局变量并从另一个触发器中读取它?
例如:
第一个触发器:
@disable_triggers = true;
// do the stuff that calls another triggers
@disable_triggers = false;
其他触发器:
if @disable_triggers = false then
// do the stuff
end if;
I have 2 tables: comments
and comments_likes
.
comments
id
message
likes
triggers:
AFTER DELETE
DELETE FROM comments_likes WHERE comment_id = OLD.id;
comments_likes
id
comment_id
triggers:
AFTER INSERT
UPDATE comments SET likes = likes + 1 WHERE comments.id = NEW.comment_id;
AFTER DELETE
UPDATE comments SET likes = likes - 1 WHERE comments.id = OLD.comment_id;
AFTER UPDATE
**omited code, updates comments**
So the question is, can I disable the triggers when activating them from another trigger?
What I want is do something likes this:
AFTER DELETE
IF NOT called_from_another_trigger() THEN
UPDATE comments SET likes = likes - 1 WHERE comments.id = OLD.comment_id;
END IF;
[EDIT]
A non optimized solution would be (very slow query... makes a query for each LIKE register):
BEGIN
IF (SELECT id FROM comments WHERE comments.id = OLD.comment_id) THEN
UPDATE comments SET comments.cache_likes = comments.cache_likes - 1 WHERE comments.id = OLD.comment_id;
END IF;
END
UPDATE LOW PRIORITY
and IGNORE
don't works.
[EDIT 2]
I have another idea, is possible to set a global variable in the first trigger and read it from the other trigger?
Ex:
first trigger:
@disable_triggers = true;
// do the stuff that calls another triggers
@disable_triggers = false;
other trigger:
if @disable_triggers = false then
// do the stuff
end if;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要禁用触发器,您可以执行以下操作:
触发器 1
触发器 2
To disable triggers you can do:
Trigger 1
Trigger 2
可能有点难看,但我所做的是将表重命名为 table2,触发器也不会附加该表,然后最后重命名回来。
Can be a bit ugly, but what i do is rename the table to table2 which the trigger does not attach too, and then, on the end, rename back.
不,你不能。这就是触发器的要点:始终运行。
此外,我不明白为什么在你的情况下需要它。最坏的情况是没有任何更新 - 不会引发错误。您始终可以在触发器中添加一个条件,以检查它们(或其部分代码)是否应该运行(例如,相关中是否有记录)桌子)。
No you can't. That's the point of triggers: to be run always.
Besides, I can't see why you would need that in your case. At worst nothing gets updated - no errors will be raised.You can always add a condition in your trigger, to check if they (or part of their code) should be run (for example if there is a record in relevant table).