MySQL 8: How to rename a column of a table

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

Renaming a column in a MySQL database is a common task that may be required during database refactoring, optimizing, or simply correcting a typo. With MySQL 8, this has become a more streamlined process. In this tutorial, we’ll cover how to rename a column of a table in MySQL 8, with step-by-step examples showing the syntax and nuances of this operation.

Prerequisites:

  • A running MySQL 8 environment.
  • Basic understanding of SQL and working knowledge of MySQL.
  • Appropriate privileges to alter tables in the MySQL database.

Rename a Column: Basic Syntax

The basic syntax to rename a column in MySQL 8 uses the ALTER TABLE and CHANGE commands. The CHANGE command not only allows changing a column’s name but also its definition. The syntax is as follows:

ALTER TABLE `table_name`
CHANGE `old_column_name` `new_column_name` column_definition;

Let’s consider a simple example. We have a table named ’employees’ with a column ‘firstname’. We want to rename this column to ‘first_name’:

ALTER TABLE `employees`
CHANGE `firstname` `first_name` VARCHAR(50) NOT NULL;

The column ‘firstname’ will now be renamed to ‘first_name’, and we’ve reaffirmed its type and constraints as VARCHAR(50) NOT NULL.

Verifying the Change

After running the above command, it’s always a good practice to verify that the change has been applied:

DESCRIBE `employees`;

This statement will output the structure of the ’employees’ table, showing that ‘firstname’ has been successfully renamed to ‘first_name’.

Rename Multiple Columns

In MySQL 8, you can also rename multiple columns in a single ALTER TABLE statement by chaining CHANGE commands:

ALTER TABLE `employees`
CHANGE `firstname` `first_name` VARCHAR(50) NOT NULL,
CHANGE `lastname` `last_name` VARCHAR(50) NOT NULL;

This will rename both ‘firstname’ to ‘first_name’ and ‘lastname’ to ‘last_name’ with their respective type definitions intact.

Renaming Columns with Additional Changes

Renaming a column can also be combined with other table alterations. For example, you might want to rename a column and change its data type:

ALTER TABLE `employees`
CHANGE `age` `age_years` INT UNSIGNED NOT NULL;

In this example, the ‘age’ column is renamed to ‘age_years’ and its type is changed to INT UNSIGNED.

Handling Reserved Words

When you need to rename a column to a name that is a MySQL reserved word, you must use backticks to enclose the new column name:

ALTER TABLE `employees`
CHANGE `name` `order` VARCHAR(50) NOT NULL;

Since ‘ORDER’ is a reserved word in MySQL, we wrap it in backticks to avoid syntax errors.

Using the RENAME COLUMN Syntax

As of MySQL 8, a more intuitive RENAME COLUMN syntax is available. This allows you to simply specify the old and new column names without repeating the column’s type:

ALTER TABLE `employees`
RENAME COLUMN `first_name` TO `given_name`;

This command renames the ‘first_name’ column to ‘given_name’ without the need to specify the column type and details.

Transactions and Renaming

If you want to ensure that the rename operation can be rolled back in case of an error or decision change, you must use a transaction (assuming the storage engine supports transactions, like InnoDB):

START TRANSACTION;
ALTER TABLE `employees`
RENAME COLUMN `first_name` TO `given_name`;
-- If everything is correct
COMMIT;
-- If something goes wrong
ROLLBACK;

This will ensure that the changes are only permanent if you commit the transaction. Otherwise, a ROLLBACK will revert the changes made during the transaction.

Performance Considerations

When renaming a column, especially in a large table, consider the performance implications. The RENAME COLUMN syntax is generally faster and less impactful since it doesn’t require specifying the column definition. If using the CHANGE command, make sure you don’t unintentionally change other column attributes which could trigger a costly table-rebuild operation.

Conclusion

Renaming columns in MySQL 8 is a straightforward task with the help of the CHANGE or RENAME COLUMN syntax. Always make sure to have backups and use transactional safeguards with mission-critical tables. Test changes in a non-production environment to avoid unforeseen issues and downtime.