From: Tom Lane <tgl@sss.pgh.pa.us>
To: reina@nsi.edu
Cc: pgsql-sql@postgresql.org
Subject: Re: Can I CONSTRAIN a particular value to be UNIQUE?
Date: Thu, 29 Nov 2001 15:38:20 -0500
Message-ID: <23371.1007066300@sss.pgh.pa.us> (raw)
In-Reply-To: <f40d3195.0111291101.3b2a5074@posting.google.com>
References: <f40d3195.0111291101.3b2a5074@posting.google.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, reina@nsi.edu
Subject: Re: Can I CONSTRAIN a particular value to be UNIQUE?
In-Reply-To: <23371.1007066300@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox