pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Broersma Jr <rabroersma@yahoo.com>
To: SQL Postgresql List <pgsql-sql@postgresql.org>
Subject: Celko's Puzzle Number 5
Date: Fri, 7 Jul 2006 23:34:57 -0700 (PDT)
Message-ID: <20060708063457.56830.qmail@web31810.mail.mud.yahoo.com> (raw)

No matter how I try it, I can't getting the book's answer for this puzzle to work.  Does anyone
know of a solution that will work for this problem.  

The Problem is:
"How do you ensure that a column will have a single alphabetic character string in it? (That means
no spaces, no numbers, and no special characters.)"

The book's solution is as follows:

CREATE TABLE Foobar
(alpha_only VARCHAR(6)
            CHECK ((UPPER(TRIM(alpha_only)) || 'AAAAA')
                   BETWEEN 'AAAAAA' AND 'ZZZZZZ')
);

However,  this check constraint only prevents numerics beginning with 'A'.
So the constraint works by preventing following strings that begin with these kinds of characters:
!....,
4....,
A!...,
A4...

But the constraint fails to prevent non-alphabetic characters when the string starts with a
character  > A.  Thus B thru Z can be follow on not alphabetic characters.

I compared the result from PostgreSQL with sqlite and access.  They returned the same result. 
According to the text, this solution "could" have been generalized to work with more complicated
strings as well.  For example string "masks" could be used to enforce a kind of tagging convention
 like 'AA44444', 'BB55555'.

Has anyone seen or done anything like this before?

I am interested to hear what kind of solutions there are.

Regards,

Richard Broersma Jr. 



view thread (3+ messages)  latest in thread

Message-ID: <20060708063457.56830.qmail@web31810.mail.mud.yahoo.com>
Permalink:  ../20060708063457.56830.qmail@web31810.mail.mud.yahoo.com/
Also on:    postgresql.org/message-id/20060708063457.56830.qmail@web31810.mail.mud.yahoo.com

 · 

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-sql@postgresql.org
  Cc: rabroersma@yahoo.com
  Subject: Re: Celko's Puzzle Number 5
  In-Reply-To: <20060708063457.56830.qmail@web31810.mail.mud.yahoo.com>

* 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