Skip site navigation (1) Skip section navigation (2)

Unique constraint on only some of the rows

From: A B <gentosaker(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Unique constraint on only some of the rows
Date: 2011-01-24 10:16:06
Message-ID: AANLkTikwhy6ZefkbjfrDYC-2yz9wMVHWsPAXvvO=RRKb@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-novice
Hello there!

If I want to create a table

create table users (
  id integer
  name varchar(8)
  enabled boolean
);

and a constraint  unique(id,name) but the unique constraint should
only be used for the enabled users, how can I do that?

The only way I can think of is to use null values when a user is not
enabled and use the constraint  unique(id,name,enable).

Are there other ways of doing this?

Responses

pgsql-novice by date

Next:From: Thomas KellererDate: 2011-01-24 10:43:33
Subject: Re: Unique constraint on only some of the rows
Previous:From: Sean DavisDate: 2011-01-24 09:52:59
Subject: Re: Select and update with limit and locking

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group