MySQL Incorrect datetime value: '0000-00-00 00:00:00'

后端 未结 19 1044
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 15:51

I\'ve recently taken over an old project that was created 10 years ago. It uses MySQL 5.1.

Among other things, I need to change the default character set from latin1

19条回答
  •  星月不相逢
    2020-11-29 16:31

    According to MySQL 5.7 Reference Manual:

    The default SQL mode in MySQL 5.7 includes these modes: ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, and NO_ENGINE_SUBSTITUTION.

    Since 0000-00-00 00:00:00 is not a valid DATETIME value, your database is broken. That is why MySQL 5.7 – which comes with NO_ZERO_DATE mode enabled by default – outputs an error when you try to perform a write operation.

    You can fix your table updating all invalid values to any other valid one, like NULL:

    UPDATE users SET created = NULL WHERE created < '0000-01-01 00:00:00'
    

    Also, to avoid this problem, I recomend you always set current time as default value for your created-like fields, so they get automatically filled on INSERT. Just do:

    ALTER TABLE users
    ALTER created SET DEFAULT CURRENT_TIMESTAMP
    

提交回复
热议问题