20 Aug 2011

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

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