How to Rename a Column in MySQL

March 30, 2020

Introduction

MySQL provides a dynamic environment that enables you to alter database items with a few basic commands. By learning how to use various statements, you can manage your databases with ease.

This tutorial contains all the commands needed to rename a column in a MySQL database.

how to change column name in mysql

Prerequisites

Rename MySQL Column with ALTER TABLE Command

ALTER TABLE is an essential command used to change the structure of a MySQL table. You can use it to add or delete columns, change the type of data within the columns, and even rename entire databases. The function that concerns us the most is how to utilize ALTER TABLE to rename a column.

Statements give us additional control over the renaming process. The RENAME COLUMN and CHANGE statements both allow for the names of existing columns to be altered. The difference is that the CHANGE clause can also be used to alter the data types of a column.

Rename MySQL Column with the RENAME Statement

The simplest way to rename a column is to use the ALTER TABLE command with the RENAME COLUMN clause. This clause is available since MySQL version 8.0.

Let’s illustrate its simple syntax. To change a column name, enter the following statement in your MySQL shell:

ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;

Replace table_name, old_column_name, and new_column_name with your table and column names. Keep in mind that you cannot rename a column to a name that already exists in the table.

For instance, to change the column id into employee_id in the table employees, you would run:

ALTER TABLE employees RENAME COLUMN id TO employ_id;

The RENAME COLUMN statement can only be used to rename a column. If you need additional functions, such as changing the data definition, or position of a column, use the CHANGE clause instead.

Note: The word COLUMN is obligatory for the ALTER TABLE RENAME COLUMN command. ALTER TABLE RENAME is the existing syntax to rename the entire table.

Rename MySQL Column with the CHANGE Statement

The CHANGE clause offers important additions to the renaming process. It can be used to rename a column and change the data type of that column with the same command.

Enter the following command in your MySQL client shell to change the name of the column and its definition:

ALTER TABLE table_name CHANGE old_column_name new_col_name Data Type;

You can change the data type of the column or keep the existing one. In both cases you have to specify the data type as the element is mandatory.

For example, to change the column id into employee_id which has the data type VARCHAR(25) in the table employees, you would run:

ALTER TABLE employees CHANGE id employ_id VARCHAR(25);

Note: If you don't know the data type of the column you are renaming, check the structure of the table and the column definition using the DESCRIBE statement: DESCRIBE table_name;.

Additional Options

You can use additional options to further manipulate table columns. The CHANGE also allows you to place the column in a different position in the table by using the optional FIRST | AFTER column_name clause. For example:

ALTER TABLE table_name CHANGE old_column_name new_col_name Data Type AFTER column_x;

With the command above you can changed the name of the column, changed the data type to y_data_type, and positioned the column after column_x.

Rename Multiple MySQL Columns

MySQL allows you to rename multiple columns with a single command. This option is possible with the RENAME and the CHANGE statement.

To change the names of multiple columns using the RENAME COLUMN clause, use the syntax:

ALTER TABLE table_name 
RENAME COLUMN old_column_name1 TO new_col_name1,
RENAME COLUMN old_column_name2 TO new_col_name2,
RENAME COLUMN old_column_name3 TO new_col_name3;

To change the names of multiple columns using the CHANGE clause, use the syntax:

ALTER TABLE table_name 
CHANGE old_column_name1 new_col_name1 Data Type,
CHANGE old_column_name2 new_col_name2 Data Type,
CHANGE old_column_name3 new_col_name3 Data Type;

Conclusion

You have successfully renamed an existing column in your MySQL database. This article has offered two options and provided the necessary commands. Understanding the essential ALTER TABLE statement is a precondition for exploring more complex expressions.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Allow Remote Connections to MySQL
March 26, 2020

Keep your database servers separate from your public-facing web servers to improve security, performance, and...
Read more
How To Find Duplicate Values in MySQL
March 4, 2020

Learn how to find the duplicate entries in your MySQL databases. The guide shows you how to use the GROUP BY...
Read more
How to Improve MySQL Performance With Tuning
January 15, 2020

The performance of MySQL databases is an essential factor in the optimal operation of your server. Make sure...
Read more
How to Fix MySQL "Command Not Found" (Linux, Windows, mac OS)
December 11, 2019

The 'Command Not Found' error is a general error not only found in MYSQL. By learning how to deal with it...
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.