search

Home  >  Q&A  >  body text

php - Timestamp problem after Mysql 5.7 version

Environment:

>select version();
> 5.7.16

Question:

There are two fields in a table, created_at and updated_at. Both field types are timestamp. created_at The default value is CURRENT_TIMESTAMP, and the default value of updated_at is set to 0000-00-00 00:00:00, the problem arises at this time. Execute the statement to create the table or report an error. The following is the error code:

Syntax error or access violation: 1067 Invalid default value for 'updated_at'

If you change the default value of updated_at to CURRENT_TIMESTAMP, it will execute smoothly, but there will be another problem. Versions below Mysql5.7 do not support one table Are there two timestamp fields with a default value of CURRENT_TIMESTAMP?

So, please ask all the masters, how to solve this problem of the default value of timestamp? Compatible with both high and low versions of Mysql.

&^ - &^

仅有的幸福仅有的幸福2838 days ago682

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-05-16 13:11:07

    5.7 requires the default value to be within a range, which seems to be from 1970 to 20 years ago. You can set a value of 2000-01-01 00:00:00 and try it. It should be OK

    reply
    0
  • 阿神

    阿神2017-05-16 13:11:07

    32-bit systems seem to be in 2038. Use a 64-bit system. Also, try to save data with timestamps.

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:11:07

    Looking at this problem from the perspective of the requirements of the update_at field, we literally understand that this field should be used to table the update time of the record.
    If the update time has a default value, but the default value is not the current database time inserted when updating/adding records (the subject uses 0000-00-00 00:00:00), then any value other than the current database time will be used. The meaning of this field does not exist (redundant/wrong data).
    Corresponding to the wrong data in the database, there is no need to save it.
    In summary, the questioner would like to see if it is possible not to give the update_at field a default value?

    reply
    0
  • Cancelreply