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

后端 未结 19 999
没有蜡笔的小新
没有蜡笔的小新 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:34

    I found the solution at https://support.plesk.com/hc/en-us/articles/115000666509-How-to-change-the-SQL-mode-in-MySQL. I had this:

    mysql> show variables like 'sql_mode';
    +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
    | Variable_name | Value                                                                                                                                     |
    +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
    | sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
    +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set, 1 warning (0.01 sec)
    

    Notice the NO_ZERO_IN_DATE,NO_ZERO_DATE in the results above. I removed that by doing this:

    mysql> SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    

    Then I had this:

    mysql> show variables like 'sql_mode';
    +---------------+--------------------------------------------------------------------------------------------------------------+
    | Variable_name | Value                                                                                                        |
    +---------------+--------------------------------------------------------------------------------------------------------------+
    | sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
    +---------------+--------------------------------------------------------------------------------------------------------------+
    1 row in set, 1 warning (0.01 sec)
    

    After doing that, I could use ALTER TABLE successfully and alter my tables.

    0 讨论(0)
  • 2020-11-29 16:35

    If you are entering the data manually you may consider removing the values and the zeros on the TIMESTAMP(6).000000 so that it becomes TIMESTAMP. That worked fine with me.

    0 讨论(0)
  • 2020-11-29 16:36

    Make the sql mode non strict

    if using laravel go to config->database, the go to mysql settings and make the strict mode false

    0 讨论(0)
  • 2020-11-29 16:37

    Check

    SELECT @@sql_mode;
    

    if you see 'ZERO_DATE' stuff in there, try

    SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'NO_ZERO_DATE',''));   
    SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'NO_ZERO_IN_DATE',''));   
    

    Log out and back in again to your client (this is strange) and try again

    0 讨论(0)
  • 2020-11-29 16:38

    This is what I did to solve my problem. I tested in local MySQL 5.7 ubuntu 18.04.

    set global sql_mode="NO_ENGINE_SUBSTITUTION";
    

    Before running this query globally I added a cnf file in /etc/mysql/conf.d directory. The cnf file name is mysql.cnf and codes

    [mysqld]
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    

    Then I restart mysql

    sudo service mysql restart
    

    Hope this can help someone.

    0 讨论(0)
  • 2020-11-29 16:43

    I got it fixed by doing this before the query

    SET SQL_MODE='ALLOW_INVALID_DATES';
    
    0 讨论(0)
提交回复
热议问题