
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I change the name of an existing column from a MySQL table?
We can change the name of a particular existing column from a MySQL table by using CHANGE statement along with ALTER statement. Its syntax would be as follows −
Syntax
ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;
Here,
- table_name is the name of the table from which we want to delete the column.
- Old_column_name is the name of the column which is to be changed.
- new_column_name is the name of the column which has to be given to the old column.
Example
In this example, we are changing the name of the column ‘id’ to ‘studentid’ from table ‘student_info’ as follows −
mysql> Select * from Student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | +------+---------+------------+------------+ 5 rows in set (0.02 sec) mysql> ALTER TABLE Student_info CHANGE id studentid int; Query OK, 0 rows affected (0.22 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> Select * from Student_info; +-----------+---------+------------+------------+ | studentid | Name | Address | Subject | +-----------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | +-----------+---------+------------+------------+ 5 rows in set (0.00 sec)
The above result set shows that the name of the column ‘id’ has been changed to ‘Studentid’.
- Related Articles
- How can I drop an existing column from a MySQL table?
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How can I see the CREATE TABLE statement of an existing MySQL table?
- How can we create a table from an existing MySQL table in the database?
- Change the column name from a MySQL table with Student record?
- How can we change the name of a MySQL table?
- How can we put comments in a column of existing MySQL table?
- How to rename a column in an existing MySQL table?
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- How to change the datatype of a column in an existing table using JDBC API?
- How can I change the storage engine of a MySQL table?
- How can I change the value of an instance of a row in MySQL table?

Advertisements