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 CFCE39DCA25 for ; Tue, 21 Mar 2006 11:16:29 -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 95303-04 for ; Tue, 21 Mar 2006 11:16:32 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id DB2BC9DC9E6 for ; Tue, 21 Mar 2006 11:16:25 -0400 (AST) Received: from smtp1.netmagicians.com (smtp1.netmagicians.com [202.87.39.91]) by svr4.postgresql.org (Postfix) with ESMTP id D46885AF877 for ; Tue, 21 Mar 2006 15:16:30 +0000 (GMT) Received: from mailserver.tatashare.com (unknown [202.149.193.242]) by smtp1.netmagicians.com (Postfix) with ESMTP id DC736210431; Tue, 21 Mar 2006 20:46:10 +0530 (IST) Received: from aftabn463 (unknown [172.16.16.236]) by mailserver.tatashare.com (Postfix) with SMTP id 967C73E4032; Tue, 21 Mar 2006 20:39:31 +0530 (IST) Reply-To: From: "Aftab Alam" To: , Subject: Re: Referential integrity broken (8.0.3), sub-select help Date: Tue, 21 Mar 2006 20:47:40 +0530 Message-ID: <001a01c64cfa$987c5d60$ec1010ac@aftabn463> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook CWS, Build 9.0.2416 (9.0.2910.0) In-Reply-To: <20060321145839.68211.qmail@web50301.mail.yahoo.com> Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-NetMagic-MailScanner-Information: Powered By NetMagic Mail Protection v1.0 X-NetMagic-MailScanner: Found to be clean X-NetMagic-MailScanner-From: aalam@tatashare.com X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.13 required=5 tests=[AWL=0.130] X-Spam-Score: 0.13 X-Spam-Level: X-Archive-Number: 200603/331 X-Sequence-Number: 24677 delete my email from the list Regards, -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of ogjunk-pgjedan@yahoo.com Sent: Tuesday, March 21, 2006 8:29 PM To: pgsql-sql@postgresql.org Subject: [SQL] Referential integrity broken (8.0.3), sub-select help 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 ---------------------------(end of broadcast)--------------------------- TIP 6: explain analyze is your friend