Re: Newbee question "Types"

From: Joel Burton <joel(at)joelburton(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, pgsql-general(at)postgresql(dot)org, ken(at)perfect-image(dot)com, Joe Tomcat <tomcat(at)mobile(dot)mp>
Subject: Re: Newbee question "Types"
Date: 2002-12-06 03:01:11
Message-ID: 20021206030111.GA8388@temp.joelburton.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice

On Thu, Dec 05, 2002 at 06:07:44PM -0800, Josh Berkus wrote:
> Bruce gives the example of State Codes, which is a classic example, and
> something I did myself in the past. However, I generally found myself
> forced to expand the state code field; the abbreviation for some US
> Territories is 4 characters, and some countries use 3 for provinces. At
> that point, I very much needed to use VARCHAR, since I don't want 'CA__' as
> my state.
>
> CHAR can be a reminder to you, the developer, in reading the schema, that you
> are expecting a string of a precise length. But I do not see it as an
> effective or useful constraint on input in real applications. A constraint
> statement, like the above, is far more effective.

With some ODBC apps, CHAR can behave in less-than-expected ways.

CREATE TABLE c (c CHAR(5));
INSERT INTO c VALUES ('a');

will put a 'a____' into the field. PG will let you find this with

SELECT * FROM c WHERE c='a';

because it handles the padding v. non-padding fine.

However, an ODBC app like Access won't find it w/a normal,
Access-mediated query. You have to specify WHERE c='a____' to find it.
*Unless* you're writing a pass-through query (which Access doesn't
touch, but hands directly to PostgreSQL). So you can get different
behavior w/o expecting it.

If your app might be used in different environments, I'd think before
using CHAR, even in places that might seem 'safe' or 'obvious'.

- J.
--

Joel BURTON | joel(at)joelburton(dot)com | joelburton.com | aim: wjoelburton
Independent Knowledge Management Consultant

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Robert Treat 2002-12-06 04:13:39 Re: [GENERAL] PostgreSQL Global Development Group
Previous Message Josh Berkus 2002-12-06 02:07:44 Re: Newbee question "Types"

Browse pgsql-novice by date

  From Date Subject
Next Message Kelly McDonald 2002-12-06 13:18:49 Re: Test to see if currval will fail?
Previous Message Josh Berkus 2002-12-06 02:07:44 Re: Newbee question "Types"