Add Constraint

To add a constraint to an existing table use the alter table statement with the add constraint command. There are four different types of constraints:

  1. Primary Key Constraints – Enforces unique values for specified column, can be referenced.
  2. Foreign Key Constraints – Enforces a reference to a primary key
  3. Unique Constraints – Ensures unique values within a column
  4. Check Constraints – Limits values acceptable for a column based on an evaluation

Add unique constraint

[cc lang=”sql”]
ALTER TABLE dbo.Presidents
ADD CONSTRAINT President_unique UNIQUE (President)
[/cc]

Add constraint to test value

[cc lang=”sql”]
ALTER TABLE dbo.Presidents
ADD CONSTRAINT President_unique CHECK (YearsInOffice >= 0 AND YearsInOffice < 13) [/cc]

3 comments
Jai Pee 13 Mar 2014 at 7:29 am

Thanks your article helps me a lot…

Thanks once again.

iouoiuiou 06 Mar 2013 at 11:48 am

its very useful

Anonymous 13 Mar 2012 at 11:40 am

Show example how to modify the invoice table by making one of the columns not null

Featured Articles

 Site Author

  • Thanks for visiting!
css.php