aelsheikh
aelsheikh

Reputation: 2328

SQL SVR 08 R2 Limit a field to specific values

For example, say I have field 'Gender' and I want to allow only the values Male, Female or Unisex. Is this possible in the SQL server management studio?

Update: So far the solution is pointing towards the 'Check constraints' function which can be found by right-clicking on the desired column.

Still, I'm getting syntax errors. Any thoughts on how I should type up the constraint?

Answer: Turns out I just needed to type it likes this

Gender = 'Male' OR Gender = 'Female' OR Gender = 'Unisex'

Upvotes: 3

Views: 1276

Answers (2)

Keethanjan
Keethanjan

Reputation: 363

You could do the following: - Create another table and create a one to many relationship from the primary table to the Gender. - Create an constraint like the following one:

CREATE TABLE Person
(
   Id int NOT NULL,
   Gender varchar(200)
   CONSTRAINT chk_Gender CHECK (Gender IN ('Male', 'Unisex', 'Female'))
)

Upvotes: 2

John Woo
John Woo

Reputation: 263803

Unlike MySQL, MSSQL doesn't have ENUM data type. The only want you can do this is to trap the value in the application level. Or by using CHECK constraint

ColumnName varchar(10) 
CHECK(ColumnName IN ('Male', 'Female', 'Unisex')) 
DEFAULT 'Unisex'

Upvotes: 4

Related Questions