GibboK
GibboK

Reputation: 73908

How to add a column constraint in MS SQL

I have a table in MS Sql 2008 r2, I would like add a constrain to a column with I can accept only 3 value like

myColum      char(2)    constrain values (A1, B2, C3)

Upvotes: 0

Views: 2407

Answers (5)

Ghislain
Ghislain

Reputation: 1

CREATE TABLE
(
 myColumn char(2) NOT NULL check(myColumn= 'A1' or myColumn= 'B2'  myColumn= 'C3') 
) 

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176896

Make use of SQL CHECK Constraint supported by sql server .

CREATE TABLE test
(
    myColum       char(2) NOT NULL
    CONSTRAINT chk_Person CHECK (myColum in ('A1', 'B2', 'C3')) 
)

Upvotes: 2

KM.
KM.

Reputation: 103579

try:

ALTER TABLE YourTable WITH CHECK ADD  CONSTRAINT ck_y CHECK (myColum in ('A1','B2','C3'))
GO

Upvotes: 0

user330315
user330315

Reputation:

Something like this:

myColum  char(2)  check (myColum in ('A1', 'B2', 'C3'))

SQLFiddle: http://sqlfiddle.com/#!3/8e304

Upvotes: 3

Joe G Joseph
Joe G Joseph

Reputation: 24046

ALTER TABLE <table>
ADD CHECK col in ('A1', 'B2', 'C3')

Upvotes: 1

Related Questions