How do I rename a column in a database table using SQL?

If I wish to simply rename a column (not change its type or constraints, just its name) in an SQL database using SQL, how do I do that? Or is it not possible?

This is for any database claiming to support SQL, I'm simply looking for an SQL-specific query that will work regardless of actual database implementation.


在PostgreSQL(以及其他许多RDBMS)上,您可以使用常规的ALTER TABLE语句来完成它:

essais=> SELECT * FROM Test1;
 id | foo | bar 
----+-----+-----
  2 |   1 |   2

essais=> ALTER TABLE Test1 RENAME COLUMN foo TO baz;
ALTER TABLE

essais=> SELECT * FROM Test1;
 id | baz | bar 
----+-----+-----
  2 |   1 |   2

特别是对于SQL Server,请使用sp_rename

USE AdventureWorks;
GO
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
GO

In MySQL, the syntax is ALTER TABLE ... CHANGE :

ALTER TABLE <table_name> CHANGE <column_name> <new_column_name> <data_type> ...

Note that you can't just rename and leave the type and constraints as is; you must retype the data type and constraints after the new name of the column.

链接地址: http://www.djcxy.com/p/24756.html

上一篇: 如何克隆一个JavaScript ES6类实例

下一篇: 如何使用SQL重命名数据库表中的列?