Adding NOT NULL columns  Dropping columns

Chapter 8: Creating Databases and Tables

Adding constraints

To add a constraint to an existing column, use:

alter table table_name 
     add constraint constraint_name {constraint_clause}

Where:

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. Dropping columns

View this book as PDF