
- 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 to alter multiple columns in a single statement in MySQL?
Alter multiple columns in a single statement with the help of CHANGE command. The syntax is as follows −
alter table yourTableName change yourOldColumnName1 yourNewColumnName1 dataType, yourOldColumnName2 yourNewColumnName2 dataType, . . . N
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table AlterMultipleColumns −> ( −> Id int, −> Name varchar(200) −> ); Query OK, 0 rows affected (0.93 sec)
Now we have two columns with Id and Name. We will alter both the columns.
Here, we will alter Id with StudentId and Name with StudentName. The query to alter multiple columns is as follows −
mysql> alter table AlterMultipleColumns −> change Id StudentId varchar(10), −> change Name StudentName varchar(100); Query OK, 0 rows affected (1.82 sec) Records: 0 Duplicates: 0 Warnings: 0
Now you can check the columns have been altered or not with help of desc command. The query is as follows −
mysql> desc AlterMultipleColumns;
The following is the output −
+-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+ | StudentId | varchar(10) | YES | | NULL | | | StudentName | varchar(100) | YES | | NULL | | +-------------+--------------+------+-----+---------+-------+ 2 rows in set (0.06 sec)
- Related Articles
- How to alter column type of multiple columns in a single MySQL query?
- How to check multiple columns for a single value in MySQL?
- Change multiple columns in a single MySQL query?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- MySQL query to sort multiple columns together in a single query
- Select multiple columns and display in a single column in MySQL?
- Update multiple columns of a single row MySQL?
- Concatenate multiple rows and columns in a single row with MySQL
- Insert multiple sets of values in a single statement with MySQL?
- MySQL Select Statement DISTINCT for Multiple Columns?
- How to search multiple columns in MySQL?
- How can we sort multiple columns in a single query?
- Can we alter order of columns in MySQL?
- How to get multiple rows in a single MySQL query?
- How to obtain multiple rows in a single MySQL query?

Advertisements