Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - default value for TIMESTAMP(3)

Tags:

mysql

My database is MySql 5.6.

I want to use CURRENT_TIMESTAMP as the default value an attribute which is type of TIMESTAMP(3).

But I get the error:

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

I think it is because CURRENT_TIMESTAMP is only in precision of second.

How can I set current time as the default value for a timestamp with fractional part?

like image 312
cli130 Avatar asked May 15 '14 06:05

cli130


People also ask

What is the default value for 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.

How do I add a default value to a TIMESTAMP column?

Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP. The DEFAULT clause also can be used to specify a constant (nonautomatic) default value; for example, DEFAULT 0 or DEFAULT '2000-01-01 00:00:00'.

What should be in a MySQL TIMESTAMP?

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.

How is TIMESTAMP stored in MySQL?

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME, which is stored “as is”.) By default, the current time zone for each connection is the server's time.


1 Answers

As per documentation on timestamp and datetime type columns:

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

Other Examples:

mysql> create table tbl_so_q23671222_1( ts timestamp(3) default now() );
ERROR 1067 (42000): Invalid default value for 'ts'

mysql> create table tbl_so_q23671222_1( ts timestamp(3) default now(3) );
Query OK, 0 rows affected (0.59 sec)

mysql> create table tbl_so_q23671222_2( ts timestamp(3) default current_timestamp );
ERROR 1067 (42000): Invalid default value for 'ts'

mysql> create table tbl_so_q23671222_2( ts timestamp(3) default current_timestamp(3) );
Query OK, 0 rows affected (0.38 sec)

mysql> desc tbl_so_q23671222_1;
+-------+--------------+------+-----+----------------------+-------+
| Field | Type         | Null | Key | Default              | Extra |
+-------+--------------+------+-----+----------------------+-------+
| ts    | timestamp(3) | NO   |     | CURRENT_TIMESTAMP(3) |       |
+-------+--------------+------+-----+----------------------+-------+
1 row in set (0.01 sec)

mysql> desc tbl_so_q23671222_2;
+-------+--------------+------+-----+----------------------+-------+
| Field | Type         | Null | Key | Default              | Extra |
+-------+--------------+------+-----+----------------------+-------+
| ts    | timestamp(3) | NO   |     | CURRENT_TIMESTAMP(3) |       |
+-------+--------------+------+-----+----------------------+-------+
1 row in set (0.01 sec)

Refer to:
Initialization and Updating for TIMESTAMP and DATETIME

like image 111
Ravinder Reddy Avatar answered Oct 14 '22 16:10

Ravinder Reddy