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
ALTER TABLE dbo.Presidents |
ADD CONSTRAINT President_unique UNIQUE (President) |
Add constraint to test value
ALTER TABLE dbo.Presidents |
ADD
CONSTRAINT
President_unique
CHECK
(YearsInOffice >= 0
AND
YearsInOffice < 13)
No comments:
Post a Comment