/    /  MySQL – Rename Column

MySQL – Rename Column

 

MySQL - Rename Column

The easiest way to rename a column is to use the ALTER TABLE command with the RENAME COLUMN clause. It has been available in MySQL since version 8.0.

Here is an example of its simple syntax. The following SQL statement can be entered in your MySQL shell to change a column name:

syntax:

ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;

You should replace table_name, old_column_name, and new_column_name with the names of the table and columns in your database. The name of a column cannot be renamed to one that already exists in the table.

Example:

ALTER TABLE employee RENAME COLUMN name TO First_name;

MySQL - Rename Column
Renaming Multiple Columns

MySQL can also change the names of multiple columns at the same time. Using the following syntax, we might be able to rename multiple columns at once:

Syntax:

ALTER TABLE table_name   
RENAME COLUMN old_column_name1 TO new_column_name1,  
RENAME COLUMN old_column_name2 TO new_column_name2,  
...  
...  
RENAME COLUMN old_column_nameN TO new_column_nameN;

or

ALTER TABLE table_name   
CHANGE old_column_name1 new_column_name1 Data Type,  
CHANGE old_column_name2 new_column_name2 Data Type,  
...  
...  
CHANGE old_column_nameN new_column_nameN Data Type;

Example:

ALTER TABLE employee   
CHANGE id cust_id int,   
CHANGE First_name cust_name varchar(45); 

MySQL - Rename Column