From: | "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> |
---|---|
To: | "Voillequin, Jean-Marc" <Jean-Marc(dot)Voillequin(at)moodys(dot)com> |
Cc: | "pgsql-sql(at)lists(dot)postgresql(dot)org" <pgsql-sql(at)lists(dot)postgresql(dot)org> |
Subject: | Re: unique index with several columns |
Date: | 2022-03-04 15:32:15 |
Message-ID: | CAKFQuwa4dELLaEdm2UsZK-LyRA08VYOYQZyxNs8zF25nYzgv3w@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
On Fri, Mar 4, 2022 at 8:07 AM Voillequin, Jean-Marc <
Jean-Marc(dot)Voillequin(at)moodys(dot)com> wrote:
>
>
> When one of the field is null, PG considers that the tuple is not the same:
>
> ('a',null) is not equal to ('a',null)
>
> So, the unique constraint is not violated in PG.
>
>
>
> But is there a way to have the same feature than Oracle?
>
>
>
> I already tried with:
>
> create unique index idx on t(coalesce(c1,''),coalesce(c2,''))
>
>
>
> But in this case, I cannot insert several (null,null) without raising a
> duplicate key error.
>
>
>
You just said you wanted nulls to be considered equal to each other...
Not tested but these should work in conjunction with each other:
CREATE UNIQUE INDEX idx ON t (c1, c2) WHERE c1 IS NOT NULL AND c2 IS NOT
NULL;
CREATE UNIQUE INDEX idx2 ON t (c1) WHERE c2 IS NULL AND c1 IS NOT NULL;
CREATE UNIQUE INDEX idx3 ON t (c2) WHERE c1 IS NULL AND c2 IS NOT NULL;
--CREATE INDEX idx4 ON t (c1, c2) WHERE c1 IS NULL AND c2 IS NULL;
(pointless...but symmetric with the others)
Hopefully this helps.
David J.
From | Date | Subject | |
---|---|---|---|
Next Message | Marc Mamin | 2022-03-04 15:32:38 | AW: unique index with several columns |
Previous Message | Kevin Tu | 2022-03-04 15:29:39 | Re: limits on the number of concurrent DBMS sessions per account |