有没有办法只处理更新前MySQL触发器中更改的记录?
我需要一个语句,允许在更新表 2 时更改表 1 中的某些字段。
我目前有以下代码:
delimiter //
CREATE TRIGGER check BEFORE UPDATE ON table2
FOR EACH ROW
BEGIN
IF NEW.Count <> OLD.Count THEN
...
END IF;
END//
delimiter ;
但是,table1非常大并且有很多更新事件,所以我担心这段代码会减慢我的服务器速度。所以我想知道是否有某种解决方案不会查看其中的所有记录,而只查看更新的记录?
I need a statement that allows to change some fields in table1 when table2 was updated.
I have currently the following code:
delimiter //
CREATE TRIGGER check BEFORE UPDATE ON table2
FOR EACH ROW
BEGIN
IF NEW.Count <> OLD.Count THEN
...
END IF;
END//
delimiter ;
However, table1 is very large and there are a lot of update events, so I'm afraid that this code can slowdown my server. So I want to know if there is some solution that will not look over all records in it, but only those updated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
触发器不会“查看”所有记录。因为您将触发器声明为
FOR EACH ROW
,所以触发器中只会处理单行。当触发器针对每一行执行时,
NEW
和OLD
引用单行,这意味着条件NEW.Count <> OLD.Count
只是比较两个(简单)列值The trigger will not "look" over all records. Because you declared the trigger as
FOR EACH ROW
only a single row will be dealt with in the trigger.As the trigger is execute for each row,
NEW
andOLD
reference a single row as, which means that the conditionNEW.Count <> OLD.Count
simply compares two (simple) column values