X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id CBDED9FA502 for ; Sat, 22 Jul 2006 10:56:23 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 39157-01 for ; Sat, 22 Jul 2006 13:56:17 +0000 (UTC) X-Greylist: delayed 00:23:08.652705 by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 18D699FA28E for ; Sat, 22 Jul 2006 10:56:17 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by svr4.postgresql.org (Postfix) with ESMTP id 06BFA5AFB1E for ; Sat, 22 Jul 2006 13:33:04 +0000 (GMT) Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1G4HbF-0004gL-00 for ; Sat, 22 Jul 2006 14:32:57 +0100 Date: Sat, 22 Jul 2006 14:32:57 +0100 To: pgsql-sql@postgresql.org Subject: Referential integrity (foreign keys) across multiple tables Message-ID: <20060722133257.GA9785@furbychan.cocan.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.9i From: Richard Jones X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200607/285 X-Sequence-Number: 25859 Simplified schema: create table hosts ( id serial primary key, hostname text not null ); create table pages ( id serial primary key, hostid int not null references hosts (id), url text not null, unique (hostid, url) ); create table page_contents ( pageid int not null references pages (id), section text not null ); (There are many hosts, many pages per host, and many page_contents sections per page). Now I want to add a column to page_contents, say called link_name, which is going to reference the pages.url column for the particular host that this page belongs to. Something like: alter table page_contents add link_name text; alter table page_contents add constraint foo foreign key (p.hostid, link_name) references pages (hostid, url) where p.id = pageid; Obviously that second statement isn't going to compile. I don't want to add the hostid column to page_contents table because I have a lot of old code accessing the database which would be hard to change (the old code would no longer be able to insert page_contents rows). Is this possible somehow? Perhaps by adding a second table? Do I have to use triggers, and if so is that as robust as referential integrity? Rich. -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com