Add Constraint
-
Posted on July 25, 2009 by Derek Dieter
-
0
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:
- Primary Key Constraints – Enforces unique values for specified column, can be referenced.
- Foreign Key Constraints – Enforces a reference to a primary key
- Unique Constraints – Ensures unique values within a column
- 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]
Post a comment
- Comments (RSS)
- Trackback
- Permalink