pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Aftab Alam <aalam@tatashare.com>
To: ogjunk-pgjedan@yahoo.com
To: pgsql-sql@postgresql.org
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> (raw)
In-Reply-To: <20060321145839.68211.qmail@web50301.mail.yahoo.com>

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




view thread (7+ messages)  latest in thread

Message-ID: <001a01c64cfa$987c5d60$ec1010ac@aftabn463>
Permalink:  ../001a01c64cfa$987c5d60$ec1010ac@aftabn463/
Also on:    postgresql.org/message-id/001a01c64cfa$987c5d60$ec1010ac@aftabn463

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: aalam@tatashare.com, ogjunk-pgjedan@yahoo.com
  Subject: Re: Referential integrity broken (8.0.3), sub-select help
  In-Reply-To: <001a01c64cfa$987c5d60$ec1010ac@aftabn463>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox