ALTER TABLE (SQL Server Compact)
Modifies a table definition by modifying, adding, or dropping columns and constraints.
ALTER TABLE table_name
ALTER COLUMN column_name
{
type_name[({precision[.scale]})][NULL|NOT NULL]
{DROP DEFAULT
| SET DEFAULT constant_expression
| IDENTITY [ ( seed , increment ) ]
}
| ADD
{ < column_definition > | < table_constraint > } [ ,...n ]
| DROP
{ [ CONSTRAINT ] constraint_name
| COLUMN column }
] }
< column_definition > ::=
{ column_name data_type }
[ [ DEFAULT constant_expression ]
| IDENTITY [ ( seed , increment ) ]
]
[ROWGUIDCOL]
[ < column_constraint > ] [ ...n ] ]
< column_constraint > ::=
[ NULL | NOT NULL ]
[ CONSTRAINT constraint_name ]
{
| { PRIMARY KEY | UNIQUE }
| REFERENCES ref_table [ (ref_column) ]
[ ON DELETE { CASCADE | NO ACTION | SET DEFAULT |SET NULL } ]
[ ON UPDATE { CASCADE | NO ACTION | SET DEFAULT |SET NULL } ]
}
< table_constraint > ::=
[ CONSTRAINT constraint_name ]
{ [ { PRIMARY KEY | UNIQUE }
{ ( column [ ,...n ] ) }
| FOREIGN KEY
( column [ ,...n ] )
REFERENCES ref_table [ (ref_column [ ,...n ] ) ]
[ ON DELETE { CASCADE | NO ACTION | SET DEFAULT |SET NULL } ]
[ ON UPDATE { CASCADE | NO ACTION | SET DEFAULT |SET NULL } ]
}
For ON DELETE or ON UPDATE, if the CASCADE option is specified, the row is updated in the referencing table if the corresponding referenced row is updated in the parent table. If NO ACTION is specified, SQL Server Compact returns an error, and the update action on the referenced row in the parent table is rolled back.
For example, you might have two tables, A and B, in a database. Table A has a referential relationship with table B: the A.ItemID foreign key references the B.ItemID primary key.
If an UPDATE statement is executed on a row in table B and an ON UPDATE CASCADE action is specified for A.ItemID, SQL Server Compact checks for one or more dependent rows in table A. If any exist, the dependent rows in table A are updated, as is the row referenced in table B.
Alternatively, if NO ACTION is specified, SQL Server Compact returns an error and rolls back the update action on the referenced row in table B when there is at least one row in table A that references it.
A. Changing the seed and increment values on the identity column
The following example changes the seed and increment values on the identity column.
CREATE TABLE MyCustomers (CustID INTEGER IDENTITY (100,1) PRIMARY KEY, CompanyName NvarChar (50));
INSERT INTO MyCustomers (CompanyName) VALUES ('A. Datum Corporation');
ALTER TABLE MyCustomers ALTER COLUMN CustId IDENTITY (200, 2);
B. Adding a default value to a column
The following example modifies the CompanyName column so that it has a default value.
ALTER TABLE MyCustomers ALTER COLUMN CompanyName SET DEFAULT 'A. Datum Corporation'
C. Dropping a default value from the column
The following example modifies the CompanyName column so that it does not have a default value.
ALTER TABLE MyCustomers ALTER COLUMN CompanyName DROP DEFAULT

Note