mysql
  1. mysql-rename-column

Rename Column - (MySQL Table & Views)

In MySQL, you may need to rename a column in a table or view for various reasons. Renaming a column is a simple task that can be done using the ALTER TABLE command. In this tutorial, we'll cover how to rename a column in MySQL table or view.

Syntax

The syntax for renaming a column in MySQL is as follows:

ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;

In the above syntax:

  • table_name is the name of the table or view that contains the column you want to rename.
  • old_column_name is the name of the column you want to rename.
  • new_column_name is the new name you want to give to the column.

Example

Suppose we have a users table with a column named fname. We want to rename this column to first_name. We can do this using the following command:

ALTER TABLE users RENAME COLUMN fname TO first_name;

After executing this command, the fname column will be renamed to first_name.

Output

When we run the above command, it will execute successfully, and there will be no output.

Explanation

In the example above, we used the ALTER TABLE command to rename the fname column in the users table to first_name.

Use

Renaming a column is useful when you want to change the name of a column in a table or view without affecting any other data in the table or view. Additionally, renaming a column can make your database schema more consistent and easier to understand.

Important Points

  • Renaming a column does not affect any data in the table or view.
  • Make sure to update any references to the old column name in your application code or SQL queries to use the new column name.
  • You must have ALTER privilege on the table or view to rename a column.

Summary

In this tutorial, we discussed how to rename a column in MySQL table or view using the ALTER TABLE command. We covered the syntax, example, output, explanation, use, and important points of renaming a column in MySQL. With this knowledge, you can now easily rename columns in your MySQL tables or views.

Published on: