SQL 在表格中间插入新列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1740838/
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
Inserting new columns in the middle of a table?
提问by Robert Gould
When one uses "ALTER TABLE tab ADD col", the new column gets added to the end of the table. For example:
当使用“ALTER TABLE tab ADD col”时,新列被添加到表的末尾。例如:
TABLE: TAB
COL_1 COL_2 COL_4
ALTER TABLE TAB ADD COL_3
table will become
表将成为
TABLE: TAB
COL_1 COL_2 COL_4 COL_3
However as the naming of my example columns suggests I'd actually like the table to end up like this:
然而,正如我的示例列的命名所暗示的那样,我实际上希望表格最终是这样的:
TABLE: TAB
COL_1 COL_2 COL_3 COL_4
With COL_3 before COL_4.
在 COL_4 之前使用 COL_3。
Besides rebuilding the table from scratch, is there any standard SQL that will get the job done? However if there is no standard SQL, I could still use some vendor dependent solutions for Oracle, but again a standard solution would be best.
除了从头开始重建表之外,是否有任何标准 SQL 可以完成工作?但是,如果没有标准 SQL,我仍然可以使用一些供应商相关的 Oracle 解决方案,但同样最好使用标准解决方案。
Thanks.
谢谢。
回答by Raj More
By default, columns are only added at the end.
默认情况下,列仅添加到末尾。
To insert a column in the middle, you have to drop and recreate the table and all related objects (constraints, indices, defaults, relationships, etc).
要在中间插入一列,您必须删除并重新创建表和所有相关对象(约束、索引、默认值、关系等)。
Several tools do this for you, and depending on the size of the table, this may be an intensive operation.
有几种工具可以为您完成此操作,并且根据表的大小,这可能是一项密集操作。
You may also consider creating views on the table that display columns in the order of preferrence (overriding the actual order in the table).
您还可以考虑在表上创建按优先顺序显示列的视图(覆盖表中的实际顺序)。
回答by user2894607
It works.
有用。
ALTER TABLE tablename ADD columnname datatype AFTER columnname;
回答by ysth
http://www.orafaq.com/wiki/SQL_FAQ#How_does_one_add_a_column_to_the_middle_of_a_table.3Fsays it can't be done, and suggests workarounds of renaming the table and doing a create table as select...
or (something I am unfamiliar with) "Use the DBMS_REDEFINITION package to change the structure".
http://www.orafaq.com/wiki/SQL_FAQ#How_does_one_add_a_column_to_the_middle_of_a_table.3F表示无法完成,并建议重命名表并执行create table as select...
或(我不熟悉的)“使用 DBMS_REDEFINITION 包进行更改结构”。
回答by Orhan Cinar
ALTER TABLE TABLENAME ALTER COLUMN COLUMNNAME POSITION X;
ALTER TABLE TABLENAME ALTER COLUMN COLUMNNAME 位置 X;
回答by Sherazin
I know it's old subject (2009) but maybe it will help someone that still looks for an answer. In MySQL, it works 2 add a column anywhere in the table.
我知道这是旧主题(2009 年),但也许它会帮助仍在寻找答案的人。在 MySQL 中,它的工作原理是 2 在表中的任何位置添加一列。
ALTER TABLE `tablename` ADD `column_name1` TEXT NOT NULL AFTER `column_name2`;
This is 2 enter a text column, but u can set whatever properties u want for the new column, just make sure u write them with caps.
这是 2 输入一个文本列,但您可以为新列设置您想要的任何属性,只需确保用大写字母书写它们即可。
I found it with Xampp, MySQL admin, when i used it 2 insert a column in the middle of a MySQL table.
我在 Xampp,MySQL 管理员中找到了它,当我使用它时 2 在 MySQL 表的中间插入一列。
Hope it helps.
希望能帮助到你。
回答by mouli mlsn172
As per my small research the only way is to create the views of the previous table in a required order of columns or else recreate the table from the scratch and in some sql query tools the 'AFTER' keyword might work".
根据我的小型研究,唯一的方法是按照所需的列顺序创建前一个表的视图,或者从头开始重新创建表,并且在某些 sql 查询工具中,'AFTER' 关键字可能会起作用”。