在 SQL 中的另一列之后添加一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4084002/
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-09-01 08:07:21  来源:igfitidea点击:

Adding a column after another column within SQL

sql

提问by mattgcon

How do I add a column after another column within MS SQL by using SQL query?

如何使用 SQL 查询在 MS SQL 中的另一列之后添加一列?

TempTable ID int, Type nvarchar(20), Active bit

TempTable ID int,类型 nvarchar(20),活动位

NewTable ID int, Type nvarchar(20), Description text, Active bit

NewTable ID int,类型 nvarchar(20),描述文本,活动位

That is what I want, how do I do that

这就是我想要的,我该怎么做

回答by Hamish

Assuming MySQL (EDIT: posted before the SQL variant was supplied):

假设 MySQL(编辑:在提供 SQL 变体之前发布):

ALTER TABLE myTable ADD myNewColumn VARCHAR(255) AFTER myOtherColumn

The AFTER keyword tells MySQL where to place the new column. You can also use FIRST to flag the new column as the first column in the table.

AFTER 关键字告诉 MySQL 在哪里放置新列。您还可以使用 FIRST 将新列标记为表中的第一列。

回答by Hersheezy

It depends on what database you are using. In MySQL, you would use the "ALTER TABLE" syntax. I don't remember exactly how, but it would go something like this if you wanted to add a column called 'newcol' that was a 200 character varchar:

这取决于您使用的数据库。在 MySQL 中,您将使用“ALTER TABLE”语法。我不记得具体是怎么做的,但如果你想添加一个名为“newcol”的列,它是一个 200 个字符的 varchar,它会是这样的:

ALTER TABLE example ADD newCol VARCHAR(200) AFTER otherCol;

回答by Evangelos Bempelis

In a Firebird database the AFTER myOtherColumndoes not work but you can try re-positioning the column using:

在 Firebird 数据库中,AFTER myOtherColumn这不起作用,但您可以尝试使用以下方法重新定位列:

ALTER TABLE name ALTER column POSITION new_position

I guess it may work in other cases as well.

我想它也可能适用于其他情况。