Re: constraint question

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: mwaples(at)optusnet(dot)com(dot)au
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: constraint question
Date: 2000-12-31 23:12:47
Message-ID: 19293.978304367@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

mwaples(at)optusnet(dot)com(dot)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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Brett W. McCoy 2000-12-31 23:48:17 Re: constraint question
Previous Message D. Duccini 2000-12-31 22:39:20 Re: constraint question