Re: constraint question

Brett W. McCoy <bmccoy@chapelperilous.net>

From: "Brett W. McCoy" <bmccoy@chapelperilous.net>
To: <mwaples@optusnet.com.au>
Cc: <pgsql-novice@postgresql.org>
Date: 2000-12-31T23:48:17Z
Lists: pgsql-novice
On Mon, 1 Jan 2001 mwaples@optusnet.com.au wrote:

> I have table users with a varchar field user_name,
> Id like to restrict this to just alphanumeric characters
> can I do this with a check constraint ?
> or do I need to use a tigger and function ?

Use a regular expression in your check constraint:

create table reg_test (
        id integer primary key,
        descr varchar(20),
        check (descr ~* '^[A-Z0-9]*$')
);

insert into reg_test values (1, 'data\&');
ERROR:  ExecAppend: rejected due to CHECK constraint $1

-- Brett
                                     http://www.chapelperilous.net/~bmccoy/
---------------------------------------------------------------------------
Feeling amorous, she looked under the sheets and cried, "Oh, no,
it's Microsoft!"