
- MySQL - Home
- MySQL - Introduction
- MySQL - Features
- MySQL - Versions
- MySQL - Variables
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Node.js Syntax
- MySQL - Java Syntax
- MySQL - Python Syntax
- MySQL - Connection
- MySQL - Workbench
- MySQL Databases
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Show Database
- MySQL - Copy Database
- MySQL - Database Export
- MySQL - Database Import
- MySQL - Database Info
- MySQL Users
- MySQL - Create Users
- MySQL - Drop Users
- MySQL - Show Users
- MySQL - Change Password
- MySQL - Grant Privileges
- MySQL - Show Privileges
- MySQL - Revoke Privileges
- MySQL - Lock User Account
- MySQL - Unlock User Account
- MySQL Tables
- MySQL - Create Tables
- MySQL - Show Tables
- MySQL - Alter Tables
- MySQL - Rename Tables
- MySQL - Clone Tables
- MySQL - Truncate Tables
- MySQL - Temporary Tables
- MySQL - Repair Tables
- MySQL - Describe Tables
- MySQL - Add/Delete Columns
- MySQL - Show Columns
- MySQL - Rename Columns
- MySQL - Table Locking
- MySQL - Drop Tables
- MySQL - Derived Tables
- MySQL Queries
- MySQL - Queries
- MySQL - Constraints
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Replace Query
- MySQL - Insert Ignore
- MySQL - Insert on Duplicate Key Update
- MySQL - Insert Into Select
- MySQL Indexes
- MySQL - Indexes
- MySQL - Create Index
- MySQL - Drop Index
- MySQL - Show Indexes
- MySQL - Unique Index
- MySQL - Clustered Index
- MySQL - Non-Clustered Index
- MySQL Operators and Clauses
- MySQL - Where Clause
- MySQL - Limit Clause
- MySQL - Distinct Clause
- MySQL - Order By Clause
- MySQL - Group By Clause
- MySQL - Having Clause
- MySQL - AND Operator
- MySQL - OR Operator
- MySQL - Like Operator
- MySQL - IN Operator
- MySQL - ANY Operator
- MySQL - EXISTS Operator
- MySQL - NOT Operator
- MySQL - NOT EQUAL Operator
- MySQL - IS NULL Operator
- MySQL - IS NOT NULL Operator
- MySQL - Between Operator
- MySQL - UNION Operator
- MySQL - UNION vs UNION ALL
- MySQL - MINUS Operator
- MySQL - INTERSECT Operator
- MySQL - INTERVAL Operator
- MySQL Joins
- MySQL - Using Joins
- MySQL - Inner Join
- MySQL - Left Join
- MySQL - Right Join
- MySQL - Cross Join
- MySQL - Full Join
- MySQL - Self Join
- MySQL - Delete Join
- MySQL - Update Join
- MySQL - Union vs Join
- MySQL Keys
- MySQL - Unique Key
- MySQL - Primary Key
- MySQL - Foreign Key
- MySQL - Composite Key
- MySQL - Alternate Key
- MySQL Triggers
- MySQL - Triggers
- MySQL - Create Trigger
- MySQL - Show Trigger
- MySQL - Drop Trigger
- MySQL - Before Insert Trigger
- MySQL - After Insert Trigger
- MySQL - Before Update Trigger
- MySQL - After Update Trigger
- MySQL - Before Delete Trigger
- MySQL - After Delete Trigger
- MySQL Data Types
- MySQL - Data Types
- MySQL - VARCHAR
- MySQL - BOOLEAN
- MySQL - ENUM
- MySQL - DECIMAL
- MySQL - INT
- MySQL - FLOAT
- MySQL - BIT
- MySQL - TINYINT
- MySQL - BLOB
- MySQL - SET
- MySQL Regular Expressions
- MySQL - Regular Expressions
- MySQL - RLIKE Operator
- MySQL - NOT LIKE Operator
- MySQL - NOT REGEXP Operator
- MySQL - regexp_instr() Function
- MySQL - regexp_like() Function
- MySQL - regexp_replace() Function
- MySQL - regexp_substr() Function
- MySQL Fulltext Search
- MySQL - Fulltext Search
- MySQL - Natural Language Fulltext Search
- MySQL - Boolean Fulltext Search
- MySQL - Query Expansion Fulltext Search
- MySQL - ngram Fulltext Parser
- MySQL Functions & Operators
- MySQL - Date and Time Functions
- MySQL - Arithmetic Operators
- MySQL - Numeric Functions
- MySQL - String Functions
- MySQL - Aggregate Functions
- MySQL Misc Concepts
- MySQL - NULL Values
- MySQL - Transactions
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - SubQuery
- MySQL - Comments
- MySQL - Check Constraints
- MySQL - Storage Engines
- MySQL - Export Table into CSV File
- MySQL - Import CSV File into Database
- MySQL - UUID
- MySQL - Common Table Expressions
- MySQL - On Delete Cascade
- MySQL - Upsert
- MySQL - Horizontal Partitioning
- MySQL - Vertical Partitioning
- MySQL - Cursor
- MySQL - Stored Functions
- MySQL - Signal
- MySQL - Resignal
- MySQL - Character Set
- MySQL - Collation
- MySQL - Wildcards
- MySQL - Alias
- MySQL - ROLLUP
- MySQL - Today Date
- MySQL - Literals
- MySQL - Stored Procedure
- MySQL - Explain
- MySQL - JSON
- MySQL - Standard Deviation
- MySQL - Find Duplicate Records
- MySQL - Delete Duplicate Records
- MySQL - Select Random Records
- MySQL - Show Processlist
- MySQL - Change Column Type
- MySQL - Reset Auto-Increment
- MySQL - Coalesce() Function
MySQL - ALTER TABLE Statement
MySQL ALTER TABLE Statement
The Alter TABLE statement of MySQL helps to perform changes on a table. Using this statement, we can add, delete, or modify columns in an existing table and we can also rename it.
Syntax
Following is the syntax of the ALTER Table statement −
ALTER TABLE table_name [alter_option ...];
Where, table_name is the name of the existing table you need to change and the alter_option represents the change you need to perform on the table such as ADD COLUMNS, DROP COLUMN etc.
Adding a new column
For instance, if you need to add a new column to a table you should use the ADD COLUMN option as shown below −
ALTER TABLE table_name ADD COLUMN (column_name column_definition...)
Example
Suppose we have created a table as shown below −
CREATE TABLE Employee( Name VARCHAR(255), Salary INT NOT NULL, Location VARCHAR(255) );
Following query adds a new column named phone to the Employee table −
ALTER TABLE Employee ADD COLUMN (Phone int (10));
To verify the above query if you describe the table using the DESC command you can observe the created column in the field list as shown below −
DESC employee;
Following is the output of the above mysql query −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
Name | varchar(255) | YES | NULL | ||
Salary | int | No | NULL | ||
Location | varchar(255) | YES | NULL | ||
Phone | int | YES | NULL |
Adding a column after an existing column
If you want to add a column before or after an existing column you need to use AFTER followed by the name of the existing column (after the column definition).
ALTER TABLE table_name ADD COLUMN (column_name column_definition...)
Example
Following query adds a column named address after the Location.
ALTER TABLE Employee ADD COLUMN Address VARCHAR(50) AFTER Location;
To verify the above query if you describe the table using the DESC command you can observe the created column in the field list as shown below −
DESC employee;
Output
The above query produces the following output &mius;
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
Name | varchar(255) | YES | NULL | ||
Salary | int | NO | NULL | ||
Location | varchar(255) | YES | NULL | ||
Address | varchar(50) | YES | NULL | ||
Phone | int | YES | NULL |
Adding a column at start
You can add the new column at the start using the FIRST statement along with alter, following is the syntax to do so −
ALTER TABLE table_name ADD COLUMN (column_name column_definition...)
Example
Following query adds a column with name ID at start −
ALTER TABLE Employee ADD COLUMN ID INT FIRST;
To verify the above query if you describe the table using the DESC command you can observe the created column in the field list as shown below −
desc employee;
Output
Following is the output of the above query −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | YES | NULL | ||
Name | varchar(255) | YES | NULL | ||
Salary | int | NO | NULL | ||
Location | varchar(255) | YES | NULL | ||
Address | int | YES | NULL | ||
Phone | int | YES | NULL |
Dropping a column
You can drop an existing column by using the DROP statement along with ALTER following is the syntax to do so −
ALTER TABLE table_name DROP COLUMN column_name;
Example
First of all, let us verify the definition of the table using DESC statement −
desc employee;
Output
The above query produces the following output −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | YES | NULL | ||
Name | varchar(255) | YES | NULL | ||
Salary | int | NO | NULL | ||
Location | varchar(255) | YES | NULL | ||
Address | varchar(50) | YES | NULL | ||
Phone | int | YES | NULL |
Following query drops the column named ID −
ALTER TABLE Employee DROP COLUMN ID;
To verify the above query if you describe the table using the DESC command you can observe the name of the deleted column is missing from the field list −
desc employee;
Following is the output of the above query −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
Name | varchar(255) | YES | NULL | ||
Salary | int | NO | NULL | ||
Location | varchar(255) | YES | NULL | ||
Address | varchar(50) | YES | NULL | ||
Phone | int | YES | NULL |
Adding an index
You can add index to an existing column of a table using the ADD INDEX statement along with the ALTER statement −
ALTER TABLE table_name ADD INDEX index_name [index_type] (key_part,...)
Example
Following query adds an index on the columns Name and Salary −
ALTER TABLE Employee ADD INDEX sample_index (Name, Salary);
You can verify the indices of the table using the SHOW INDEX statement as shown below −
SHOW INDEX FROM Employee;
Output
The above query will produce the following output −
*************** 1. row *************** Table: employee Non_unique: 1 Key_name: sample_index Seq_in_index: 1 Column_name: Name Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: Visible: YES Expression: NULL *************** 2. row *************** Table: employee Non_unique: 1 Key_name: sample_index Seq_in_index: 2 Column_name: Salary Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: Visible: YES Expression: NULL
Adding a primary key
Following is the syntax of creating a primary key −
ALTER TABLE table_name ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...);
Example
Let us create a column named ID in the table Employee −
ALTER TABLE Employee ADD COLUMN ID INT FIRST;
Following query creates a primary key on the column ID −
ALTER TABLE Employee ADD CONSTRAINT MyPrimaryKey PRIMARY KEY(ID);
To verify the above query if you describe the table using the DESC command you can observe PRI against the ID field under the column Key −
desc employee;
Output
Following is the output of the above program −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | NO | PRI | NULL | |
Name | varchar(255) | YES | MUL | NULL | |
Salary | int | NO | NULL | ||
Location | varchar(255) | YES | NULL | ||
Phone | int | YES | NULL |
Adding a foreign key
Following is the syntax of creating a foreign key −
ALTER TABLE table_name ADD CONSTRAINT key FORIEGN KEY (column_name);
Example
Assume we have created another table named test as shown below −
CREATE table Test (ID int PRIMARY KEY);
Let us create a column named ID in the table Employee −
ALTER TABLE Employee ADD COLUMN ID INT FIRST;
Following query creates a primary key on the column ID −
ALTER TABLE Employee ADD CONSTRAINT fk FOREIGN KEY(ID) REFERENCES test(ID);
Adding a constraint
Following is the syntax to add constraint to a column −
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(column1, column2...);
Example
Following query adds UNIQUE constraint to the table Employee −
ALTER TABLE Employee ADD CONSTRAINT con UNIQUE(Phone);
Dropping an index
Following is the syntax to drop an index on a table −
ALTER TABLE table_name DROP INDEX index_name
Example
Following query drops the index created above on the table Employee −
ALTER TABLE Employee DROP INDEX sample_index;
Drop a primary key
Following is the syntax to drop a primary key −
ALTER TABLE table_name DROP PRIMARY key_name;
Example
Suppose we have created a table and added a PRIMARY KEY as shown below −
create table sample (ID INT); alter table sample add CONSTRAINT PRIMARY KEY (ID);
To verify the above query if you describe the table using the DESC command you can observe PRI against the ID field under the column Key −
desc sample;
Output
Following is the output of the above query −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | NO | PRI | NULL |
Following query drops the above created Primary key −
alter table sample drop PRIMARY KEY;
If you describe the sample table using the desc statement you can observe that the primary constraint is removed −
desc sample;
Output
The above query produces the following output −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | NO | NULL |
Dropping a foreign key
Following is the syntax to drop a foreign key −
ALTER TABLE table_name DROP PRIMARY key_name;
Example
Following query drops the foreign key fk of the table employee −
ALTER TABLE Employee DROP FOREIGN KEY fk;