From: Michael Fuhr <mike@fuhr.org>
To: Richard Broersma Jr <rabroersma@yahoo.com>
Cc: SQL Postgresql List <pgsql-sql@postgresql.org>
Subject: Re: Celko's Puzzle Number 5
Date: Sat, 8 Jul 2006 05:54:03 -0600
Message-ID: <20060708115403.GA13518@winnie.fuhr.org> (raw)
In-Reply-To: <20060708063457.56830.qmail@web31810.mail.mud.yahoo.com>
References: <20060708063457.56830.qmail@web31810.mail.mud.yahoo.com>
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
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: mike@fuhr.org, rabroersma@yahoo.com
Subject: Re: Celko's Puzzle Number 5
In-Reply-To: <20060708115403.GA13518@winnie.fuhr.org>
* 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