
- 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
Add a new column and index to an existing table with ALTER in a single MySQL query?
To add a new column to an existing table, use ADD. With that, to add a new index, use the ADD INDEX(). Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(100), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.69 sec)
Let us check the description of the table −
mysql> desc DemoTable;
This will produce the following output −
+-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(100) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec)
Here is the query to add a new column and index to an existing table −
mysql> alter table DemoTable -> add ArrivalDate DATETIME DEFAULT NOW(), -> add index(ArrivalDate); Query OK, 0 rows affected (2.05 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of the table once again −
mysql> desc DemoTable;
This will produce the following output −
+-------------+--------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+-------------------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(100) | YES | | NULL | | | ArrivalDate | datetime | YES | MUL | CURRENT_TIMESTAMP | | +-------------+--------------+------+-----+-------------------+----------------+ 3 rows in set (0.01 sec)
- Related Articles
- How to add column and index in a single MySQL query?
- How to add a new column to an existing table using JDBC API?
- Adding new enum column to an existing MySQL table?
- How to add a new column to an existing table of Laravel in a migration?
- Adding a new NOT NULL column to an existing table with records
- How to alter column type of multiple columns in a single MySQL query?
- Inserting data into a new column of an already existing table in MySQL?
- How can we add multiple columns, with single command, to an existing MySQL table?
- Create MySQL query to create a table from an existing table?
- How to rename a column in an existing MySQL table?
- MySQL SELECT to add a new column to a query and give it a value?
- How to add column to an existing table in PostgreSQL?
- ALTER TABLE to add a composite primary key in MySQL?
- Alter a table column from VARCHAR to NULL in MySQL
- MySQL query to add days with interval of 45 days and display the output in a new column

Advertisements