MySql TIMESTAMP column is auto updated. Why?

后端 未结 2 1961
野的像风
野的像风 2021-01-18 10:39

I have a table created with this SQL:

CREATE TABLE `test_table` (
  `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `data` TEXT,
  `timestamp` TIMESTAMP
)         


        
相关标签:
2条回答
  • 2021-01-18 10:58

    If you want that to your timestamp will be not added automatically ON UPDATE you have to define it like this

    CREATE TABLE `test_table` (
       ...
       `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ...
    
    0 讨论(0)
  • 2021-01-18 10:59

    Use a datetime field instead

    Should I use field 'datetime' or 'timestamp'?

    Timestamps in MySQL generally used to track changes to records, and are often updated every time the record is changed. If you want to store a specific value you should use a datetime field.

    If you meant that you want to decide between using a UNIX timestamp or a native MySQL datetime field, go with the native format. You can do calculations within MySQL that way ("SELECT DATE_ADD(my_datetime, INTERVAL 1 DAY)") and it is simple to change the format of the value to a UNIX timestamp ("SELECT UNIX_TIMESTAMP(my_datetime)") when you query the record if you want to operate on it with PHP.

    0 讨论(0)
提交回复
热议问题