Re: constraint question

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: mwaples@optusnet.com.au
Cc: pgsql-novice@postgresql.org
Date: 2000-12-31T23:12:47Z
Lists: 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