Re: Alternatives to a unique indexes with NULL

From: Peter Hicks <peter(dot)hicks(at)poggs(dot)co(dot)uk>
To: John McKown <john(dot)archie(dot)mckown(at)gmail(dot)com>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Alternatives to a unique indexes with NULL
Date: 2015-01-17 13:00:02
Message-ID: 54BA5CD2.5060202@poggs.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi John

On 17/01/15 12:39, John McKown wrote:
> ​I read the above. As I understand it, you can have a unique index on
> a column which is NULL-able. That will guarantee that all the non-NULL
> values are unique. What it will not guarantee is that there will be at
> most one NULL value in the indexed column. Are you saying that what
> you want is a column with a unique index where you cannot have two or
> more rows with NULL in the indexed column?
That's correct - the application reads in a very old-format of
fixed-length file and, if any field in there is just a single space,
replaces it with a NULL, since a single space implies a null. However,
only one of the records needs a constraint on one of these fields.
> ​If so, then you will need to have a value to indicate the equivalent
> of NULL. Personally, I use a zero length string "" instead of a single
> blank ' '. This is value since you say this column is a "character
> varying(1)". Which seems a bit strange to me, but I don't know your
> application.
OK, that makes sense and it was more-or-less along the lines of what I
expected. I like the idea of a zero-length string versus a single
space, so I'll go implement that.

I believe the column type a Rails-ism, which creates 'string' fields
with a length constraint of 1 as 'character varying(1)'. Probably not
ideal, but there's usually a trade-off somewhere.

Thanks very much for your help and quick response!

Peter

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andreas Kretschmer 2015-01-17 13:03:34 Re: Alternatives to a unique indexes with NULL
Previous Message John McKown 2015-01-17 12:39:16 Re: Alternatives to a unique indexes with NULL