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.
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')
alter table mytable add constraint [CK_mytable] CHECK ([text_field] = 'red' or [text_field] = 'green' or [text_field] = 'blue')
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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'
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'
ASKER