MySQL - TIMESTAMP的默认值(3)

时间:2021-01-02 17:11:09

My database is MySql 5.6.

我的数据库是MySql 5.6。

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

我想使用CURRENT_TIMESTAMP作为默认值,属性类型为TIMESTAMP(3)。

But I get the error:

但我得到错误:

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

错误1067(42000):“已更新”的默认值无效

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

我认为这是因为CURRENT_TIMESTAMP的精度仅为秒。

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

如何将当前时间设置为带小数部分的时间戳的默认值?

1 个解决方案

#1


17  

As per documentation on timestamp and datetime type columns:

根据timestamp和datetime类型列的文档:

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.

如果TIMESTAMP或DATETIME列定义在任何位置包含显式小数秒精度值,则必须在整个列定义中使用相同的值。

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

请参阅:TIMESTAMP和DATETIME的初始化和更新

#1


17  

As per documentation on timestamp and datetime type columns:

根据timestamp和datetime类型列的文档:

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.

如果TIMESTAMP或DATETIME列定义在任何位置包含显式小数秒精度值,则必须在整个列定义中使用相同的值。

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

请参阅:TIMESTAMP和DATETIME的初始化和更新