| From: | Michael Fuhr <mike(at)fuhr(dot)org> |
|---|---|
| To: | Richard Broersma Jr <rabroersma(at)yahoo(dot)com> |
| Cc: | SQL Postgresql List <pgsql-sql(at)postgresql(dot)org> |
| Subject: | Re: Celko's Puzzle Number 5 |
| Date: | 2006-07-08 11:54:03 |
| Message-ID: | 20060708115403.GA13518@winnie.fuhr.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | 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
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Richard Broersma Jr | 2006-07-08 15:19:19 | Re: table joining duplicate records |
| Previous Message | Patrice OLIVER | 2006-07-08 11:35:04 | unsubscribe |