pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedCelko's Puzzle Number 5
3+ messages / 2 participants
[nested] [flat]
* Celko's Puzzle Number 5
@ 2006-07-08 06:34 Richard Broersma Jr <rabroersma@yahoo.com>
0 siblings, 1 reply; 3+ messages in thread
From: Richard Broersma Jr @ 2006-07-08 06:34 UTC (permalink / raw)
To: 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.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Celko's Puzzle Number 5
@ 2006-07-08 11:54 Michael Fuhr <mike@fuhr.org>
parent: Richard Broersma Jr <rabroersma@yahoo.com>
0 siblings, 1 reply; 3+ messages in thread
From: Michael Fuhr @ 2006-07-08 11:54 UTC (permalink / raw)
To: Richard Broersma Jr <rabroersma@yahoo.com>; +Cc: pgsql-sql
On Fri, Jul 07, 2006 at 11:34:57PM -0700, Richard Broersma Jr wrote:
> "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.)"
You can check patterns with regular expressions. PostgreSQL supports
SIMILAR TO, which is standard SQL since SQL:1999, and POSIX regular
expressions; see "Pattern Matching" in the "Functions and Operators"
chapter of the documentation:
http://www.postgresql.org/docs/8.1/interactive/functions-matching.html
Any of these CHECK expressions should work:
CHECK (alpha_only SIMILAR TO '[A-Za-z]+')
CHECK (alpha_only ~ '^[A-Za-z]+$')
CHECK (alpha_only ~* '^[a-z]+$')
Unfortunately, even though SIMILAR TO has been standard SQL for
several years, not all databases implement it. Many databases
do support regular expressions but generally via a non-standard
syntax (as PostgreSQL does with its ~, ~*, !*, and !~* operators).
--
Michael Fuhr
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Celko's Puzzle Number 5
@ 2006-07-08 15:20 Richard Broersma Jr <rabroersma@yahoo.com>
parent: Michael Fuhr <mike@fuhr.org>
0 siblings, 0 replies; 3+ messages in thread
From: Richard Broersma Jr @ 2006-07-08 15:20 UTC (permalink / raw)
To: Michael Fuhr <mike@fuhr.org>; +Cc: pgsql-sql
> http://www.postgresql.org/docs/8.1/interactive/functions-matching.html
>
> Any of these CHECK expressions should work:
>
> CHECK (alpha_only SIMILAR TO '[A-Za-z]+')
> CHECK (alpha_only ~ '^[A-Za-z]+$')
> CHECK (alpha_only ~* '^[a-z]+$')
>
> Unfortunately, even though SIMILAR TO has been standard SQL for
> several years, not all databases implement it. Many databases
> do support regular expressions but generally via a non-standard
> syntax (as PostgreSQL does with its ~, ~*, !*, and !~* operators).
Thanks for the link!
Regards,
Richard Broersma Jr.
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-07-08 15:20 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-07-08 06:34 Celko's Puzzle Number 5 Richard Broersma Jr <rabroersma@yahoo.com>
2006-07-08 11:54 ` Michael Fuhr <mike@fuhr.org>
2006-07-08 15:20 ` Richard Broersma Jr <rabroersma@yahoo.com>
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