Shantanu Gupta
Shantanu Gupta

Reputation: 21188

How can I use check constraint in sql server 2005

i want to check for a particular set of values.
eg

Upvotes: 1

Views: 1099

Answers (2)

David Hall
David Hall

Reputation: 33143

There is quite a wealth of information in the SQL Server documentation on this, but the two statements to create the check constraints you ask for are:

ALTER TABLE tablename ADD CONSTRAINT constraintName CHECK (colname between 1 and 5);

ALTER TABLE tablename ADD CONSTRAINT constraintName CHECK (colname in (1,2,4));

The condition of a check constraint can include:

  1. A list of constant expressions introduced with in

  2. A range of constant expressions introduced with between

  3. A set of conditions introduced with like, which may contain wildcard characters

This allows you to have conditions like:

(colname >= 1 AND colname <= 5)

Upvotes: 4

chris
chris

Reputation: 37440

ALTER TABLE tablename ADD CONSTRAINT constraintName CHECK (colname in (1,2,4));

Upvotes: 1

Related Questions