Purpose of using CHANGE command in MySQL?


The CHANGE command in MySQL is used to rename column name. Let us first create a table −

mysql> create table DemoTable796 (
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,Name varchar(100),StudentAge int
);
Query OK, 0 rows affected (0.56 sec)

Let us check the description of table −

mysql> desc DemoTable796;

This will produce the following output −

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| StudentId  | int(11)      | NO   | PRI | NULL    | auto_increment |
| Name       | varchar(100) | YES  |     | NULL    |                |
| StudentAge | int(11)      | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Following is the query to use CHANGE command in MySQL −

mysql> alter table DemoTable796 change Name StudentName varchar(100);
Query OK, 0 rows affected (0.29 sec)
Records: 0 Duplicates: 0 Warnings: 0

Let us check the description of table once again −

mysql> desc DemoTable796;

This will produce the following output −

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| StudentId   | int(11)      | NO   | PRI | NULL    | auto_increment |
| StudentName | varchar(100) | YES  |     | NULL    |                |
| StudentAge  | int(11)      | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Updated on: 02-Jul-2020

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements