Computer >> Computer tutorials >  >> Programming >> MySQL

Rename column name in MySQL?


To rename column name in MySQL, you need to use the ALTER and CHANGE commands.

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 rename column name 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)