
- 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
Is it impossible to add a column in MySQL specifically before another column?
No, you can easily add a column before another column using ALTER.
Note − To add a column at a specific position within a table row, use FIRST or AFTER col_name Let us first create a table −
mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20), -> CountryName varchar(100) -> ); Query OK, 0 rows affected (0.67 sec)
Let us check all the column names from the table −
mysql> show columns from DemoTable;
Output
This will produce the following output −
+-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | varchar(20) | YES | | NULL | | | CountryName | varchar(100) | YES | | NULL | | +-------------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
Following is the query to add a column before another column in MySQL.
mysql> alter table DemoTable add Age int AFTER Name; Query OK, 0 rows affected (1.50 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check all the column names from the above table once again −
mysql> show columns from DemoTable;
Output
This will produce the following output. We have successfully added a column name −
+-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | varchar(20) | YES | | NULL | | | Age | int(11) | YES | | NULL | | | CountryName | varchar(100) | YES | | NULL | | +-------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
- Related Articles
- Add a temporary column in MySQL where the values depend on another column?
- Can we add a column to a table from another table in MySQL?
- Update a column based on another MySQL table’s column
- Fetch the first letter of a column value and insert it in another column with MySQL
- Update one column data to another column in MySQL if the second column is NOT NULL?
- Add records from corresponding duplicate values in another column with MySQL
- Add a column to a MySQL table which is the result of concatenation of text and value from another auto increment column?
- How to sum cells in a column if a condition is met in another column with MySQL?
- Add leading zeros to a MySQL column?
- How to add column from another DataFrame in Pandas?
- MySQL add “prefix” to every column?
- GROUP BY a column in another MySQL table
- How to add a NOT NULL column in MySQL?
- How to add a column in a table in MySQL?
- MySQL SELECT to add a new column to a query and give it a value?

Advertisements