Re: Newbie question

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Vinay <vinay(at)mdp(dot)net>, pgsql-admin(at)postgresql(dot)org
Subject: Re: Newbie question
Date: 2003-06-27 02:47:42
Message-ID: 23574.1056682062@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Bruno Wolff III <bruno(at)wolff(dot)to> writes:
>> I want the column to accept the column value which is absolutely length of two.

> A char(2) will always have two characters (unless it is NULL). If you want
> to check for two nonblank characters or two letters or something like
> that use a check constraint.

> Some like:
> create table test (
> col1 char(2) constraint bad_length check (col1 ~ '^[a-zA-Z][a-zA-Z]$'
> );

The most obvious way to my mind is
check(length(rtrim(col1)) = 2)
if "length" is simply defined as "number of characters excluding
trailing blanks". If you want a more complex check then something
like Bruno's example will probably get the job done.

If you are going to apply a constraint like this then I'd counsel just
declaring the column as text --- making it char(2) simply means that
the system is applying an extra check that is redundant with your
constraint.

regards, tom lane

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Tom Lane 2003-06-27 03:33:27 Re: pg_restore --data-only (-a) does not work
Previous Message Bruce Momjian 2003-06-27 01:30:10 Re: 7.4 Documetation