简体   繁体   English

获取触发器以仅在审计表中插入更改的列值

[英]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.

相关问题 包含NULL作为值的列未在审计表中进行审计。 我正在使用AFTER更新触发器 - Column which contains NULL as value is not getting audit in audit table. I am using AFTER update trigger 触发审计跟踪表 - trigger for audit trail table 审核触发器,将Blob插入为LongText - Audit Trigger, Insert Blob As LongText SQL触发器:一次将删除的行插入到表中,而无需将每一列都列为INSERT值 - SQL TRIGGER: Insert deleted row into a table at once without listing every column as INSERT values MySQL触发器问题:仅在更改列时触发? - MySQL Trigger question: only trigger when a column is changed? MySQL 触发器:在 INSERT 上更改列值 - MySQL Trigger: Change column values on INSERT 在仅具有一个自动增量列的表中插入之前无法触发 - Fail to trigger BEFORE INSERT INTO a table that only has one auto increment column MYSQL触发器以在同一表中插入列值 - MYSQL Trigger to insert column value in same table MySQL - 在满足其他值的地方插入后触发更新另一个表中的列 - MySQL - Trigger to update Column in another table after insert where it satisfys other values MySQL 触发器 - 复制新。 插入的数据与现有列值进行比较,如果匹配,则将它们插入其他表 - MySQL Trigger - Copy NEW. inserted data compare to existing column values and then insert them to other table if they match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM