Re: Unique indicies - Mailing list pgsql-general

From Dot Yet
Subject Re: Unique indicies
Date
Msg-id 93bc4af40802220638i241b16c2nf22c3b81f632daf2@mail.gmail.com
Whole thread Raw
In response to Unique indicies  (Naz Gassiep <naz@mira.net>)
Responses Re: Unique indicies
List pgsql-general
that would mean

Every row in foo for column f1 has to be unique
and
Every row in foo for column f2 has to be unique

Say for example:

create table test1 ( a int, b int ) ;
create unique index idx_t1_a on test1(a) ;
create unique index idx_t1_b on test1(b) ;


insert into test1 values (1,1) ; -- OK
insert into test1 values (1,2) ; -- FAIL
insert into test1 values (2,2) ; -- OK
insert into test1 values (2,1) ; -- FAIL

This is because the each record is composed to two unique columns, and the uniqueness is broken down to each column, rather than the record as a whole.

In case when you are creating the index as:
create unique index idx_t1_ab on test1(a, b) ;

insert into test1 values (1,1) ; -- OK
insert into test1 values (1,2) ; -- OK
insert into test1 values (2,2) ; -- OK
insert into test1 values (2,1) ; -- OK


This is because, each combination of column a and column b is unique in it's entirety.

hth,
dotyet

On Fri, Feb 22, 2008 at 6:53 AM, Naz Gassiep <naz@mira.net> wrote:
If you have an index like this:

CREATE UNIQUE INDEX foo ON tablename (f1, f2);

Is there any value in having independent indicies on f1 and f2 as well
or are they unnecessary?

Thanks
- Naz.

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

pgsql-general by date:

Previous
From: luca.ciciriello@email.it
Date:
Subject: configure build flags
Next
From: "Carlos H. Reimer"
Date:
Subject: current_query pg_stat_activity column