Alex Gordon
Alex Gordon

Reputation: 60691

sql server 2008 - adding a constraint

i am trying to add a constraint to a datatype of char(1)

i would like the user to only be able to enter Y or N or n or Y

i clicked on check constraint in the CHECK CONSTRAINT EXPRESSION window what am i supposed to enter?

Upvotes: 2

Views: 1581

Answers (3)

A-K
A-K

Reputation: 17080

It is always better to make such changes manually, not via GUI.

ALTER TABLE YourTable
ADD CONSTRAINT CHK_YourTable_YourColumn_YesOrNo CHECK(YourCOlumn IN ('Y', 'N'))

Edit: GUI can issue suboptimal DDL, and with little practice you can be just as efficient with manual scripts as with GUI, and you know exactly what is happening. Also you really want to store all your DDL in version control, including the script for changes.

Upvotes: 2

ulty4life
ulty4life

Reputation: 3012

alter table TableName
    add constraint CHK_TableName_ColumnName check (ColumnName in ('Y','N','y','n'))

Upvotes: 0

OMG Ponies
OMG Ponies

Reputation: 332521

Using an ALTER TABLE statement:

ALTER TABLE dbo.YOUR_TABLE 
  ADD CONSTRAINT bool_check CHECK (LOWER(your_column) IN ('n', 'y')) ;

Upvotes: 1

Related Questions