ERROR 1067 (42000): Invalid default value for 'created_at'

前端 未结 14 1723
-上瘾入骨i
-上瘾入骨i 2020-12-04 07:31

When I tried to alter the table it showed the error:

ERROR 1067 (42000): Invalid default value for \'created_at\'

I googled for this error

14条回答
  •  一个人的身影
    2020-12-04 08:14

    You can do it like this:

     CREATE TABLE `ttt` (
      `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
      `t1` TIMESTAMP  NULL DEFAULT '0000-00-00 00:00:00',
      `t2` TIMESTAMP  NULL DEFAULT '0000-00-00 00:00:00',
      `t3` TIMESTAMP  NULL DEFAULT '0000-00-00 00:00:00',
      `t4` TIMESTAMP  NULL DEFAULT 0,
      PRIMARY KEY (`id`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    • Because the TIMESTAMP value is stored as Epoch Seconds, the timestamp value '1970-01-01 00:00:00' (UTC) is reserved since the second #0 is used to represent '0000-00-00 00:00:00'.
    • In MariaDB 5.5 and before there could only be one TIMESTAMP column per table that had CURRENT_TIMESTAMP defined as its default value. This limit has no longer applied since MariaDB 10.0.

    see: https://mariadb.com/kb/en/mariadb/timestamp/

    sample

    MariaDB []> insert into ttt (id) VALUES (1),(2),(3);
    Query OK, 3 rows affected (0.01 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    MariaDB []> select * from ttt;
    +----+---------------------+---------------------+---------------------+---------------------+
    | id | t1                  | t2                  | t3                  | t4                  |
    +----+---------------------+---------------------+---------------------+---------------------+
    |  1 | 0000-00-00 00:00:00 | 2000-01-01 12:01:02 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  2 | 0000-00-00 00:00:00 | 2000-01-01 12:01:02 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  3 | 0000-00-00 00:00:00 | 2000-01-01 12:01:02 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    +----+---------------------+---------------------+---------------------+---------------------+
    3 rows in set (0.00 sec)
    
    MariaDB []>
    

提交回复
热议问题