Обсуждение: unique (a,b)?

Поиск
Список
Период
Сортировка

unique (a,b)?

От
Knut Suebert
Дата:
Hello,

I thought that a table-constraint unique (a,b) in a table like

a | b
--+--
1 | 1
2 | 2
3 | 3

would allow an insert of (1,2) as it is an unique combination. That seems to be wrong.

Is there an easy way to check unique combinations in a table definition? Or
do I have to define a special trigger?

Thanks,
Knut

Re: unique (a,b)?

От
Knut Suebert
Дата:
ksueber schrieb:
> would allow an insert of (1,2) as it is an unique combination. That seems to be wrong.

Seems. The error was somewhere else, sorry.
Knut

Re: unique (a,b)?

От
Tom Lane
Дата:
Knut Suebert <knut.suebert@web.de> writes:
> I thought that a table-constraint unique (a,b) in a table like

> a | b
> --+--
> 1 | 1
> 2 | 2
> 3 | 3

> would allow an insert of (1,2) as it is an unique combination. That seems to be wrong.

It works fine for me:

regression=# create table ff (a int, b int, unique(a,b));
NOTICE:  CREATE TABLE/UNIQUE will create implicit index 'ff_a_key' for table 'ff'
CREATE
regression=# insert into ff values(1,1);
INSERT 1401043 1
regression=# insert into ff values(2,2);
INSERT 1401044 1
regression=# insert into ff values(3,3);
INSERT 1401045 1
regression=# insert into ff values(1,2);
INSERT 1401046 1
regression=# select * from ff;
 a | b
---+---
 1 | 1
 2 | 2
 3 | 3
 1 | 2
(4 rows)
regression=# insert into ff values(1,2);
ERROR:  Cannot insert a duplicate key into unique index ff_a_key

If you're having a problem you'll need to be more specific.

            regards, tom lane