pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Bruno Wolff III <bruno@wolff.to>
Cc: Vinay <vinay@mdp.net>
Cc: pgsql-admin@postgresql.org
Subject: Re: Newbie question
Date: Thu, 26 Jun 2003 22:47:42 -0400
Message-ID: <23574.1056682062@sss.pgh.pa.us> (raw)
In-Reply-To: <20030626203911.GA10461@wolff.to>
References: <001c01c33c1d$843e0100$157a40c0@vinay>
	<20030626203911.GA10461@wolff.to>

Bruno Wolff III <bruno@wolff.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



view thread (18+ messages)  latest in thread

Message-ID: <23574.1056682062@sss.pgh.pa.us>
Permalink:  ../23574.1056682062@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/23574.1056682062@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: tgl@sss.pgh.pa.us, bruno@wolff.to, vinay@mdp.net
  Subject: Re: Newbie question
  In-Reply-To: <23574.1056682062@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox