We have discussed how to create a table in the database, but there are cases when we would like to modify the table that was created. In order to modify a table SQL provides ALTER as the keyword. We will try to understand the usage of the ALTER keyword for altering a table in detail.
Table of Contents
SQL ALTER Table
There are cases when we designed and created a table based on the existing need of the application. Consider in future the requirement is changed and we need to make modifications in the existing table. In such cases, we would be using the ALTER keyword. Let us now consider the following scenarios for further discussion.
- Adding a new column to the table
- Dropping a column from the table
- Changing data type of a column
- Add a constraint to a table
- Remove constraint from the table.
Note: All the scenarios and queries mentioned below are for MySQL database.
We will consider the following Customer table for further discussion.
CopyCREATE TABLE `test`.`customer` ( `CustomrId` INT NOT NULL, `CustomerName` VARCHAR(45) NULL, `ProductId` VARCHAR(45) NOT NULL, `State` VARCHAR(45) NULL, PRIMARY KEY (`CustomrId`, `ProductId`), UNIQUE INDEX `CustomrId_UNIQUE` (`CustomrId` ASC) VISIBLE);
1. Adding a New Column to the table
CopyALTER TABLE table_name ADD column_name datatype;
In the syntax above, the ALTER TABLE is the keyword that tells the database that a modification is needed in the table and the ADD keyword tells that a column addition needs to be done.
Adding a customer_age column of int datatype in the Customer table.
CopyALTER TABLE test.customer ADD customer_age int;
2. Dropping a Column from Table
CopyALTER TABLE table_name DROP COLUMN column_name;
The DROP COLUMN keyword tell that a column needs to be deleted.
Removing customer_age column of int datatype in the Customer table.
CopyALTER TABLE test.customer DROP COLUMN customer_age;
3. Changing Data Type of a Column
CopyALTER TABLE table_name MODIFY COLUMN column_name datatype;
MODIFY COLUMN keyword tells the database that a column needs to be modified.
Changing the data type of productId from Varchar to Int.
CopyALTER TABLE test.customer MODIFY COLUMN ProductId int;
4. Add a Constraint to a Table
CopyALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...columnN);
ADD CONSTRAINT keyword tells the database that a constraint needs to be added in the table. The UNIQUE keyword tells that the column after the UNIQUE keyword will have unique values in it.
Adding MyUniqueConstraint to the Customer table.
CopyALTER TABLE test.customer ADD CONSTRAINT MyUniqueConstraint UNIQUE(CustomerName);
5. Remove Constraint from the Table
CopyALTER TABLE table_name DROP CONSTRAINT MyUniqueConstraint;
In the syntax above, the DROP CONSTRAINT keyword tells the database that a constraint needs to be removed from the table.
Syntax for MySQL:-
CopyALTER TABLE table_name DROP INDEX MyUniqueConstraint;
Removing MyUniqueConstraint from Customer table.
If you are using MySQL following Query will help.
CopyALTER TABLE test.customer DROP INDEX MyUniqueConstraint;