如何重命名 SQL 表中的列?

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

How can I rename my column in a SQL table?

sqlsql-serversql-server-2005

提问by Penguen

How can I rename a column via alter tablein MS SQL 2005?

如何alter table在 MS SQL 2005 中重命名列?

For example:

例如:

alter table tablename rename "old col name" to "new col name"

回答by Cambium

sp_rename 'TableName.ColumnName', 'NewColumnName', 'COLUMN'

回答by Damien_The_Unbeliever

i want to make it without sp_rename how can i do that alter..

我想在没有 sp_rename 的情况下制作它,我该怎么做才能改变..

You can't. You can create a new column in the table, using the new name, copy the contents of the old column into the new column, and then drop the old column (that's two ALTERs and an UPDATE), but the only way to do it otherwise is sp_rename.

你不能。您可以在表中创建一个新列,使用新名称,将旧列的内容复制到新列中,然后删除旧列(即两个 ALTER 和一个 UPDATE),但唯一的方法是否则是 sp_rename。

Here's a link to the ALTER TABLE documentation, where you can see what options are available to you. Change isn't one of them.

这是 ALTER TABLE文档的链接,您可以在其中查看可用的选项。变化不是其中之一。

This section of the documentation covers what you can do as part of an ALTER COLUMN clause of ALTER TABLE:

文档的这一部分涵盖了作为 ALTER TABLE 的 ALTER COLUMN 子句的一部分可以执行的操作:

ALTER COLUMN column_name 
{ 
    [ type_schema_name. ] type_name [ ( { precision [ , scale ] 
        | max | xml_schema_collection } ) ] 
    [ COLLATE collation_name ] 
    [ NULL | NOT NULL ] 
| {ADD | DROP } { ROWGUIDCOL | PERSISTED | NOT FOR REPLICATION}
}
ALTER COLUMN column_name 
{ 
    [ type_schema_name. ] type_name [ ( { precision [ , scale ] 
        | max | xml_schema_collection } ) ] 
    [ COLLATE collation_name ] 
    [ NULL | NOT NULL ] 
| {ADD | DROP } { ROWGUIDCOL | PERSISTED | NOT FOR REPLICATION}
}

Note, there's no mention of a new name. So, again, to repeat, you can't rename a column using ALTER TABLE, in SQL Server. If they implemented the standard syntax (which they don't), it would be ALTER TABLE [table_name] RENAME {COLUMN} [column_name] TO [new_column_name]

请注意,没有提到新名称。因此,再次重申,您不能在 SQL Server 中使用 ALTER TABLE 重命名列。如果他们实现了标准语法(他们没有),那就是 ALTER TABLE [table_name] RENAME {COLUMN} [column_name] TO [new_column_name]

回答by Hans Olsson

sp_renameas described here.

sp_rename作为描述在这里

Though I seem to remember that it can't always be used.

虽然我似乎记得它不能总是使用。

回答by Chirag Thakar

Try this to rename the column is

试试这个重命名列是

EXEC sp_RENAME 'table_name.old_Column_name', 'new_Column_name', 'COLUMN'

回答by Jennya

For MySQL the syntax is:

对于 MySQL,语法是:

ALTER TABLE <db>.<table> CHANGE COLUMN <old_column_name> <new_column_name> <column_type>;