SQL CREATE / ALTER / DROP COLUMN

SQL CREATE / ALTER / DROP COLUMN

This tutorial will explain how to Alter Table in SQL and how to use them with the examples.

The ALTER TABLE statement is used to add, delete, or modify columns and constraints in an existing table.

ALTER TABLE - ADD Column
Adds a column into a table

The following example add Column1 with integer type into Table11 table
ALTER TABLE Table11
ADD Column1 int

ALTER TABLE - DROP Column
Removes an existing column from a table

Lets create a sample table first
CREATE TABLE Table12 (
Column1 VARCHAR(50) NOT NULL,
Column2 VARCHAR(50) NOT NULL
);

The following example will remove Column2 column from Table12
ALTER TABLE Table12
DROP COLUMN Column2

ALTER TABLE - ALTER Column
Modifies an existing column from a table

Lets create a sample table first
CREATE TABLE Table13 (
Column1 VARCHAR(50) NOT NULL,
Column2 VARCHAR(50) NOT NULL
);

The following example will modify Column2 column data type to integer . This statement would fail if there is already not integer data in the Column2
ALTER TABLE Table13
ALTER COLUMN Column2 INT NOT NULL