In this article, we will learn how to use the ALTER TABLE statement in SQL.
To add or delete or modify columns in an existing table, the ALTER TABLE statement is used.
The ALTER TABLE statement also can be used to add or drop various constraints on an existing table.
ALTER TABLE – ADD: To add a column in a table.
Syntax
ALTER TABLE TABLE_NAME ADD column1 datatype, column2 datatype, .... columnN datatype;
Example
The subsequent statement would add CDate and EDate columns to the “Article” table:
ALTER TABLE Article ADD CDate date, EDate date;
ALTER TABLE – DROP COLUMN: To delete a column in a table.
Syntax
ALTER TABLE TABLE_NAME DROP COLUMN column1,column2 ...columnN;
Example
The subsequent statement would delete CDate and EDate columns from the “Article” table:
ALTER TABLE Article DROP COLUMN CDate, EDate;
ALTER TABLE – ALTER COLUMN: To change the data type of a column in a table.
Syntax
ALTER TABLE TABLE_NAME ALTER COLUMN column_name datatype;
Example
The subsequent statement would change the datatype of a column in a table:
ALTER TABLE Article ALTER COLUMN Views bigint;
Also, check How To Use TRUNCATE TABLE Statement In SQL