Rename Columns in MySQL
Introduction
While working with tables, you may need to change the name of a column to make it more meaningful or correct mistakes.
In MySQL, this can be done using the ALTER TABLE statement.
What is Rename Column
Renaming a column means changing its name without affecting the data stored in it.
Only the column name changes, while:
Data remains the same.
Table structure remains intact.
Syntax (MySQL 8.0+)
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; Example
ALTER TABLE Students RENAME COLUMN name TO student_name; This changes the column name from name to student_name.
Alternative Method (Older Versions)
In older MySQL versions, you need to use MODIFY or CHANGE:
ALTER TABLE Students CHANGE name student_name VARCHAR(50); Note:
You must specify the data type again.
Important Points
Only the column name changes, not the data
Queries using old column names will stop working.
Make sure to update all references in your application.
Example Scenario
Suppose your table has:
CREATE TABLE Students ( id INT, name VARCHAR(50) ); You want a better column name:
ALTER TABLE Students RENAME COLUMN name TO full_name; Common Mistakes
Forgetting the exact column name
Not updating queries after renaming.
Missing data type in older syntax
Using unsupported syntax in older MySQL versions
Key Points to Remember
Use
ALTER TABLE RENAME COLUMNto rename columns.Data remains unchanged
Update all dependent queries.
Use CHANGE for older MySQL versions.