2017-07-29 10 views
2

Ich erhalte den FehlerMySQL: Ungültige Standardwert bei TIMESTAMP-

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

beim Ausführen der folgenden Abfrage

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

Ich bin mit MySQL 5.7.18

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

Nach der MySQL 5.7 documentation ist die folgende Syntax

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

Was ist falsch an der obigen SQL-Syntax?

+0

Nicht sicher, ob es das Problem ist, aber Sie brauchen nicht NULL mit DEFAULT? Versuchen Sie auch, zu entfernen (6). –

+0

Ich stimme zu, dass das NOT NULL das Problem sein kann, weil das Feld beim Einfügen anfänglich null ist, um nach dem Einfügen in den current_timestamp geändert zu werden. – Myonara

+0

Wie endet man mit Sachen wie int (* 10 *)!?! – Strawberry

Antwort

2

Interessanterweise sowohl diese Arbeit:

`start_time` timestamp(6), 

Und:

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

Sie letztere verwenden können - lassen Sie die Genauigkeitsbezeichner der Definition aus .

Aber die richtige Methode ist:

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

Wie im documentation erklärt:

Wenn eine TIMESTAMP oder DATETIME Spaltendefinition überall einen expliziten Sekundenbruchteile Präzisionswert enthält, der gleiche Wert muss be wird in der gesamten Spaltendefinition verwendet. Dies ist zulässig:

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

Dies ist nicht zulässig:

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

Versuchen:

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)