Converting mysql column from INT to TIMESTAMP

后端 未结 1 991
一个人的身影
一个人的身影 2020-12-14 10:02

When I was yound and stupid had little experiance, I decided it would be a good idea, to generate timestamps in PHP and store them in INT column in my My

相关标签:
1条回答
  • 2020-12-14 10:46

    You're nearly there, use FROM_UNIXTIME() instead of directly copying the value.

    -- creating new column of TIMESTAMP type
    ALTER TABLE `pm`
      ADD COLUMN `date_sent2` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP();
    
    -- Use FROM_UNIXTIME() to convert from the INT timestamp to a proper datetime type
    -- assigning value from old INT column to it, in hope that it will be recognized as timestamp
    UPDATE `pm` SET `date_sent2` = FROM_UNIXTIME(`date_sent`);
    
    -- dropping the old INT column
    ALTER TABLE `pm` DROP COLUMN `date_sent`;
    
    -- changing the name of the column
    ALTER TABLE `pm` CHANGE `date_sent2` `date_sent` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP();
    
    0 讨论(0)
提交回复
热议问题