Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id EDF892E00FD for ; Fri, 7 Mar 2008 11:33:35 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 80954-06 for ; Fri, 7 Mar 2008 11:33:23 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.154]) by postgresql.org (Postfix) with ESMTP id 6701D2E00C4 for ; Fri, 7 Mar 2008 11:33:31 -0400 (AST) Received: by fg-out-1718.google.com with SMTP id 22so416414fge.23 for ; Fri, 07 Mar 2008 07:33:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=2QPkbCccUX0KAkFnSuY2KrAdIjPIIMh6Ir3HWwo5F1E=; b=Il3Fn7wAIdZPmV8GJUU5cvrKlGhGi8tJrWHo4YD2Etnsq5RqZxv09G6UsQD3ZaoUxViiOAYuJQ9k3kEVj5T6UQbkm0uYsqtVQDWFdHSlm0vf99IbGqT1T8SJZnhQmkry42pax9gya4rHOEeJ+UYYPO64glCFna+wteCVFFO3JOg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=EadA80QpvmPp0ePpbS6IQK0LrlsVI+TS8fdwizZOcWvThuQPIVSQ53wtvET4cxkG5UFsCoeiJ+bEsakbSCSiP3CPc7exlwgTKyAag59e9mNpAJtzR1jiHbKeN+g3K09Ww5u9VrREcZFLSkKxgy/zpew0eG3dP3kvtvqcltgV4wM= Received: by 10.86.89.4 with SMTP id m4mr1650176fgb.12.1204904008881; Fri, 07 Mar 2008 07:33:28 -0800 (PST) Received: by 10.86.86.18 with HTTP; Fri, 7 Mar 2008 07:33:28 -0800 (PST) Message-ID: Date: Fri, 7 Mar 2008 15:33:28 +0000 From: "Jamie Tufnell" To: pgsql-sql@postgresql.org Subject: Re: Composite UNIQUE across two tables? In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200803/43 X-Sequence-Number: 30371 Hi Ray, On 3/7/08, Ray Madigan wrote: > How I think about it. > > A user has access to a site and all of the sites within the site group that > the site is in. > > if you reword your condition > > A user has access to all of the sites in a site group with a default defined > by site_id. Then there is no problem having both variables in the table. One field in the users table (site_id) implicitly ties the user to a site_group_id. One field in the users table (site_group_id) explicitly ties the user to a site_group_id. The problem I have (or had.. read below) was enforcing that those site_group_id's are equal. > Also, you have to trade off the cost of the table join to get the group_id > in all of the queries as opposed to the extra integer required. My thought > is that the extra Integer is small compared to the number of wueries that > have to run and would then adopt the second wording of the constraint. > > Even if you come up with an alternative, composite key you will still have > to deal with all of the table joins. The table joins isn't a big deal, but > it is unnecessary. When I asked about a composite FK in my previous message, I'd planned to use it in addition to your solution (not in place of)... the idea being to solve the problem mentioned above. What I was thinking is something like this .. I'm interested to hear your thoughts on this: users table: FOREIGN KEY (site_id, site_group_id) REFERENCES sites (id, site_group_id) sites table: UNIQUE (id, site_group_id) Assuming that's going to work... then I could use your suggestion of adding site_group_id to the users table. As you said, that will make writing SELECTs a lot simpler. How would I handle INSERTs / UPDATEs though without having to always specify both fields? I have a feeling it will require some functions being called by triggers / default values. That's all doable, but I wonder if there's a way of expressing these relationships without having to duplicate site_group_id. Cheers, J.