Re: Celko's Puzzle Number 5 - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: Celko's Puzzle Number 5
Date
Msg-id 20060708115403.GA13518@winnie.fuhr.org
Whole thread Raw
In response to Celko's Puzzle Number 5  (Richard Broersma Jr <rabroersma@yahoo.com>)
Responses Re: Celko's Puzzle Number 5
List 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


pgsql-sql by date:

Previous
From: Patrice OLIVER
Date:
Subject: unsubscribe
Next
From: Richard Broersma Jr
Date:
Subject: Re: table joining duplicate records