mysql添加当前日期字段,MySQL在插入时的DATETIME字段中设置当前日期

  • Post author:
  • Post category:mysql


I have a ‘created_date’ DATETIME field which I would like to be populated with the current date upon insert. What syntax should I use for the trigger for this? This is what I have so far but it is incorrect.

CREATE TRIGGER set_created_date

BEFORE INSERT ON product

FOR EACH ROW BEGIN

SET NEW.created_date = now();

解决方案

Your best bet is to change that column to a timestamp. MySQL will automatically use the first timestamp in a row as a ‘last modified’ value and update it for you. This is configurable if you just want to save creation time.