Re: Unique cosntraint based on contents of a field?

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Jaime Casanova <jcasanov(at)systemguards(dot)com(dot)ec>
Cc: Warren Bell <warrenbell2(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Unique cosntraint based on contents of a field?
Date: 2010-04-14 22:09:55
Message-ID: 201004142209.o3EM9t416257@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jaime Casanova wrote:
> On Wed, Apr 7, 2010 at 10:30 PM, Warren Bell <warrenbell2(at)gmail(dot)com> wrote:
> > Is there a way to create a unique constraint based on the content of a
> > field? For instance, say you have an integer field where you only want one
> > record with the number 1 in that field but there can be many records with
> > say the number 2 or any other single number in that field.
> >
>
> create unique index unique_field_idx
> on table1(field)
> where field = 1;

Wow, great idea. I never thought of partial indexes as a method for
unique qualification. I am glad to see our documentation mentions this
usage:

http://www.postgresql.org/docs/8.4/static/sql-createindex.html

Another possible application is to use WHERE with UNIQUE to enforce
uniqueness over a subset of a table.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2010-04-15 00:07:27 Re: Removing files under pg_clog
Previous Message Mike Charles 2010-04-14 21:47:26 Re: Storing data on a regular lat/lon grid