Invalid default value for 'create_date' timestamp field

后端 未结 16 1428
我寻月下人不归
我寻月下人不归 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:14

    I was able to resolve this issue on OS X by installing MySQL from Homebrew

    brew install mysql
    

    by adding the following to /usr/local/etc/my.cnf

    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
    

    and restarting MySQL

    brew tap homebrew/services
    brew services restart mysql
    
    0 讨论(0)
  • 2020-11-30 17:17

    I try to set type of column as 'timestamp' and it works for me.

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

    If you generated the script from the MySQL workbench.

    The following line is generated

    SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
    

    Remove TRADITIONAL from the SQL_MODE, and then the script should work fine

    Else, you could set the SQL_MODE as Allow Invalid Dates

    SET SQL_MODE='ALLOW_INVALID_DATES';
    
    0 讨论(0)
  • 2020-11-30 17:19

    TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC (see doc). The default value must be within that range.

    Other odd, related, behavior:

    CREATE TABLE tbl1 (
        ts TIMESTAMP);  
    Query OK, 0 rows affected (0.01 sec)
    
    CREATE TABLE tbl2 (
        ts TIMESTAMP,
        ts2 TIMESTAMP);
    ERROR 1067 (42000): Invalid default value for 'ts2'
    
    CREATE TABLE tbl3 (
        ts TIMESTAMP,
        ts2 TIMESTAMP DEFAULT '1970-01-01 00:00:01');
    Query OK, 0 rows affected (0.01 sec)
    

    Side note, if you want to insert NULLS:

    CREATE TABLE tbl4 (
        ts TIMESTAMP NULL DEFAULT NULL);
    
    0 讨论(0)
提交回复
热议问题