pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: José Soares <jose@sferacarta.com>
To: Tim Perdue <perdue@raccoon.com>
Cc: pgsql-sql@hub.org
Subject: Re: [SQL] Primary Key Bugs
Date: Mon, 26 Apr 1999 14:44:14 +0200
Message-ID: <37245F9D.F0232CC2@sferacarta.com> (raw)
References: <000c01be8f45$6c334f90$0b8c5aa5@timnt.weather.net>



Tim Perdue ha scritto:

> Somehow I have records in my database with duplicate primary keys. Because
> of this, I'm not able to update a lot of records, because Postgres then
> complains that I'm trying to insert a duplicate primary key.
>
> Can anyone suggest a way to select the duplicate ids out of the table so I
> can change them?
>
> I can't really do a SELECT DISTINCT on a 3GB database table or my machine
> would likely go down.
>
> Basically, I just want a query like this:
>
> select * into tbl_tmp from tbl_mail where mailid is duplicated
>
> 8-)
>
> Any suggestions?
>
> Tim Perdue
> PHPBuilder.com / GotoCity.com / Geocrawler.com

The SQL expression to retrieve duplicate rows is:

select *  from tbl_mail where mailid in (
select mailid from tbl_mail group by mailid having count(mailid) > 1
);

But currently PostgreSQL have problems with having (see TODO):
        * subqueries containing HAVING return incorrect results

Therefore you have to query the tbl_mail in two times.

1) select mailid from tbl_mail group by mailid having count(mailid) > 1
2) select *  from tbl_mail where mailid = (replace with values returned at
point 1)

José






view thread (2+ messages)

Message-ID: <37245F9D.F0232CC2@sferacarta.com>
Permalink:  ../37245F9D.F0232CC2@sferacarta.com/
Also on:    postgresql.org/message-id/37245F9D.F0232CC2@sferacarta.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: jose@sferacarta.com, perdue@raccoon.com, pgsql-sql@hub.org
  Subject: Re: [SQL] Primary Key Bugs
  In-Reply-To: <37245F9D.F0232CC2@sferacarta.com>
  References: <000c01be8f45$6c334f90$0b8c5aa5@timnt.weather.net> <37245F9D.F0232CC2@sferacarta.com>

* 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