pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
unique index on fields with possible null values
2+ messages / 2 participants
[nested] [flat]

* unique index on fields with possible null values
@ 2007-07-27 00:59 Dmitry Ruban <dmitry@ruban.biz>
  2007-07-27 02:49 ` Re: unique index on fields with possible null values Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Dmitry Ruban @ 2007-07-27 00:59 UTC (permalink / raw)
  To: pgsql-sql

Hello pgsql-sql,

I'm trying to find a solution for unique index on fields with possible
null values.

Example table:

CREATE TABLE test (
 a integer NOT NULL,
 b integer NULL
);

As long as unique index can't check if there are records with null
values i found the only one solution for this problem:

CREATE UNIQUE INDEX test_uniq1 ON test (a, COALESCE(b,0));

Are there any other ways of doing this?

-- 
Best regards,
 Dmitry                          mailto:dmitry@ruban.biz




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: unique index on fields with possible null values
  2007-07-27 00:59 unique index on fields with possible null values Dmitry Ruban <dmitry@ruban.biz>
@ 2007-07-27 02:49 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2007-07-27 02:49 UTC (permalink / raw)
  To: Dmitry Ruban <dmitry@ruban.biz>; +Cc: pgsql-sql

Dmitry Ruban <dmitry@ruban.biz> writes:
> I'm trying to find a solution for unique index on fields with possible
> null values.

You appear to be hoping that a unique index would constrain a column to
contain at most one null entry.  It doesn't work like that, and I
strongly urge you to reconsider what you're using null for.  I think you
are in for a world of hurt, well beyond this one particular point,
because the SQL spec's semantics for null are not designed around the
idea that it represents a single distinguishable value.  What it
represents is "unknown".

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2007-07-27 02:49 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-07-27 00:59 unique index on fields with possible null values Dmitry Ruban <dmitry@ruban.biz>
2007-07-27 02:49 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox