如何在MYSQL列中保存数据更改

如何在MYSQL列中保存数据更改,mysql,sql,date,mysql-workbench,alter-table,Mysql,Sql,Date,Mysql Workbench,Alter Table,我收到了一个数据库,我不得不将其中一个表上的日期格式更改为msql日期格式。我想知道,一旦我执行了以下命令,如何在MySQL上保存这些更改: SELECT from_unixtime(last_post_date) FROM it_forum; 我要更改为的列如下: 您通常会创建一个新列,从旧列中填充,然后删除旧列: -- rename the "old" column alter table mytable rename column last_post_date to last_post

我收到了一个数据库,我不得不将其中一个表上的日期格式更改为msql日期格式。我想知道,一旦我执行了以下命令,如何在MySQL上保存这些更改:

SELECT from_unixtime(last_post_date) 
FROM it_forum;
我要更改为的列如下:


您通常会创建一个新列,从旧列中填充,然后删除旧列:

-- 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
):

之前:

| last_post_date | | -------------: | | 1591132456 | |最后发布日期| | -------------: | | 1591132456 | 之后:

| last_post_date | | :------------------ | | 2020-06-02 22:14:16 | |最后发布日期| | :------------------ | | 2020-06-02 22:14:16 | | last_post_date | | :------------------ | | 2020-06-02 22:14:16 |