All we need is an easy explanation of the problem, so here it is.
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;
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
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.
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂