我想将对working
表的更改记录到history
表中,但是只有在UPDATE期间值发生变化时,我才尝试创建update_history触发器,但是无论是否值是否已更改,例如说我的工作表中有此值:
shift_id|site |organisational_unit
--------|---------|-------------------
123475|site01 |my org
如果我执行更新查询
UPDATE working SET site = $1, organisational_unit = $2 WHERE shift_id=$3', ['site01', 'my new org', '123475']
这会在历史记录表中为site
创建一行,甚至认为它没有更改值,我只希望为organizational_unit更改添加新行
historyid|shiftid|fieldname |oldvalue |newvalue |updatedat |
---------|-------|-------------------|---------|-----------|-------------------|
7| 123475|organisational_unit|my org |my new org |2019-07-01 10:21:19|
8| 123475|site |site01 |site01 |2019-07-01 10:21:19|
我的触发器看起来像这样
-- create function for updates to track history
CREATE function update_history ()
RETURNS TRIGGER
as $$
BEGIN
-- check if data in column has changed between the update
IF NEW.client_id <> OLD.client_id THEN
-- if it has insert a row to the history table detailing the changes
INSERT INTO history (ShiftId, fieldName, OldValue, NewValue)
VALUES(New.shift_id, 'client id ', OLD.client_id, NEW.client_id);
-- if nothing has changed don't do anything
END IF;
IF NEW.organisational_unit <> OLD.organisational_unit THEN
INSERT INTO history (ShiftId, fieldName, OldValue, NewValue)
VALUES(New.shift_id, 'organisational_unit', OLD.organisational_unit, NEW.organisational_unit);
END IF;
IF NEW.site <> OLD.site THEN
INSERT INTO history
(ShiftId, fieldName, OldValue, NewValue)
VALUES(New.shift_id, 'site', OLD.site, NEW.site);
END IF;
return null;
END;
$$
language plpgsql;
答案 0 :(得分:4)
最有效的方法是定义仅在某些列更改时才触发的触发器:
CREATE TRIGGER ... BEFORE UPDATE ON ... FOR EACH ROW
WHEN (NEW IS DISTINCT FROM OLD)
EXECUTE FUNCTION update_history();
这样可以避免在不必要的情况下执行该功能。
答案 1 :(得分:2)
要检查列是否已更改,请不要使用<>
。它不考虑空值。使用IS DISTINCT FROM
IF NEW.client_id IS DISTINCT FROM OLD.client_id
...
或使用IF NEW IS DISTINCT FROM OLD
如果要防止发生插入主表的情况,应使用BEFORE UPDATE
触发器,并应执行
return null;
仅在您不希望使用INSERT
的地方