134

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.

Thom A
  • 88,727
  • 11
  • 45
  • 75
MetroidFan2002
  • 29,217
  • 16
  • 62
  • 80

12 Answers12

142

Specifically for SQL Server, use sp_rename

USE AdventureWorks;
GO
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
GO
Rob Grant
  • 7,239
  • 4
  • 41
  • 61
Galwegian
  • 41,475
  • 16
  • 112
  • 158
  • 3
    It seems Microsoft-specific and nothing in the original query indicated a Microsoft DBMS. – bortzmeyer Oct 06 '08 at 14:54
  • 4
    Yes, the answer I was looking for is "standard" SQL, and not dependent on any particular implementation. However, it is a good answer for anyone using Microsoft's system. – MetroidFan2002 Oct 06 '08 at 15:12
  • 3
    Note to all: don't use something like `EXEC sp_rename '[Sales].[SalesTerritory].[TerritoryID]', '[TerrID]', 'COLUMN';`, as it will literally rename the column to `[TerrID]`, which will break a lot of stuff... talking from experience here... – JHBonarius Oct 14 '21 at 08:54
  • [Yes], [I'm] [not] [sure] [why] [the] [proverbial] [Bill] [Gates] [chose] [to] [use] [square] [brackets] [around] [identifier] [names]. [It's] [really] [dumb]! – Reversed Engineer Jun 07 '22 at 11:54
109

On PostgreSQL (and many other RDBMS), you can do it with regular ALTER TABLE statement:

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

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

=> SELECT * FROM Test1;
 id | baz | bar 
----+-----+-----
  2 |   1 |   2
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
bortzmeyer
  • 34,164
  • 12
  • 67
  • 91
40

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.

Dan Getz
  • 8,774
  • 6
  • 30
  • 64
jaspher chloe
  • 509
  • 5
  • 15
29

Unfortunately, for a database independent solution, you will need to know everything about the column. If it is used in other tables as a foreign key, they will need to be modified as well.

ALTER TABLE MyTable ADD MyNewColumn OLD_COLUMN_TYPE;
UPDATE MyTable SET MyNewColumn = MyOldColumn;
-- add all necessary triggers and constraints to the new column...
-- update all foreign key usages to point to the new column...
ALTER TABLE MyTable DROP COLUMN MyOldColumn;

For the very simplest of cases (no constraints, triggers, indexes or keys), it will take the above 3 lines. For anything more complicated it can get very messy as you fill in the missing parts.

However, as mentioned above, there are simpler database specific methods if you know which database you need to modify ahead of time.

Shadow Man
  • 3,234
  • 1
  • 24
  • 35
25

I think this is the easiest way to change column name.

SP_RENAME 'TABLE_NAME.OLD_COLUMN_NAME','NEW_COLUMN_NAME'
Marcel
  • 15,039
  • 20
  • 92
  • 150
Kunal Relan
  • 279
  • 3
  • 8
12

In sql server you can use

exec sp_rename '<TableName.OldColumnName>','<NewColumnName>','COLUMN'

or

sp_rename '<TableName.OldColumnName>','<NewColumnName>','COLUMN'
Bimzee
  • 1,138
  • 12
  • 15
9

In Informix, you can use:

RENAME COLUMN TableName.OldName TO NewName;

This was implemented before the SQL standard addressed the issue - if it is addressed in the SQL standard. My copy of the SQL 9075:2003 standard does not show it as being standard (amongst other things, RENAME is not one of the keywords). I don't know whether it is actually in SQL 9075:2008.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
6

You can use the following command to rename the column of any table in SQL Server:

exec sp_rename 'TableName.OldColumnName', 'New colunmName'
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
3

ALTER TABLE is standard SQL. But it's not completely implemented in many database systems.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
2

The standard would be ALTER TABLE, but that's not necessarily supported by every DBMS you're likely to encounter, so if you're looking for an all-encompassing syntax, you may be out of luck.

Rob
  • 47,999
  • 5
  • 74
  • 91
1

Alternatively to SQL, you can do this in Microsoft SQL Server Management Studio, from the table Design Panel.

First Way

Slow double-click on the column. The column name will become an editable text box.

Second Way

SqlManagement Studio>>DataBases>>tables>>specificTable>>Column Folder>>Right Click on column>>Reman

Third Way

Table>>RightClick>>Design

Syed Uzair Uddin
  • 3,296
  • 7
  • 31
  • 47
0

To rename you have to change the column

e.g

Suppose

*registration is Table Name

newRefereeName is a column name That I want to change to refereeName SO my SQL Query will be*

ALTER TABLE 'registration' CHANGE 'newRefereeName' 'refereeName' VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL;