How to rename a column in an existing MySQL table?

MySQLMySQLi Database

To rename a column in an existing MySQL table we can use ALTER TABLE command with CHANGE keyword as follows −

mysql> Alter table Student CHANGE Email Emailid Varchar(30);
Query OK, 5 rows affected (0.38 sec)
Records: 5 Duplicates: 0 Warnings: 0

With the help of query above, MySQL has changed the name of column ‘Email’ to ‘Emailid’.

We can specify the same data type & size or different data type & size with new column name as follows −

mysql> Alter table Student CHANGE Emailid Mailid char(35);
Query OK, 5 rows affected (0.29 sec)
Records: 5 Duplicates: 0 Warnings: 0

With the help of query above, MySQL has changed the name of the column from ‘Emailid’ to ‘Mailid’ along with its data type from varchar(30) to char(35).

raja
Published on 19-Feb-2018 08:36:09
Advertisements