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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: reina(at)nsi(dot)edu (Tony Reina)
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Can I CONSTRAIN a particular value to be UNIQUE?
Date: 2001-11-29 20:38:20
Message-ID: 23371.1007066300@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

reina(at)nsi(dot)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 and 0. 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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message G. Anthony Reina 2001-11-29 20:48:04 Re: Can I CONSTRAIN a particular value to be UNIQUE?
Previous Message Stephan Szabo 2001-11-29 19:59:40 Re: Can I CONSTRAIN a particular value to be UNIQUE?