Add Column Constraint
-
Posted on August 2, 2009 by Derek Dieter
-
1
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
5 comments
Claudio
03 May 2018 at 9:20 am
I blog often and I seriously appreciate your information. The article has really peaked my interest.
I am going to bookmark your website and keep checking for new details about once a week.
I subscribed to your Feed too.43546