是否对“插入重复键更新”中的每一行执行 BEFORE INSERT 触发器?询问

发布于 2024-12-09 00:28:24 字数 331 浏览 0 评论 0原文

这是如何运作的 ?如果要运行的查询如下“

INSERT INTO tSomething VALUES (...) ON DUPLICATE KEY UPDATE ......

我想要插入行时根据其他列的值更新列的值但是,我不希望这种更新行为始终是我选择的 INSERT 。但是,我无法使用 AFTER INSERT TRIGGER as

SET NEW.score = NEW.score1 + NEW.score2 更新列;

我在某处读到,在这种情况下 BEFORE INSERT 触发器将允许每次在上面时触发触发器查询被执行。这是真的吗?如果是,我如何使用触发器解决这个问题。

How does this work ? Does mysql invoke BEFORE INSERT trigger only in case of inserts or everytime an insert or update happens if the query to be run is as follows "

INSERT INTO tSomething VALUES (...) ON DUPLICATE KEY UPDATE ......

I want to update a column's value depending upon other col's values when inserting a row. However, I don't want this behaviour for updates. The query which will be executed always would be the one mentioned above. I chose INSERT AFTER for this. However, I can't update a col using AFTER INSERT TRIGGER as

SET NEW.score = NEW.score1 + NEW.score2 ;

I read somewhere that BEFORE INSERT trigger in this case would allow triggers to be fired everytime when above query is executed. Is this true ? If yes, how do I solve this problem using a trigger.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

杀手六號 2024-12-16 00:28:24

mysql 是否仅在插入时或每次发生插入或更新时调用 AFTER INSERT 触发器

正确 来自手册

将为每一行激活一个 BEFORE INSERT 触发器,然后是一个 AFTER INSERT 触发器或同时激活 BEFORE UPDATE 和 AFTER UPDATE 触发器,具体取决于该行是否有重复的键

我不确定您到底在做什么。

但是,我不希望这种更新行为”这句话让我感到困惑。如果您唯一拥有的是 AFTER INSERT 触发器,那么当您运行 UPDATE 语句时它不会被触发。如果您的语句更新一行,它将触发 UPDATE 触发器,而不是 INSERT 触发器。

Does mysql invoke AFTER INSERT trigger only in case of inserts or everytime an insert or update happens

Right from the manual:

a BEFORE INSERT trigger will activate for every row, followed by either an AFTER INSERT trigger or both the BEFORE UPDATE and AFTER UPDATE triggers, depending on whether there was a duplicate key for the row

I'm not sure what exactly you are after.

The sentence "However, I don't want this behaviour for updates" confuses me somehow. If the only thing you have is an AFTER INSERT trigger, then it will not be fired if you run an UPDATE statement. And if your statement updates a row, it will fire an UPDATE trigger, not your INSERT trigger.

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