Re: CHAR or VARCHAR

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: "Martin A(dot) Marques" <martin(at)math(dot)unl(dot)edu(dot)ar>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: CHAR or VARCHAR
Date: 2001-03-22 00:27:48
Message-ID: 3AB94704.115DD386@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Martin,

> two questions.
> When should I use one, and when the other?

IMHO, CHAR serves three purposes:

1. It is a good data type for fixed-length abbreviations that will never
be more or less than the number of characters intended. US state codes,
for example, which are always exactly two characters (Unless you count
Puerto Rico and the Virgin Islands!)

2. SOmetimes you have a field which is *almost* always the same exact
length, and needs to be used extensively in JOIN operations. In that
case, the 2-byte savings of the CHAR data-type over VARCHAR may make a
difference.

3. Converting several VARCHAR columns to CHAR and then concatinating
them (||) will give you a quick-and-easy fixed-width multi-column format
for list boxes and the like.

Otherwise, always use VARCHAR. You won't need to worry about trailing
spaces or fields being equal to a bunch of blanks (' ').

> Which is the limit on CHAR(n) and VARCHAR(n)?

Limit? There's a limit?

Ask Stephan. I'm pretty sure it varies by distribution, but is
somewhere in the thousands.

-Josh Berkus

--
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Peter J. Schoenster 2001-03-22 01:10:31 Re: CHAR or VARCHAR
Previous Message datactrl 2001-03-22 00:00:48 Re: Re: psql win32 version