如何移动 MySQL 表中的列?
目前我有以下 MySQL 表:Employees (empID, empName, Department);
我想将该表更改为以下内容:Employees (empID, Department, empName);
>
如何使用 ALTER
语句来完成此操作?
注意:我只想更改列位置。
Currently I am having the following MySQL table: Employees (empID, empName, department);
I want to change the table to the following: Employees (empID, department, empName);
How can this be done using ALTER
statements?
Note: I want to change only column positions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果
empName
是VARCHAR(50)
列:EDIT
根据注释,您还可以执行以下操作:
请注意
的重复empName
是故意的。你必须告诉 MySQL 你想要保留相同的列名。您应该知道这两个语法版本都是 MySQL 特定的。例如,它们在 PostgreSQL 或许多其他 DBMS 中不起作用。
另一个编辑:正如 @Luis Rossi 在评论中指出的,您需要在
AFTER
修饰符之前完全指定更改的列定义。上面的示例仅包含VARCHAR(50)
,但如果您需要其他特征(例如NOT NULL
或默认值),则还需要包含这些特征。请参阅有关ALTER TABLE
的文档 了解更多信息。If
empName
is aVARCHAR(50)
column:EDIT
Per the comments, you can also do this:
Note that the repetition of
empName
is deliberate. You have to tell MySQL that you want to keep the same column name.You should be aware that both syntax versions are specific to MySQL. They won't work, for example, in PostgreSQL or many other DBMSs.
Another edit: As pointed out by @Luis Rossi in a comment, you need to completely specify the altered column definition just before the
AFTER
modifier. The above examples just haveVARCHAR(50)
, but if you need other characteristics (such asNOT NULL
or a default value) you need to include those as well. Consult the docs onALTER TABLE
for more info.更改列位置:
如果需要将其移动到第一个位置,则必须在 ALTER TABLE CHANGE [COLUMN] 查询末尾使用术语 FIRST:
Change column position:
If you need to move it to the first position you have to use term FIRST at the end of ALTER TABLE CHANGE [COLUMN] query:
phpMyAdmin 在表的结构视图中为此提供了一个 GUI。
选中要移动的列,然后单击列列表底部的更改操作。
然后,您可以更改所有列属性,并且您会在屏幕最右侧找到“移动列”功能。
当然,这只是在完美的最佳答案中构建查询,但 GUI 粉丝可能会喜欢替代方案。
我的 phpMyAdmin 版本是 4.1.7
phpMyAdmin provides a GUI for this within the structure view of a table.
Check to select the column you want to move and click the change action at the bottom of the column list.
You can then change all of the column properties and you'll find the 'move column' function at the far right of the screen.
Of course this is all just building the queries in the perfectly good top answer but GUI fans might appreciate the alternative.
my phpMyAdmin version is 4.1.7
我必须对产品后期引入的列在 10 多个表上运行此操作。因此编写了这个快速的凌乱脚本来为所有“相关”表生成更改命令。
这将为架构中包含您尝试移动到不同位置的列的所有表生成 ALTER TABLE 命令。
I had to run this for a column introduced in the later stages of a product, on 10+ tables. So wrote this quick untidy script to generate the alter command for all 'relevant' tables.
This will generate an ALTER TABLE command for all TABLES in the schema that contain the COLUMN that you're trying to move to a different position.
在 SQL 中:
如果您想将
id
列移动到第一位,我们有一个查询
,如下所示:在此查询中,信息如下:
但是
如果您想将一列移动到另一列之后
,则意味着您的 A 列可能位于第二列,并且您希望将其移动到 B 列之后的表的最后一个位置,因此请使用以下查询:该查询的信息如下:
你想要移动(A 列)。
在 PHPMYADMIN 中:
+
在表格内,单击COLUMNS
。更改
列
action
(您要移动的列)。现在您看到另一个页面,移动列
。这是选择选项并选择地方你想移动该列。
保存
按钮。In SQL :
If you want to move
id
column to the first place,we have a query for that
, is like below:In this query, information is like below:
But
if you want to move a column after another column
, mean maybe your A column is at the secound and you want to move it to the last place of your table after B column so use this query:The information of this query is like below:
you want to move (A column).
In PHPMYADMIN :
+
inside of your table, click onCOLUMNS
.change
undercolumn
action
(column you want to move). Now you see another page,Move column
. It is select option and choose placeyou want to move that column.
save
button.对于使用TablePlus的用户,您可以标记所有表格,右键->;复制,在新表中->粘贴。
For those using TablePlus, you can just mark all tables, right click -> Copy, in the new table -> Paste.