Re: Multi-Column Constraints and Null Values

From: Jeff Frost <jeff(at)frostconsultingllc(dot)com>
To: Phillip Tornroth <ptornroth(at)intellidot(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Multi-Column Constraints and Null Values
Date: 2006-04-29 20:14:36
Message-ID: Pine.LNX.4.64.0604291314040.9581@discord.dyndns.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 27 Apr 2006, Phillip Tornroth wrote:

> I have a problem in a number of my tables. I'd like to add uniqueness
> constraints across multiple columns, but some of the columns are nullable.
> This gives me trouble since when I create a constraint on columns A and B..
> I'd like the constraint to be enforced such that you couldn't insert values
> ("value for A", null) twice. I understand why the constraints work this way,
> but I'm wondering if anyone knows of a workaround.
>

Phill,

You likely want a multicolumn unique index created like so:

CREATE UNIQUE INDEX foo_A_B_unique_idx ON foo (A,B);

See the docs here:
http://www.postgresql.org/docs/current/interactive/indexes-unique.html

--
Jeff Frost, Owner <jeff(at)frostconsultingllc(dot)com>
Frost Consulting, LLC http://www.frostconsultingllc.com/
Phone: 650-780-7908 FAX: 650-649-1954

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Bruno Wolff III 2006-04-29 20:27:37 Re: Multi-Column Constraints and Null Values
Previous Message Marc G. Fournier 2006-04-29 17:33:00 Ignore, test of an alias