Solved

Limiting the Possible Values in a Text Column

Posted on 2004-08-30
6
280 Views
Last Modified: 2011-10-03
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.
0
Comment
Question by:LucasWhite84
  • 3
  • 2
6 Comments
 

Author Comment

by:LucasWhite84
ID: 11932639
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.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11932727
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')
0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 400 total points
ID: 11932746
Or you can do it using Enterprise Manager - right click on table -> design table
in the table designer, right-click and select Check Constraints
there you acn enter the constraint name e.g. CK_mytable, and the text e.g. [text_field] = 'red' or [text_field] = 'green' or [text_field] = 'blue'
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

Author Comment

by:LucasWhite84
ID: 11932878
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.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 100 total points
ID: 11932983
>>Constraints of type CHECK cannot be created on columns of type TEXT.<<
If all you need to save is red green or blue.  Than you should not be using TEXT data type in the first place.  Use varchar(5) instead.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11933007
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'
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question