
- 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
Easy way to re-order columns in MySQL?
To re-order columns in MySQL, use the ALTER TABLE MODIFY COLUMN. The syntax is as follows -
ALTER TABLE yourTableName MODIFY COLUMN yourColumnName data type after yourColumnName.
To understand the above syntax, let us first create a table. The query to create a table is as follows.
mysql> create table reOrderColumn -> ( -> ProductId int, -> DeliveryDate datetime, -> ProductName varchar(100) -> ); Query OK, 0 rows affected (0.76 sec)
Now check the description of the table. The query is as follows.
mysql> desc reOrderColumn;
The following is the output.
+--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductId | int(11) | YES | | NULL | | | DeliveryDate | datetime | YES | | NULL | | | ProductName | varchar(100) | YES | | NULL | | +--------------+--------------+------+-----+---------+-------+ 3 rows in set (0.10 sec)
Now re-order the column using ALTER MODIFY command. I will reorder the DeliveryDate column after the ProductName column. The query is as follows.
mysql> alter table reOrderColumn modify column DeliveryDate datetime after ProductName; Query OK, 0 rows affected (1.61 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us now check the column have been reordered or not. The query is as follows.
mysql> desc reOrderColumn;
The following is the output displaying the columns have been reordered.
+--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductId | int(11) | YES | | NULL | | | ProductName | varchar(100) | YES | | NULL | | | DeliveryDate | datetime | YES | | NULL | | +--------------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
- Related Articles
- Is there an easy way to rename a table in a MySQL procedure?
- Is there any easy way to add multiple records in a single MySQL query?
- How to add mixed fractions in an easy way?
- How to order MySQL rows by multiple columns?
- How to use pip to install python modules in easy way?
- Return order of MySQL SHOW COLUMNS?
- Easy way to remember Strassen's Matrix Equation in C++
- Can we alter order of columns in MySQL?
- How will you explain Python namespaces in easy way?
- Order a MySQL table by two columns?
- Is there a way to name columns in an INSERT statement in MySQL?
- Snagit Tool: Taking Better Screenshots the Easy Way
- Easy way of installing Eclipse plugins on Ubuntu
- What is the easy way to make friends in a new school?
- How to order by the highest value from two columns in MySQL?

Advertisements