Last Updated: 28 May, 2023
In MySQL, once a table is created and, for some reason, we need to rename or change the column name, MySQL has provided two solutions for renaming or changing the column name.
Use the ALTER TABLE statement with the RENAME COLUMN clause.
Use the ALTER TABLE statement with the CHANGE COLUMN clause.
To rename a column in MySQL, we can use the ALTER TABLE statement with the RENAME COLUMN clause.
The syntax and example are as follows:
Syntax:
ALTER TABLE table_name RENAME COLUMN column_name TO new_column_name;
Example:
ALTER TABLE student RENAME COLUMN roll TO student_roll;
To rename a column in MySQL, we can use the ALTER TABLE statement with the CHANGE COLUMN clause. The basic syntax is as follows:
ALTER TABLE table_name CHANGE old_column_name new_column_name data_type;
Here's what each part of the syntax means:
Example:
ALTER TABLE student COLUMN COLUMN roll student_roll VARCHAR(10);
This will change the column name from "roll" to "student_roll" and set its data type to varchar(10).
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com