X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A31219FA232 for ; Mon, 3 Jul 2006 13:02:35 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 18627-04 for ; Mon, 3 Jul 2006 13:02:26 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from wolff.to (wolff.to [66.93.197.194]) by postgresql.org (Postfix) with SMTP id 9FC759FA192 for ; Mon, 3 Jul 2006 13:02:25 -0300 (ADT) Received: (qmail 31659 invoked by uid 500); 3 Jul 2006 16:06:43 -0000 Date: Mon, 3 Jul 2006 11:06:43 -0500 From: Bruno Wolff III To: deb.vanni@tiscali.it Cc: pgsql-sql@postgresql.org Subject: Re: unique values of profile in the whole system Message-ID: <20060703160643.GA29299@wolff.to> Mail-Followup-To: Bruno Wolff III , deb.vanni@tiscali.it, pgsql-sql@postgresql.org References: <1151510191.273338.208060@y41g2000cwy.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1151510191.273338.208060@y41g2000cwy.googlegroups.com> User-Agent: Mutt/1.4.2.1i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/33 X-Sequence-Number: 25607 On Wed, Jun 28, 2006 at 08:56:31 -0700, deb.vanni@tiscali.it wrote: > * tbl_data_type : contains the data type of the profile, their id > and their names. E.g.: id=1, data type name="last name"; id=2, > data type name="address", and so on > * tbl_data : the data of all the profiles of the system; it has > three columns: the id of the profile the data belongs to (linked > to the tbl_user), the data type id (linked to tbl_data_type) and > the value of the data. E.g.: profile=1, data_type_1=1, > value="Smith", and so on > Suppose we have a data type named "unique_id", which value should be > stored in tbl_data. The value must be unique in the whole system, so > the > profiles store only one "unique_id", and I have to able to identify a > profile by this value(that's why must be unique!). > Generating such a unique id it's not a problem, using e.g. a sequence. > The problem is the user can change this value accessing to the proper > stored procedure, and the system should check that the value chosen do > not violate the requirement of uniqueness. > > I have only two solutions, I'd be glad to hear from you if they are > correct, or if you have already encountered similar problems and you > can > point me to some useful document. You could add a flag to tbl_data that indicates whether or not the data should be unique. Then you can create a partial index where this flag is true over the combination of type id and value. For referential integrity of the flag, you can add the same flag to the tbl_data_type table and make a unique index over the id type and the flag and then make the reference from tbl_data to tbl_data_type use the type id and flag as a foreign key instead of just type id.