在 mysql 中进行查询而不调用触发器(如何禁用触发器)

发布于 2024-09-16 07:12:37 字数 1720 浏览 6 评论 0原文

我有 2 个表:commentscomments_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 PRIORITYIGNORE 不起作用。

[编辑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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月光色 2024-09-23 07:12:37

要禁用触发器,您可以执行以下操作:

触发器 1

SET @disable_trigger = 1;
// do stuff that calls trigger 2
SET @disable_trigger = NULL;

触发器 2

IF @disable_trigger IS NULL THEN
    // do stuff only if called from a query and not from trigger 1
END IF;

To disable triggers you can do:

Trigger 1

SET @disable_trigger = 1;
// do stuff that calls trigger 2
SET @disable_trigger = NULL;

Trigger 2

IF @disable_trigger IS NULL THEN
    // do stuff only if called from a query and not from trigger 1
END IF;
め可乐爱微笑 2024-09-23 07:12:37

可能有点难看,但我所做的是将表重命名为 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.

岁月苍老的讽刺 2024-09-23 07:12:37

不,你不能。这就是触发器的要点:始终运行。

此外,我不明白为什么在你的情况下需要它。最坏的情况是没有任何更新 - 不会引发错误。

您始终可以在触发器中添加一个条件,以检查它们(或其部分代码)是否应该运行(例如,相关中是否有记录)桌子)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文