Celko's Puzzle Number 5

From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: SQL Postgresql List <pgsql-sql(at)postgresql(dot)org>
Subject: Celko's Puzzle Number 5
Date: 2006-07-08 06:34:57
Message-ID: 20060708063457.56830.qmail@web31810.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message onin 2006-07-08 08:57:03 (no subject)
Previous Message Michael Glaesemann 2006-07-08 04:17:12 Re: Atomar SQL Statement