MySQL set current date in a DATETIME field on insert

mysql, triggers

Solution

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.

See doc http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

Problem

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(); ```

Original source

Related problems