Add Site or Add Page to Favorites
>

 CHAR vs. VARCHAR 

Preview DRM-free music
Play clips of the most popular DRM-Free music in MP3 format
Legal Music .com

Get information on your favorite TV Shows at TV Series .com

Clarify the meaning of the acronyms on your web site.

Acronym Dictionary

Acronym Finder

Social Networking Web Sites

Register your own domain name at A to Z Domains.

or buy domains at Domain Names For Sale .net


Printer-friendly PDF* format:

Database Design

This Section

Automatic padding of character columns

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.

Why not just make everything VARCHAR?

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.

Fixed vs. variable length program variables

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.

When to use CHAR vs. VARCHAR

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

You are currently viewing this page in HTML 4* format (* see Clicklets for more infomation). This document is also available in XHTML 1 Style Sheet*XHTML 1* XML*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.


Copyright © 2006 How To Guides .com. Alteration of content, including addition of any function such as hypertext links or pop-up advertising, or interference with the hypertext links or other functions of this site is expressly prohibited.

DISCLAIMER: All information, links, forms, applications and other items on this site or obtained from it are provided AS IS, WITHOUT WARRANTY OF ANY KIND EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.