MySQL set current date in a DATETIME field on insert

后端 未结 4 1678
迷失自我
迷失自我 2020-12-13 19:55

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 wha

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 20:02

    Since MySQL 5.6.X you can do this:

    ALTER TABLE `schema`.`users` 
    CHANGE COLUMN `created` `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ;
    

    That way your column will be updated with the current timestamp when a new row is inserted, or updated.

    If you're using MySQL Workbench, you can just put CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP in the DEFAULT value field, like so:

    http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html

提交回复
热议问题