Re: Can I CONSTRAIN a particular value to be UNIQUE? - Mailing list pgsql-sql

From Tom Lane
Subject Re: Can I CONSTRAIN a particular value to be UNIQUE?
Date
Msg-id 23371.1007066300@sss.pgh.pa.us
Whole thread Raw
In response to Can I CONSTRAIN a particular value to be UNIQUE?  (reina@nsi.edu (Tony Reina))
List 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 by date:

Previous
From: "G. Anthony Reina"
Date:
Subject: Re: Can I CONSTRAIN a particular value to be UNIQUE?
Next
From: Tom Lane
Date:
Subject: Re: Can I CONSTRAIN a particular value to be UNIQUE?