X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 6A7FDD1B238 for ; Wed, 2 Jun 2004 09:43:46 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 13380-05 for ; Wed, 2 Jun 2004 12:43:48 +0000 (GMT) Received: from spoetnik.xs4all.nl (spoetnik.xs4all.nl [213.84.24.58]) by svr1.postgresql.org (Postfix) with ESMTP id 8C377D1B257 for ; Wed, 2 Jun 2004 09:43:42 -0300 (ADT) Received: by spoetnik.xs4all.nl (Postfix, from userid 1000) id 26A911CC6A; Wed, 2 Jun 2004 14:43:45 +0200 (CEST) Date: Wed, 2 Jun 2004 14:43:45 +0200 From: Reinoud van Leeuwen To: pgsql-sql@postgresql.org Subject: Re: UNIQUE columnt depdening on other column??? Message-ID: <20040602124345.GQ23232@spoetnik.xs4all.nl> Mail-Followup-To: pgsql-sql@postgresql.org References: <20040602113937.D45E11765F@ar-sd.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20040602113937.D45E11765F@ar-sd.net> User-Agent: Mutt/1.4.2.1i X-message-flag: Warning! The sender of this mail thinks you should use a more secure email client. X-Virus-Scanned: by amavisd-new at hub.org X-Archive-Number: 200406/12 X-Sequence-Number: 17957 On Wed, Jun 02, 2004 at 02:39:45PM +0300, Andrei Bintintan wrote: > Hi, > > I have a problem. > > Let's say I have the following table: > CREATE TABLE rekl_element( > id serial PRIMARY KEY, > active boolean NOT NULL DEFAULT 'y', > num int4 NOT NULL, > text varchar(10) > ); > > Now I want that "num" column is "unique" but only for those columns that > have active='y'. For the columns that have active='f' I don't care if num is > unique or not. I'm asking this because num will be doubled some times. You can use a little trick for this. Add a column 'uniqueness' that has a default nextval ('mysequence'::text). Make an unique index on the columns (num, uniqueness). Obviously, this will work because the column uniqueness has unique values. Now write a trigger that sets the uniqueness column to 0 when the active column equals 'y'. This will result in: - unique num columns (or the index will fail) where active = 'y' - arbitrary num colums (index will always be unique) where active = 'n' -- __________________________________________________ "Nothing is as subjective as reality" Reinoud van Leeuwen reinoud.v@n.leeuwen.net http://www.xs4all.nl/~reinoud __________________________________________________