触发列值更改? [英] Trigger for column value change?

查看:69
本文介绍了触发列值更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当表行被修改或创建时,我正在使用一些SQL触发器来启动操作.就像一个超级按钮一样,在我的示例中,将列gen_date设置为当前日期/时间:

I am using some SQL triggers to launch actions when a table row gets modified or created. That works like a charm and in my example sets the column gen_date to the current date/time:

CREATE TRIGGER insert_template BEFORE INSERT ON template
FOR EACH ROW BEGIN
  SET new.gen_date := now();
END;

我还有另一列image,我想添加一列image_date,该列应具有当该字段更新时的当前时间/日期的值.

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.

问题:是否可以设置一个触发器来跟踪列的修改?

Question: Is it possible to set up a trigger that keeps track of column wise modifications?

推荐答案

NEW.可访问新值,OLD.可访问旧值.您可以比较它们以定义值是否已更改.

New values are accessible by NEW., old by 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;

这篇关于触发列值更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆