MySQL 如何在不丢失列数据的情况下更改MySQL表的列位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10718905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:30:08  来源:igfitidea点击:

How to change the column position of MySQL table without losing column data?

mysqlsql

提问by D S

I want to change the column positions of my database table without losing data.

我想在不丢失数据的情况下更改我的数据库表的列位置。

For example:

例如:

Current table:

当前表:

+----+------+-------+----------+
| id | name | email | password |
+----+------+-------+----------+

to

+----+----------+------+-------+
| id | password | name | email |
+----+----------+------+-------+

回答by Hearaman

Try this:

尝试这个:

ALTER TABLE table_name MODIFY password varchar(20) AFTER id

回答by Arman Ozak

Hearaman's answer is correct; but if you are using phpMyAdmin, there is a visual and practical way to do that.

Hearaman 的回答是正确的;但如果您使用的是 phpMyAdmin,则有一种直观且实用的方法可以做到这一点。

  1. Open the table
  2. Choose the "Structure" tab
  3. Click on "Move columns"
  4. Drag and drop column names
  1. 打开桌子
  2. 选择“结构”选项卡
  3. 点击“移动列
  4. 拖放列名称

Move columns link, middle of the Structure tabMove columns popup

移动列链接,结构选项卡中间移动列弹出窗口

回答by Mohemmed Niyaz

If you are using MySQL workbench,

如果您使用的是 MySQL 工作台,

  1. Right-click on table
  2. Alter table
  3. drag columns and re-order
  4. click apply and finish
  1. 右键单击表
  2. 改变表
  3. 拖动列并重新排序
  4. 单击应用并完成

回答by jundev

Also, you can do it like this:

此外,你可以这样做:

ALTER TABLE table_name CHANGE COLUMN column_name column_name data_type AFTER another_column_name;