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

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

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

  1. SELECT from_unixtime(last_post_date)
  2. FROM it_forum;

我要更改为的列如下:

kxe2p93d

kxe2p93d1#

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

  1. -- rename the "old" column
  2. alter table mytable rename column last_post_date to last_post_date_old;
  3. -- create the "new" column
  4. alter table mytable add last_post_date datetime;
  5. -- feed the "new" column
  6. update mytable set last_post_date = from_unixtime(last_post_date_old);
  7. -- drop the "old" column
  8. alter table mytable drop column last_post_date_old;

你需要在工作台上停机才能安全运行。
注:以下为 rename 语法仅在mysql 8.0中可用。在早期版本中,您需要使用更麻烦的 change 语法,这需要重新说明数据类型(下面假定 int ):

  1. alter table mytable change last_post_date last_post_date_old int;

db小提琴演示
之前:

  1. | last_post_date |
  2. | -------------: |
  3. | 1591132456 |

之后:

  1. | last_post_date |
  2. | :------------------ |
  3. | 2020-06-02 22:14:16 |
展开查看全部

相关问题