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;
1
  • 3
    tip for questions, replace 'tried this but failed' and replace with 'tried this... which generated the error message....'
    – danblack
    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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.