如何重新排列 MySQL 列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2934312/
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
How to rearrange MySQL columns?
提问by Yeti
I need to move the position of existing columns (for better visibility).
我需要移动现有列的位置(以获得更好的可见性)。
How can this be done without affecting the data?
如何在不影响数据的情况下做到这一点?
采纳答案by vkGunasekaran
回答by soulmerge
The only way I know is to change the column. You would first extract your column definition using SHOW CREATE TABLE
and issue an ALTER TABLE
:
我知道的唯一方法是更改列。您将首先使用SHOW CREATE TABLE
并发出一个ALTER TABLE
:
ALTER TABLE foo
CHANGE COLUMN bar
bar COLUMN_DEFINITION_HERE
FIRST;
Or if you want it aftera certain other column:
或者,如果您想在某个其他列之后使用它:
... AFTER OTHER_COLUMN;
回答by shubham
- Alter Table table_name modify column_name column_datatype first;
- Alter Table table_name modify column_name column_datatype After other_column_name;
- 先Alter Table table_name 修改column_name column_datatype;
- Alter Table table_name 修改 column_name column_datatype 在 other_column_name 之后;
回答by zahid9i
Here is the sql query
ALTER TABLE table_name MODIFY COLUMN misplaced_column Column-definition AFTER other_column;Here in Column-definitionis full column definition. To see the column definition if you are using phpmyadmin click on structure tab. Then click on change link on desired column. Then withour modifyig any things click save. It will show you the sql. Copy the sql and just add *AFTER other_column* at the end. It will be all.
这里是sql查询
ALTER TABLE table_name MODIFY COLUMN misplaced_column Column-definition AFTER other_column; 这里的Column-definition是完整的列定义。如果您使用的是 phpmyadmin,要查看列定义,请单击结构选项卡。然后单击所需列上的更改链接。然后没有修改任何东西点击保存。它将向您显示sql。复制 sql 并在最后添加 *AFTER other_column* 。这将是全部。
If you like to bring the *misplaced_column* to the first position then ALTER TABLE table_name MODIFY COLUMN misplaced_column Column-definition FIRST;
如果你想把 *misplaced_column* 放在第一个位置,那么 ALTER TABLE table_name MODIFY COLUMN misplaced_column Column-definition FIRST;
However, It seems it is a duplicate question.
但是,这似乎是一个重复的问题。
回答by JRichardsz
Based on @VKGS answer:
基于@VKGS 回答:
If your table called languageis:
如果您的语言表是:
|---------------------|------------------|
| description | name |
|---------------------|------------------|
| object ... | java |
|---------------------|------------------|
| javascript... | nodejs |
|---------------------|------------------|
And you want to make the column name the first column or before description, execute this:
并且您想让列名成为第一列或描述之前,请执行以下操作:
ALTER TABLE language MODIFY description varchar(100) AFTER name;
Don't forget type of the column.
不要忘记列的类型。