X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 254D09FB1DA for ; Sat, 8 Jul 2006 08:54:17 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 72807-03 for ; Sat, 8 Jul 2006 08:54:12 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id 50B519FB22E for ; Sat, 8 Jul 2006 08:54:11 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.7/8.13.7) with ESMTP id k68Bs4VE092458 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 8 Jul 2006 05:54:07 -0600 (MDT) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.6/8.13.6) with ESMTP id k68Bs4aE013632; Sat, 8 Jul 2006 05:54:04 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.6/8.13.6/Submit) id k68Bs34k013631; Sat, 8 Jul 2006 05:54:03 -0600 (MDT) (envelope-from mfuhr) Date: Sat, 8 Jul 2006 05:54:03 -0600 From: Michael Fuhr To: Richard Broersma Jr Cc: SQL Postgresql List Subject: Re: Celko's Puzzle Number 5 Message-ID: <20060708115403.GA13518@winnie.fuhr.org> References: <20060708063457.56830.qmail@web31810.mail.mud.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060708063457.56830.qmail@web31810.mail.mud.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/132 X-Sequence-Number: 25706 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