Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Saturday, November 3, 2007

SQL Server 2005 Check Constraint

I wanted to use something like the MySQL Enum constraint in SQL Server to limit the values allowed in a column...

I ended up using a separate table and a foreign key for this, but never the less, here's how to use the Check constraint, in my case, to limit RecordStatus to contain only 'Active' or 'Inactive':


CREATE TABLE NewsItems (
NewsItemID int IDENTITY NOT NULL PRIMARY KEY,
RecordStatus nvarchar(10) NOT NULL DEFAULT 'ACTIVE' CHECK (RecordStatus IN ('Active', 'Inactive'))
);

No comments:

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels