When considering whether to use CHAR or VARCHAR, the decision depends more on programming considerations than on the database. The difference is that comparisons of two fixed character fields will be automatically padded to equal lengths with spaces; comparisons with VARCHAR will not.
In order to save time on the database design, you might be tempted to avoid using CHAR columns and simply use VARCHAR for all character data in the database. As a result, every time one of these columns is compared to a fixed character column or host variable, the programmers will have to use an RPAD or RTRIM function, since programming languages won't do automatic padding on VARCHAR columns. Because the database cannot simply search the values in the index when a function is applied to a key column queries may unexpectedly take much longer to complete. So the time that was saved in the design phase will probably be spent many times over trying to determine why certain queries are running so poorly.
One reason that the VARCHAR data type is often used is that some programmers by habit use variable
length character variables, which will also defeat the programming language's
automatic padding for fixed-length comparisons.
For example, in Java, programmers might simply use String,
which is variable length, for all character variables and avoid
char[], which is the appropriate type for storing
data being transferred to or from a CHAR column in a database.
In database design, we're taught how to determine whether to make a column CHAR or VARCHAR.
You ask yourself, "is the length of the value significant?"
For example, if the column has up to 6 characters representing a gender,
is a 6-character "MALE " different from a 4-character "MALE"?
In this case the answer is obviously "no" and the column should therefore be
CHAR so that regardless of whether you compare it to a 4- or 6-character constant
or 6-character host variable, the comparison is automatically padded and
works properly regardless of whether the column contains "MALE " or
"FEMALE" and whether the constant or host variable contains
"MALE", "MALE " or "FEMALE".
If either side of the comparison is a variable length character type, then you will
need to RPAD and/or RTRIM one or both sides of the comparison to make it work properly.
Last updated Tuesday November 14, 2006
Printer-friendly PDF* format:
You are currently viewing this page in XHTML 1 Style Sheet* format (* see Clicklets for more infomation). This document is also available in XHTML 1*XML*HTML 4*HTML 5 Style Sheet*HTML 5 XML*HTML 5 non-XML* XHTML 2* XHTML Mobile* WML Mobile* and printer-friendly PDF* formats. This is accomplished with Single Source Publishing, a content management system that uses templates in XSLT style sheets provided by XML Styles .com to transform the source content for various content delivery channels. There is also RDF* metadata that describes the content of this document.