Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Konvertieren der MySQL-Spalte von INT in TIMESTAMP

Sie haben es fast geschafft, verwenden Sie FROM_UNIXTIME() anstatt den Wert direkt zu kopieren.

-- 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();