SQL > ALTER TABLE > Rename Column Syntax

Sometimes we want to change the name of a column. To do this in SQL, we specify that we want to change the structure of the table using the ALTER TABLE command, followed by a command that tells the relational database that we want to rename the column. The exact syntax for each database is as follows:

In MySQL, the SQL syntax for ALTER TABLE Rename Column is,

ALTER TABLE "table_name"
Change "column 1" "column 2" ["Data Type"];

In Oracle, the syntax is,

ALTER TABLE "table_name"
RENAME COLUMN "column 1" TO "column 2";

Let's look at the example. Assuming our starting point is the Customer table created in the CREATE TABLE section:

Table Customer

Column NameData Type
First_Namechar(50)
Last_Namechar(50)
Addresschar(50)
Citychar(50)
Countrychar(25)
Birth_Datedatetime

To rename "Address" to "Addr", we key in,

MySQL:

ALTER TABLE Customer CHANGE Address Addr char(50);

Oracle:

ALTER TABLE Customer RENAME COLUMN Address TO Addr;

SQL Server:
It is not possible to rename a column using the ALTER TABLE statement in SQL Server. Use sp_rename instead.

The resulting table structure is:

Table Customer

Column NameData Type
First_Namechar(50)
Last_Namechar(50)
Addrchar(50)
Citychar(50)
Countrychar(25)
Birth_Datedatetime

Next: SQL DROP COLUMN




Copyright © 2014   1keydata.com   All Rights Reserved.     Privacy Policy