Re: Can I CONSTRAIN a particular value to be UNIQUE?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Can I CONSTRAIN a particular value to be UNIQUE?
Дата
Msg-id 23371.1007066300@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Can I CONSTRAIN a particular value to be UNIQUE?  (reina@nsi.edu (Tony Reina))
Список pgsql-sql
reina@nsi.edu (Tony Reina) writes:
> I'd like to have some way for SQL to give me an error if I attempt to
> insert more than one instance of a given subject, target, and trial
> where success = 1, but would allow me to insert as many instances of
> the same subject, target, and trial where success = 0.

Two possibilities:

1. If you're not wedded to that particular data representation, consider  making the success values 1 and NULL not 1
and0.  Then a unique  index across all four columns would act as you desire.
 

2. In PG 7.2, partial indexes work again, so you could do
CREATE UNIQUE INDEX ... (subject, target, trial) WHERE success = 1;

Which of these is better depends on how many of each sort of row
you expect to have, and whether you'll be doing any queries wherein
you could use an index on subject/target/trial for the non-success
rows.
        regards, tom lane


В списке pgsql-sql по дате отправления:

Предыдущее
От: "G. Anthony Reina"
Дата:
Сообщение: Re: Can I CONSTRAIN a particular value to be UNIQUE?
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Can I CONSTRAIN a particular value to be UNIQUE?