Re: constraint question - Mailing list pgsql-novice

From Tom Lane
Subject Re: constraint question
Date
Msg-id 19293.978304367@sss.pgh.pa.us
Whole thread Raw
In response to constraint question  (mwaples@optusnet.com.au)
List pgsql-novice
mwaples@optusnet.com.au writes:
> 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 ?

Sure, use a regexp pattern match, eg

regression=# create table fooey (f1 text check (f1 ~ '^[A-Za-z0-9]*$'));
CREATE
regression=# insert into fooey values('zzz33');
INSERT 145186 1
regression=# insert into fooey values('zzz 33');
ERROR:  ExecAppend: rejected due to CHECK constraint fooey_f1
regression=#

The pattern match operators are not very well documented in the 7.0
docs, but see
http://www.postgresql.org/devel-corner/docs/postgres/functions-matching.htm

            regards, tom lane

pgsql-novice by date:

Previous
From: mwaples@optusnet.com.au
Date:
Subject: Re: constraint question
Next
From: "Brett W. McCoy"
Date:
Subject: Re: constraint question