Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 737
Hanxue Avatar asked Jul 29 '17 07:07

Hanxue


People also ask

What is the default value of timestamp in MySQL?

A TIMESTAMP column that permits NULL values does not take on the current timestamp at insert time except under one of the following conditions: Its default value is defined as CURRENT_TIMESTAMP and no value is specified for the column.

Is 0000 00 00 valid date?

You should use NULL because 0000-00-00 00:00:00 is not a valid date.

What is the format of timestamp in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.


2 Answers

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. 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)
);
like image 105
Gordon Linoff Avatar answered Nov 16 '22 02:11

Gordon Linoff


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)
like image 1
wchiquito Avatar answered Nov 16 '22 01:11

wchiquito