我收到了一个数据库,我不得不将其中一个表上的日期格式更改为msql日期格式。我想知道如何在mysql上保存这些更改,一旦我执行了以下命令:
SELECT from_unixtime(last_post_date) FROM it_forum;
SELECT from_unixtime(last_post_date)
FROM it_forum;
我要更改为的列如下:
kxe2p93d1#
您通常会创建一个新列,从旧列填充它,然后删除旧列:
-- rename the "old" columnalter table mytable rename column last_post_date to last_post_date_old; -- create the "new" columnalter table mytable add last_post_date datetime;-- feed the "new" columnupdate mytable set last_post_date = from_unixtime(last_post_date_old);-- drop the "old" columnalter table mytable drop column last_post_date_old;
-- rename the "old" column
alter table mytable rename column last_post_date to last_post_date_old;
-- create the "new" column
alter table mytable add last_post_date datetime;
-- feed the "new" column
update mytable set last_post_date = from_unixtime(last_post_date_old);
-- drop the "old" column
alter table mytable drop column last_post_date_old;
你需要在工作台上停机才能安全运行。注:以下为 rename 语法仅在mysql 8.0中可用。在早期版本中,您需要使用更麻烦的 change 语法,这需要重新说明数据类型(下面假定 int ):
rename
change
int
alter table mytable change last_post_date last_post_date_old int;
db小提琴演示之前:
| last_post_date || -------------: || 1591132456 |
| last_post_date |
| -------------: |
| 1591132456 |
之后:
| last_post_date || :------------------ || 2020-06-02 22:14:16 |
| :------------------ |
| 2020-06-02 22:14:16 |
1条答案
按热度按时间kxe2p93d1#
您通常会创建一个新列,从旧列填充它,然后删除旧列:
你需要在工作台上停机才能安全运行。
注:以下为
rename
语法仅在mysql 8.0中可用。在早期版本中,您需要使用更麻烦的change
语法,这需要重新说明数据类型(下面假定int
):db小提琴演示
之前:
之后: