MySQL auto-store datetime for each row

后端 未结 7 448
醉话见心
醉话见心 2020-12-04 11:33

In MySQL, I\'m sick of adding the columns dt_created and dt_modified (which are date time stamps for creation and last modified respectively) to al

相关标签:
7条回答
  • 2020-12-04 11:45

    If you're using phpmyadmin you can do this by :

    enter image description here

    0 讨论(0)
  • 2020-12-04 11:47

    could be set as default an on update of rows

    ALTER TABLE `tablename` CHANGE `dt` `dt` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
    
    0 讨论(0)
  • 2020-12-04 11:49
    ALTER TABLE  `tablename` CHANGE  `dt`  `dt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
    

    should be the correct code.

    0 讨论(0)
  • 2020-12-04 11:49

    Well, you can't have both:

    mysql doc:

    It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.

    Sad, isn't it?

    You could however use null instead of now() following this tip

    0 讨论(0)
  • 2020-12-04 11:56

    You can use DEFAULT constraints to set the timestamp:

    ALTER TABLE
     MODIFY dt_created datetime DEFAULT CURRENT_TIMESTAMP
    
    ALTER TABLE
     MODIFY dt_modified datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    

    Then you wouldn't have to specify NOW() in your INSERT/UPDATE statements.

    Reference: TIMESTAMP properties

    0 讨论(0)
  • 2020-12-04 11:59

    In phpmyadmin you can set enter image description here

    OR use this query

    ALTER TABLE  `tablename`
        CHANGE  `dt_created`  `dt_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
    
    0 讨论(0)
提交回复
热议问题