To add a constraint to an existing column, use:
alter table table_name add constraint constraint_name {constraint_clause}
Where:
table_name is the name of the table to which you are adding a constraint.
constraint_name is the constraint you are adding.
constraint_clause is the rule the constrain enforces.
For example, to add a constraint to the titles table that does not allow an advance in excess of 10,000:
alter table titles add constraint advance_chk check (advance < 10000)
If a user attempts to insert a value greater than 10,000 into the titles table, Adaptive Server produces an error message similar to this:
Msg 548, Level 16, State 1: Line 1:Check constraint violation occurred, dbname = ‘pubs2’,table name= ‘titles’, constraint name = ‘advance_chk’. Command has been aborted.
Adding a constraint does not affect the existing data. Also, if you add a new column with a default value and specify a constraint on that column, the default value is not validated against the constraint.
For information about dropping a constraint, see “Dropping constraints”.
Copyright © 2005. Sybase Inc. All rights reserved. |