How to Set a Check Constraint on a Column? - MS SQL Server

CREATE TABLE EmpHiringInfo
(
      employeeID int NOT NULL CONSTRAINT employeeid_fk REFERENCES Employee(employeeID) ON DELETE CASCADE,
      gender         char(1) CONSTRAINT gender_ck CHECK ('M', 'F'),
      CONSTRAINT empid_fk_pk PRIMARY KEY(employeeID)
);

When i run the code, i get this error that points to gender's check constraint:

An expression of non-boolean type specified in a context where a condition is expected, near ','.
LVL 1
F-J-KAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krunal_shahCommented:
try like this,
CREATE TABLE EmpHiringInfo
(
      employeeID int NOT NULL CONSTRAINT employeeid_fk REFERENCES Employee(employeeID) ON DELETE CASCADE,
      gender         char(1),
      CONSTRAINT empid_fk_pk PRIMARY KEY(employeeID)
);



ALTER TABLE EmpHiringInfo ADD CONSTRAINT gender_ck CHECK (gender == 'M' OR gender == 'F' )
0
Om PrakashCommented:
CREATE TABLE EmpHiringInfo
(
      employeeID int NOT NULL CONSTRAINT employeeid_fk REFERENCES Employee(employeeID) ON DELETE CASCADE,
      gender         char(1) CONSTRAINT gender_ck CHECK (gender in ('M', 'F')),
      CONSTRAINT empid_fk_pk PRIMARY KEY(employeeID)
)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
F-J-KAuthor Commented:
Ok, i run this:

ALTER TABLE EmpHiringInfo
(
      employeeID int NOT NULL CONSTRAINT employeeid_fk REFERENCES Employee(employeeID) ON DELETE CASCADE,
      gender         char(1) CONSTRAINT gender_ck CHECK (gender in ('M', 'F')),
      CONSTRAINT empid_fk_pk PRIMARY KEY(employeeID)
);

Now, i get another error:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.

I don't see where i went wrong
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

krunal_shahCommented:
have you tried this,
ALTER TABLE EmpHiringInfo ADD CONSTRAINT gender_ck CHECK (gender in ('M', 'F'))
0
F-J-KAuthor Commented:
@krunal_shah:

Error presists. For some reason, i had to drop the table and then create it again. The check constraint worked! Thanks
0
F-J-KAuthor Commented:
Solved! Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.