简体   繁体   English

MySQL:TIMESTAMP 的默认值无效

[英]MySQL: Invalid default value for TIMESTAMP

I get the error我收到错误

ERROR 1067 (42000) at line 5459: Invalid default value for 'start_time'

when running the following query运行以下查询时

DROP TABLE IF EXISTS `slow_log`;
CREATE TABLE IF NOT EXISTS `slow_log` (
  `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `user_host` mediumtext NOT NULL,
  `query_time` time(6) NOT NULL,
  `lock_time` time(6) NOT NULL,
  `rows_sent` int(11) NOT NULL,
  `rows_examined` int(11) NOT NULL,
  `db` varchar(512) NOT NULL,
  `last_insert_id` int(11) NOT NULL,
  `insert_id` int(11) NOT NULL,
  `server_id` int(10) unsigned NOT NULL,
  `sql_text` mediumtext NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';

I am using MySQL 5.7.18我正在使用 MySQL 5.7.18

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.18, for osx10.10 (x86_64) using  EditLine wrapper

According to the MySQL 5.7 documentation , the following syntax is根据MySQL 5.7 文档,以下语法是

CREATE TABLE t1 (
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

What is wrong with the SQL syntax above?上面的 SQL 语法有什么问题?

Interestingly, both these work:有趣的是,这两种方法都有效:

`start_time` timestamp(6), 

And:和:

`start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

You can use the latter -- leave the precision specifier out of the definition.您可以使用后者——将精度说明符排除在定义之外。

But the right method is:但正确的方法是:

`start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),

As explained in the documentation :文档中所述:

If a TIMESTAMP or DATETIME column definition includes an explicit fractional seconds precision value anywhere, the same value must be used throughout the column definition.如果TIMESTAMPDATETIME列定义在任何地方包含显式小数秒精度值,则必须在整个列定义中使用相同的值。 This is permitted:这是允许的:

 CREATE TABLE t1 ( ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) );

This is not permitted:这是不允许的:

 CREATE TABLE t1 ( ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(3) );

Try:尝试:

mysql> DROP TABLE IF EXISTS `slow_log`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `slow_log` (
    ->   `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
    ->                                      ON UPDATE CURRENT_TIMESTAMP(6),
    ->   `user_host` mediumtext NOT NULL,
    ->   `query_time` time(6) NOT NULL,
    ->   `lock_time` time(6) NOT NULL,
    ->   `rows_sent` int NOT NULL,
    ->   `rows_examined` int NOT NULL,
    ->   `db` varchar(512) NOT NULL,
    ->   `last_insert_id` int NOT NULL,
    ->   `insert_id` int NOT NULL,
    ->   `server_id` int unsigned NOT NULL,
    ->   `sql_text` mediumtext NOT NULL
    -> ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';
Query OK, 0 rows affected (0.00 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM