Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 291F52E1C3A for ; Fri, 7 Mar 2008 05:05:35 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 87998-01 for ; Fri, 7 Mar 2008 05:05:32 -0400 (AST) X-Greylist: delayed 00:31:08.936235 by SQLgrey-1.7.5 Received: from smtp2f.orange.fr (smtp2f.orange.fr [80.12.242.151]) by postgresql.org (Postfix) with ESMTP id 3E31D2E1988 for ; Fri, 7 Mar 2008 05:05:32 -0400 (AST) Received: from smtp2f.orange.fr (mwinf2f21 [10.232.18.121]) by mwinf2f15.orange.fr (SMTP Server) with ESMTP id 2AA311C049F0 for ; Fri, 7 Mar 2008 09:34:24 +0100 (CET) Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2f21.orange.fr (SMTP Server) with ESMTP id 7DC7170000B0 for ; Fri, 7 Mar 2008 09:34:21 +0100 (CET) Received: from scoobydo (unknown [90.25.90.78]) by mwinf2f21.orange.fr (SMTP Server) with ESMTP id 26A9D700009D; Fri, 7 Mar 2008 09:34:21 +0100 (CET) X-ME-UUID: 20080307083421158.26A9D700009D@mwinf2f21.orange.fr From: =?iso-8859-1?Q?S=E9bastien_Meudec?= To: "'Bart Degryse'" , "'Jamie Tufnell'" , Subject: Re: Composite UNIQUE across two tables? Date: Fri, 7 Mar 2008 09:40:08 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 Thread-Index: AciALAFf131EveTnQ8GjtFODnRC5WwAAkbgg In-Reply-To: <47D100E0.A3DD.0030.0@indicator.be> Message-Id: <20080307083421.26A9D700009D@mwinf2f21.orange.fr> 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/39 X-Sequence-Number: 30367 Hi Bart, I'm following this topic with interest. Could you describe me more how you design an unique index with both a = column name and a function name by an example Thx. Sebastien ________________________________________ De=A0: pgsql-sql-owner@postgresql.org = [mailto:pgsql-sql-owner@postgresql.org] De la part de Bart Degryse Envoy=E9=A0: vendredi 7 mars 2008 08:46 =C0=A0: Jamie Tufnell; pgsql-sql@postgresql.org Objet=A0: Re: [SQL] Composite UNIQUE across two tables? create a function that, given the siteid returns the sitegroupid create a unique index on username and that function >>> "Jamie Tufnell" 2008-03-06 20:04 >>> Hi, I'm remodeling our authentication tables at the moment to accomodate future changes.=A0 I've hit a wall and thought I'd ask here for some help.=A0 Hopefully I'm just missing something simple. I'll do my best to explain the domain.. We have users, sites, and groups of sites. Usernames should be unique within a site group. Each user is tied to one, and only one, site. Each site belongs to exactly one group, and one group can contain many sites. (some sites are actually groupless in reality, but at one point in my design I figured it might make it easier to force them to be in a group of 1 -- perhaps that's part of my problem?). A user has implied permission to access any site in their group of sites. So far this is what I have: CREATE TABLE site_groups ( =A0=A0=A0 id serial, =A0=A0=A0 name varchar(120) not null, =A0=A0=A0 PRIMARY KEY (id) ); CREATE TABLE sites ( =A0=A0=A0 id serial, =A0=A0=A0 site_group_id integer not null, =A0=A0=A0 name varchar(120) not null, =A0=A0=A0 PRIMARY KEY (id), =A0=A0=A0 FOREIGN KEY (site_group_id) REFERENCES site_groups (id) ); CREATE TABLE users ( =A0=A0=A0 id serial, =A0=A0=A0 site_id integer not null, =A0=A0=A0 username varchar(120) not null, =A0=A0=A0 PRIMARY KEY (id), =A0=A0=A0 FOREIGN KEY (site_id) REFERENCES sites (id) ); The problem is the "Usernames should be unique within a site group." I could include a site_group_id FK in the users table, and put a UNIQUE(username, site_group_id), but it feels like I'm doing something wrong here since the user's site_group_id is already implied by the user's site_id. When users login I get their username, password and site name. Without a UNIQUE constraint on username+site_group_id, it's possible I'll get more than one user id matching the same login information which shouldn't happen. I suppose I could write a trigger to enforce this uniqueness, but it just feels like I'm probably doing something wrong elsewhere. Any help/abuse greatly appreciated :-) Cheers, J. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://mail.postgresql.org/mj/mj_wwwusr?domain=3Dpostgresql.org&extra=3Dp= gsql-sq l