本文介绍了MYSQL触发器更新复制整个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个触发器,以便在任何 UPDATE 上将整个行复制到审核表中.
I am trying to create a trigger to copy an entire row to an audit table on any UPDATE.
我有2张桌子
Frequencies
和Frequencies_Audit
这是我的触发器.
create trigger auditlog
before update on frequencies
for each row insert into
frequencies_audit select frequencies.*;
当我更新记录时,我得到了未知表frequencies
.
When I update the record I get Unknown Table frequencies
.
由于我们会不断在数据库中添加列,因此我不想尽可能地分别输入每个字段名称.
I don't want to have to enter each field name separately if possible since we are constantly adding columns in the database.
推荐答案
而不是BEFORE UPDATE
,您可以编写AFTER UPDATE
触发器,如下所示:
Rather than BEFORE UPDATE
, you can write AFTER UPDATE
trigger as follow ::
DELIMITER //
CREATE TRIGGER auditlog AFTER UPDATE ON frequencies
FOR EACH ROW BEGIN
INSERT INTO frequencies_audit select * from frequencies where freqId = NEW.freqId;
END;//
DELIMITER ;
freqId只是ID列的名称.将其替换为频率表中Id列的名称.
freqId is just a name of Id column. Replace it with the name of Id column in your frequencies table.
这篇关于MYSQL触发器更新复制整个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!