Invalid default value for 'create_date' timestamp field

后端 未结 16 1427
我寻月下人不归
我寻月下人不归 2020-11-30 16:49

I have the following sql create statement

mysql> CREATE  TABLE IF NOT EXISTS `erp`.`je_menus` (
    ->   `id` INT(11) NOT NULL AUTO_INCREMENT ,
    -&g         


        
相关标签:
16条回答
  • 2020-11-30 17:09

    Using OS X, install mysql from Homebrew, System Variables based on its compiled-in defaults. Solution is to remove "NO_ZERO_DATE" from System Variables "sql_mode".

    Just please keep in mind that scope involve.

    If you want to affect only in your session, please use "@@session", For example:

    SET @@session.sql_mode ="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION".
    

    In this case, it will not affect once your session ends or your change it. It has not effect on other session.

    If you want to affect on all client, please use "@@global", for example:

    SET @@global.sql_mode ="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION".
    

    In this case, it only affects on the clients that connect after the change(not affect on current all clients), and will not work once server exit.

    0 讨论(0)
  • 2020-11-30 17:09

    You could just change this:

    `create_date` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
    

    To something like this:

    `create_date` TIMESTAMP NOT NULL DEFAULT '2018-04-01 12:00:00',
    
    0 讨论(0)
  • 2020-11-30 17:10

    That is because of server SQL Mode - NO_ZERO_DATE.

    From the reference: NO_ZERO_DATE - In strict mode, don't allow '0000-00-00' as a valid date. You can still insert zero dates with the IGNORE option. When not in strict mode, the date is accepted but a warning is generated.

    0 讨论(0)
  • 2020-11-30 17:10

    In ubuntu desktop 16.04, I did this:

    1. open file: /etc/mysql/mysql.conf.d/mysqld.cnf in an editor of your choice.

    2. Look for: sql_mode, it will be somewhere under [mysqld].

    3. and set sql_mode to the following:

      NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

    4. Save and then restart mysql service by doing:

      sudo service mysql restart

    0 讨论(0)
  • 2020-11-30 17:13

    I had a similar issue with MySQL 5.7 with the following code:

    `update_date` TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP

    I fixed by using this instead:

    `update_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

    0 讨论(0)
  • 2020-11-30 17:13

    Default values should start from the year 1000.

    For example,

    ALTER TABLE mytable last_active DATETIME DEFAULT '1000-01-01 00:00:00'
    

    Hope this helps someone.

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