ALTER TABLE RENAME - Rename Columns
The ALTER command is a DDL command to modify the structure of existing tables in the database by adding, modifying, renaming, or dropping columns and constraints. Use the ALTER TABLE RENAME command to rename column names.
Syntax:
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
For the demo purpose, consider the following Employee
table.
EmpId | FirstName | LastName | PhoneNo | Salary | Address | PinCode | |
---|---|---|---|---|---|---|---|
The following SQL script will rename PinCode
to ZipCode
in the Employee
table in Oracle, MySQL, PostgreSQL, SQLite database.
ALTER TABLE Employee RENAME COLUMN PinCode TO ZipCode;
Use the built-in procedure sp_rename
to changes the name of a user-created object in the database such as tables, indexes, columns, and alias data types in MS SQL Server. The following renames PinCode
to ZipCode
.
EXEC sp_rename 'Employee.PinCode', 'Employee.ZipCode';
The above ALTER TABLE RENAME SQL script will change the Employee
table as below.
EmpId | FirstName | LastName | PhoneNo | Salary | Address | ZipCode | |
---|---|---|---|---|---|---|---|
You cannot rename muliple columns in a single rename statement. Use different rename script to rename multiple columns.
The following RENAME script renames multiple column names.
ALTER TABLE Employee
RENAME COLUMN FirstName TO First_Name;
ALTER TABLE Employee
RENAME COLUMN PhoneNo TO Phone;