Received: from sss.pgh.pa.us ([192.204.191.242]) by postgresql.org (8.11.3/8.11.4) with ESMTP id fATKccm27251 for ; Thu, 29 Nov 2001 15:38:38 -0500 (EST) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.4/8.11.4) with ESMTP id fATKcKk23374; Thu, 29 Nov 2001 15:38:20 -0500 (EST) To: reina@nsi.edu (Tony Reina) cc: pgsql-sql@postgresql.org Subject: Re: Can I CONSTRAIN a particular value to be UNIQUE? In-reply-to: References: Comments: In-reply-to reina@nsi.edu (Tony Reina) message dated "29 Nov 2001 11:01:11 -0800" Date: Thu, 29 Nov 2001 15:38:20 -0500 Message-ID: <23371.1007066300@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200111/420 X-Sequence-Number: 5802 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 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