X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 42E929DCA0F for ; Tue, 21 Mar 2006 10:58:37 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 91557-03 for ; Tue, 21 Mar 2006 10:58:41 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web50301.mail.yahoo.com (web50301.mail.yahoo.com [206.190.38.55]) by postgresql.org (Postfix) with SMTP id 071BE9DCC51 for ; Tue, 21 Mar 2006 10:58:34 -0400 (AST) Received: (qmail 68213 invoked by uid 60001); 21 Mar 2006 14:58:39 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Date:From:Subject:To:MIME-Version:Content-Type; b=ydh0EGfYc+Ub/m6PSD/eWV4F48FEbyxxq0AOESNZai+TxYUGGB6I64WAd6+cBKexGyBkEQYuNLbPcabh6y9SXn26MTDQxhAKXHSgvMCR1xF65jIpLFB8X8/5hfzuoDP0EgG6IeqeQvTZ1Rz0eULMBc5ZX7SHy+tLOb1mQZJmrF0= ; Message-ID: <20060321145839.68211.qmail@web50301.mail.yahoo.com> Date: Tue, 21 Mar 2006 06:58:39 -0800 (PST) From: Subject: Referential integrity broken (8.0.3), sub-select help To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=2.616 required=5 tests=[AWL=-0.220, DNS_FROM_RFC_ABUSE=0.479, DNS_FROM_RFC_WHOIS=0.879, FORGED_YAHOO_RCVD=0.928, NO_REAL_NAME=0.55] X-Spam-Score: 2.616 X-Spam-Level: ** X-Archive-Number: 200603/329 X-Sequence-Number: 24675 Hello, I've got 2 tables, "url" (U), and "bookmark" (B), with "bookmark" pointing to "url" via FK. Somehow I ended up with some rows in B referencing non-existent rows in U. This sounds super strange and dangerous to me, and it's not clear to me how/why PG let this happen. I'm using 8.0.3. Here are the table references I just mentioned: Table "bookmark": id SERIAL CONSTRAINT pk_bookmark_id PRIMARY KEY Table "url": url_id INTEGER CONSTRAINT fk_bookmark_id REFERENCES bookmark(id) Problem #1: Strange that PG allowed this to happen. Maybe my DDL above allows this to happen and needs to be tightened? I thought the above would ensure referential integrity, but maybe I need to specify something else? Problem #2: I'd like to find all rows in B that point to non-existent rows in U. I can do it with the following sub-select, I believe, but it's rather inefficient (EXPLAIN shows both tables would be sequentially scanned): SELECT * FROM bookmark WHERE id NOT IN (SELECT b.id FROM bookmark b, url u WHERE b.url_id=u.id); Is there a more efficient way to get the rows from "bookmark"? Thanks, Otis