I have created a created_time
field with BIGINT
in a MySQL 5.7 table, now I want to auto generate a Unix millisecond timestamp when I insert a record. Is it possible to do this? I have tried the code below but it failed:
ALTER TABLE db.video_info MODIFY COLUMN created_time bigint(20)
DEFAULT (ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000)) NULL;
-
3tip for questions, replace 'tried this but failed' and replace with 'tried this... which generated the error message....'danblack– danblack2021年03月30日 04:50:38 +00:00Commented Mar 30, 2021 at 4:50
1 Answer 1
MySQL 8.0 supports the expression default like you show.
MySQL 5.7 does not support expressions as default. Only NULL, or a constant value, or CURRENT_TIMESTAMP
if it's a DATETIME or TIMESTAMP column.
For MySQL 5.7, you have two alternatives:
Declare the column as
created_time datetime(3) default current_timestamp(3)
.Use BIGINT as you are doing, but write a trigger to set the value.
-
ref: documentation on data type defaultsdanblack– danblack2021年03月30日 04:49:17 +00:00Commented Mar 30, 2021 at 4:49