[英]Trigger for column value change?
I am using some SQL triggers to launch actions when a table row gets modified or created. 当表行被修改或创建时,我正在使用一些SQL触发器来启动操作。 That works like a charm and in my example sets the column
gen_date
to the current date/time: 这就像一个
gen_date
按钮,在我的示例中将gen_date
列gen_date
为当前日期/时间:
CREATE TRIGGER insert_template BEFORE INSERT ON template
FOR EACH ROW BEGIN
SET new.gen_date := now();
END;
I have another column image
and I would like to add a column image_date
which should have the value of the current time/date when that field gets updated. 我有另一个列
image
,我想添加一列image_date
,当该字段被更新时,它应该具有当前时间/日期的值。
Question: Is it possible to set up a trigger that keeps track of column wise modifications? 问题:是否可以设置一个触发器来跟踪列的修改?
New values are accessible by NEW.
新值可通过
NEW.
访问NEW.
, old by OLD.
,由
OLD.
. 。 You can compare them to define if values were changed.
您可以比较它们以定义值是否已更改。
CREATE TRIGGER insert_template BEFORE INSERT ON template
FOR EACH ROW BEGIN
SET NEW.gen_date := now();
IF NEW.image <> '' THEN
SET NEW.image_date := now();
END IF;
END;
CREATE TRIGGER update_template BEFORE UPDATE ON template
FOR EACH ROW BEGIN
IF NEW.image <> OLD.image THEN
SET NEW.image_date := now();
END IF;
END;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.