3

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;
Vérace
31k9 gold badges73 silver badges86 bronze badges
asked Mar 30, 2021 at 2:55
1
  • 3
    tip for questions, replace 'tried this but failed' and replace with 'tried this... which generated the error message....' Commented Mar 30, 2021 at 4:50

1 Answer 1

6

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.

answered Mar 30, 2021 at 4:47
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.