The ALTER TABLE statement can be used to modify an existing table by adding, altering, or dropping columns and indexes.
ALTER TABLE Sales
ADD UnitPrice MONEY;
The command(s) completed successfully.
This example adds a single field to the 'Sales' table by specifying the new field name and data type.
ALTER TABLE Sales
ADD UnitPrice MONEY
DEFAULT 3.00 NOT NULL;
(3 row(s) affected)
This example adds the reserved words NOT NULL to require valid data to be added to that field. However, you must also specify a default value for any data already inserted in the table.
ALTER TABLE Sales
DROP COLUMN UnitPrice;
The command(s) completed successfully.
This example demonstrates how to use DROP COLUMN to delete a single field.
ALTER TABLE Sales
ADD Item VARCHAR (30)
CONSTRAINT UniqueConstraint UNIQUE;
The command(s) completed successfully.
This example defines an index for a new field by using the CONSTRAINT clause. Indexes cannot be created for the following data types: text, ntext, or image data type.
Note: The TEXT and INT data types cannot have a field size property set.
ALTER TABLE Names
ADD CONSTRAINT UniqueValues
UNIQUE (FirstName, LastName);
The command(s) completed successfully.
This shows how ADD CONSTRAINT can be used to add a multi-field index.
ALTER TABLE Names
DROP CONSTRAINT UniqueValues;
The command(s) completed successfully.
DROP CONSTRAINT can also be used to remove a multi-field index.