How to Rename Column Name in SQL?

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Methods to Rename a Column in SQL

We can use the following methods to rename a column in SQL.

  1. We can change the name of a column by using the ALTER command along with the RENAME COLUMN command.

Syntax:

The above syntax is applicable to MySQL, Oracle, and Postgres SQL.

  1. Another way to rename a column name is by using the CHANGE command along with the ALTER command.

Syntax:

The above command is applicable to MySQL and MariaDB.

  1. ALTER command cannot be used to change the column name while using SQL Server. For SQL Server sp_rename command is used.

Syntax:

Examples of Renaming Column Names in SQL

Let us now look at a few examples:

  1. Using ALTER along with the RENAME command.

Let us consider the following Table Employees_detail:

Emp_IDEmp_nameEmp_genderEmp_age
101Amar RathoreMale27
102Dimple KhannaFemale28
103Mayuri ChatterjiFemale27
104Karthik PadmanMale30
105Aisha KhanFemale29
106DiannaFemale27
107JaspreetMale30

We have to rename the column name Emp_age to age_of_emp.

SQL Query:

Output:

Emp_IDEmp_nameEmp_genderage_of_Emp
101Amar RathoreMale27
102Dimple KhannaFemale28
103Mayuri ChatterjiFemale27
104Karthik PadmanMale30
105Aisha KhanFemale29
106DiannaFemale27
107JaspreetMale30

Hence, we see that the column name Emp_age is now changed to age_of_Emp.

  1. Rename a column using ALTER with the CHANGEcommand.

Let us consider a table faculty.

f_IDf_namef_yoef_age
201Dr. Ajeya Krishna1047
202Dr. Sudhanshu Jha1548
203Dr. Gargi Sen1347
204Dr. Karthik Rao1650
205Dr. Aiman Shadab849
206Dr. M Dsouza1057
207Dr.Rajshekhar1250

We will rename column name f_name to phdf_name.

SQL query

Output:

f_IDphdf_namef_yoef_age
201Dr. Ajeya Krishna1047
202Dr. Sudhanshu Jha1548
203Dr. Gargi Sen1347
204Dr. Karthik Rao1650
205Dr. Aiman Shadab849
206Dr. M Dsouza1057
207Dr.Rajshekhar1250

The column name is changed from f_name to phdf_name.

  1. Example to show rename column name in SQL Server

Let us consider a table product_details:

Prod_IDbrandchocolatesales/day
055NestleMilky Bar10000000
056AmulDark Chocolate500000
057GandourSafari200000
058CadburyDairy Milk1000000
059NestleKitkat500000
060CadburySilk300000
061ParleMelody5000000

We will now rename the column name chocolate to prod_name.

SQL Query:

Output:

Prod_IDbrandprod_namesales/day
055NestleMilky Bar10000000
056AmulDark Chocolate500000
057GandourSafari200000
058CadburyDairy Milk1000000
059NestleKitkat500000
060CadburySilk300000
061ParleMelody5000000

Hence, the column name chocolate is now changed to prod_name.

Learn more

To learn the fundamentals of SQL to write queries and work progressively using multiple databases in-depth, refer to this article on SQL.

Conclusion

  • We learned the different ways to rename column names in SQL.
  • The most common ways that are supported by MySQL, Oracle, PostgreSQL, and MariaDB are to use the ALTER command using the RENAME command and the ALTER command using the CHANGE COLUMN command.
  • To change a column name in SQL Server sp_rename command is used, and here ALTER command cannot be used.

It's now your turn to play around with SQL queries and try out to rename column names in SQL.

See Also: