2017-07-29 41 views
2

無效的默認值我得到的錯誤MySQL的:對於TIMESTAMP

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

運行下面的查詢

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'; 

我使用的是MySQL 5.7.18

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

按照時MySQL 5.7 documentation,以下語法是

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

上面的SQL語法有什麼問題?

+0

不知道這是一個問題,但你需要NOT NULL與DEFAULT?另外嘗試刪除(6)。 –

+0

我同意,NOT NULL可能是問題,因爲在插入期間,該字段最初爲null,以便在插入到current_timestamp之後進行更改。 – Myonara

+0

如何最終得到像int(* 10 *)這樣的東西!?! – Strawberry

回答

2

有趣的是,這兩個工作:

`start_time` timestamp(6), 

和:

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

您可以使用後者 - 離開精度說明了定義。

但正確的方法是:

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

documentation解釋:

如果TIMESTAMPDATETIME列定義包含的任何地方明確 分數秒精度值,相同的值必須在整個列定義中使用 。這是允許的:

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

這是不允許的:

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

嘗試:

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)