Re: question about unique indexes

From: Cédric Villemain <cedric(dot)villemain(dot)debian(at)gmail(dot)com>
To: Jonathan Vanasco <postgres(at)2xlp(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: question about unique indexes
Date: 2010-05-10 00:15:36
Message-ID: AANLkTimmrO1w5LSRilrZ3MQmSrlRoYwlFY8RmBpwBSyp@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2010/5/10 Jonathan Vanasco <postgres(at)2xlp(dot)com>:
> -- running pg 8.4
>
> i have a table defining geographic locations
>
>        id
>        lat
>        long
>        country_id not null
>        state_id
>        city_id
>        postal_code_id
>
> i was given a unique index on
>        (country_id, state_id, city_id, postal_code_id)
>
> the unique index isn't working as i'd expect it to.  i was hoping someone
> could explain why:
>
> in the two records below, only country_id and state_id are assigned  ( aside
> from the serial )
>
> geographic_location_id | coordinates_latitude | coordinates_longitude |
> country_id | state_id | city_id | postal_code_id
> ------------------------+----------------------+-----------------------+------------+----------+---------+----------------
>                    312 |                      |                       |
>    233 |       65 |         |
>                    443 |                      |                       |
>    233 |       65 |         |
>
> i was under the expectation that the unique constraint would apply in this
> place.
>
> from the docs:
>        When an index is declared unique, multiple table rows with equal
> indexed values are not allowed. Null values are not considered equal. A
> multicolumn unique index will only reject cases where all indexed columns
> are equal in multiple rows.

NULLs are not considered equal, so you can have an UNIQUE on a column
with multiple times a NULL. You migth want to explicitely add a 'NOT
NULL' to your columns here.

>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

--
Cédric Villemain

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Yan Cheng CHEOK 2010-05-10 00:54:39 Increasing checkpoint_segments - Any bad thing might happen?
Previous Message Jonathan Vanasco 2010-05-10 00:09:58 question about unique indexes