ALTER TABLE Statements


ALTER TABLE Statements


Use the ALTER TABLE statement to add, delete, or modify the columns of a table.

ALTER TABLE Syntax

To add, delete, or modify columns, use the ADD, ALTER, or DROP keywords of the ALTER TABLE statement, as shown in the example code below.

  • The ADD keyword accepts a column definition or a comma-separated list of column definitions.
  • The ALTER keyword accepts a column definition.
  • The DROP keyword accepts a column name.
ALTER TABLE <table_name>
  ADD [ COLUMN ] [ IF NOT EXISTS ]
    _ColumnDefinition_ | ( <column_definition> [ , ... ] )
    |   ALTER COLUMN <column_definition>
    |   DROP COLUMN [ IF EXISTS ] <column_name>
 
<column_definition> ::=
  <column_name>
  <data_type>
  [ NOT NULL ]
  [ DEFAULT <literal> ]
  [ PRIMARY KEY ]
  [ UNIQUE ]

The following query adds a new ExternalCustomerId column with data type int to the table:

ALTER TABLE MyCustomers ADD (ExternalCustomerId int)

The following query changes the data type of the ExternalCustomerId column to string:

ALTER TABLE MyCustomers ALTER COLUMN ExternalCustomerId string

The following query removes the ExternalCustomerId column:

ALTER TABLE MyCustomers DROP COLUMN ExternalCustomerId