Limit SQL Server column to a list of possible values

Dean picture Dean · Aug 16, 2012 · Viewed 48.9k times · Source

How do I put a constraint on a column so that it can only contain the following values? What do you call this type of constraint?

Allowed values: "yes", "no" or "maybe"
Column Data Type: nvarchar(5)
DBMS: SQL Server 2008

Answer

Joe G Joseph picture Joe G Joseph · Aug 16, 2012

you can use a CHECK constraint

ALTER TABLE <table>
ADD CONSTRAINT chk_val CHECK (col in ('yes','no','maybe'))

MSDN link