471,122 Members | 1,128 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

How To Aplly Check Constraint

Hi to everyone,

I m new to MS SQL , i am CREATEING a Table. i want one field of table should hold only two values like (Yes,No). how is possible with aplying CHECK constraint.
plz help me to sove this problem.

Waiting for early response.
Jan 16 '07 #1
1 3803
168 Expert 100+
If your field can contain only 'YES' or 'NO', you should consider using bit data type instead.
Here is a sample of check constraint implementation:
Expand|Select|Wrap|Line Numbers
  1. create table #table (
  2.     id int identity(1,1), 
  3.     datavalue varchar(5), 
  4.     check (datavalue in ('yes', 'no'))
  5. )
  7. insert #table (datavalue) values ('yes')    -- will pass check constraint
  8. insert #table (datavalue) values ('yes1')    -- will not pass check constraint
  9. insert #table (datavalue) values ('no')        -- will pass check constraint
  10. insert #table (datavalue) values ('yes11')    -- will not pass check constraint
  11. insert #table (datavalue) values ('no11')    -- will not pass check constraint
  12. select * from #table
  14. drop table #table
Jan 16 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Edmund Dengler | last post: by
1 post views Thread by huyuhui | last post: by
3 posts views Thread by Helen Wheels | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.