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

xesrikrc  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(424)

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

SELECT from_unixtime(last_post_date) 
FROM it_forum;

我要更改为的列如下:

kxe2p93d

kxe2p93d1#

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

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

db小提琴演示
之前:

| last_post_date |
| -------------: |
|     1591132456 |

之后:

| last_post_date      |
| :------------------ |
| 2020-06-02 22:14:16 |

相关问题