Re: constraint question - Mailing list pgsql-novice

From Brett W. McCoy
Subject Re: constraint question
Date
Msg-id Pine.LNX.4.30.0012311847080.21590-100000@chapelperilous.net
Whole thread Raw
In response to constraint question  (mwaples@optusnet.com.au)
List 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!"


pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: constraint question
Next
From: Boris
Date:
Subject: Blob question -(((