[英]Getting trigger to insert changed column values only in audit table
I'm using a trigger to store changes in an audit table, I only want to store the values from columns that have been changed. 我正在使用触发器将更改存储在审计表中,我只想存储已更改列中的值。
BEGIN
IF NEW.history_of_repair_trigger_fired = 1 THEN
INSERT INTO history_of_repair SET
edit_date_time=NEW.last_edited_date_time,
edited_by=NEW.edited_by,
repair_id=NEW.repair_id,
tenant_name=NEW.tenant_name,
property_id=NEW.property_id,
priority=NEW.priority,
comments=NEW.comments,
signed_off=NEW.signed_off;
END IF;
END
At the moment this is storing the unchanged values in the audit table too, which is not desirable. 目前,这还将未更改的值存储在审计表中,这是不希望的。
What's the best way of only storing the changed columns in my audit table? 仅将更改的列存储在审核表中的最佳方法是什么?
Based on your comments, I wouldn't be concerned about storing only the changed values -- disk space is cheap. 根据您的评论,我不会只存储更改的值,磁盘空间很便宜。 Storing the actual values has the advantage that it makes it trivial to restore a record to a point in time if you need to in a hurry.
存储实际值的优势在于,如果需要急忙,可以轻松地将记录恢复到某个时间点。 If you need to produce a human-readable audit record, then store it in a varchar/nvarchar and construct the message in your trigger by accumulating the changes into a string.
如果您需要产生人类可读的审核记录,则将其存储在varchar / nvarchar中,并通过将更改累积到字符串中来在触发器中构造消息。 I might store this in addition to the actual values.
除了实际值,我可能还会存储它。 Note that you could also provide a table-valued function which dynamically constructs this human readable column for you rather than storing it.
请注意,您还可以提供一个表值函数,该函数为您动态构造此人类可读的列,而不是存储它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.