Re: Howto have a unique restraint on UPPER (textfield)

From: Andreas <maps(dot)on(at)gmx(dot)net>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Howto have a unique restraint on UPPER (textfield)
Date: 2010-01-31 20:48:28
Message-ID: 4B65EC9C.6000606@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Joshua Tolley schrieb:
> On Sun, Jan 31, 2010 at 03:26:14AM +0100, Andreas wrote:
>
>> Hi,
>>
>> is there a way to define a unique restraint on UPPER (textfield)?
>>
>> E.g. mytable (
>> name_id serial PRIMARY KEY,
>> name varchar(255),
>> UNIQUE ( upper (name) )
>> )
>>
>> psql throws a syntax error because of the upper() function.
>>
>> I need to prohibit that 2 of strings like cow, Cow, CoW appears in
>> the name-column.
>>
>
> Like this:
>
> 5432 josh(at)josh# create table c (d text);
> CREATE TABLE
> 5432 josh(at)josh*# create unique index c_ix on c (upper(d));
> CREATE INDEX
> 5432 josh(at)josh*# insert into c (d) values ('text');
> INSERT 0 1
> 5432 josh(at)josh*# insert into c (d) values ('tExt');
> ERROR: duplicate key value violates unique constraint "c_ix"
>
Thanks for clearing this up. :)

It works with CREATE UNIQUE INDEX.
So I had the missconception that UNIQUE (...) within CREATE TABLE (...)
was actually just an shorter way to define a unique index which it is not.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2010-01-31 21:07:46 Re: Howto have a unique restraint on UPPER (textfield)
Previous Message msi77 2010-01-31 08:02:17 Re: Howto have a unique restraint on UPPER (textfield)