Link to home
Start Free TrialLog in
Avatar of LucasWhite84
LucasWhite84

asked on

Limiting the Possible Values in a Text Column

I have a column in a 2000 SQL Server Database that is of the text data type.  I would like to restrict the possible values to either 'blue', 'red', or 'green'.  Any help is appreciated, and I will reward points quickly.
Avatar of LucasWhite84
LucasWhite84

ASKER

In other words, I want the database to have a constraint where it won't allow any more values other than 'blue', 'red', and 'green' to be placed in this column.
You can use hte following script - modifying fro your table / column names of course :

alter table mytable add constraint [CK_mytable]  CHECK ([text_field] = 'red' or [text_field] = 'green' or [text_field] = 'blue')
ASKER CERTIFIED SOLUTION
Avatar of BillAn1
BillAn1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried to run the script and it said...

Server: Msg 1760, Level 16, State 1, Line 1
Constraints of type CHECK cannot be created on columns of type TEXT.

Then, I tried to type in [Color] = 'red' or [Color] = 'green' or [Color] = 'blue' in the Enterprise Manager constraint entry and it said that there was an error with the constraint.
SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, as the error says, Constraints of type CHECK cannot be created on columns of type TEXT.
Sorry, I didn't notice that you had text data types.
TEXT data types are really only for large text strings (normally > 8000 characters) they have a lot of limitations and are really desinged only for extra long strings.
what you want is data type char or varchar. You should change your column type to something like varchar(5) if the only valid values are 'red'. 'blue' or 'green'