【发布时间】:2020-06-02 20:59:41
【问题描述】:
我收到了一个数据库,我必须将其中一张表的日期格式更改为 msql 日期格式。我想知道如何在执行命令后将这些更改保存在 MySQL 上:
SELECT from_unixtime(last_post_date)
FROM it_forum;
我要更改的列如下:
【问题讨论】:
标签: mysql sql date mysql-workbench alter-table
我收到了一个数据库,我必须将其中一张表的日期格式更改为 msql 日期格式。我想知道如何在执行命令后将这些更改保存在 MySQL 上:
SELECT from_unixtime(last_post_date)
FROM it_forum;
我要更改的列如下:
【问题讨论】:
标签: mysql sql date mysql-workbench alter-table
您通常会创建一个新列,从旧列填充它,然后删除旧列:
-- 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):
alter table mytable change last_post_date last_post_date_old int;
之前:
|上次发布日期 | | -------------: | | 1591132456 |之后:
|上次发布日期 | | :----------------- | | 2020-06-02 22:14:16 |【讨论】: