Re: [SQL] Primary Key Bugs

From: José Soares <jose(at)sferacarta(dot)com>
To: Tim Perdue <perdue(at)raccoon(dot)com>
Cc: pgsql-sql(at)hub(dot)org
Subject: Re: [SQL] Primary Key Bugs
Date: 1999-04-26 12:44:14
Message-ID: 37245F9D.F0232CC2@sferacarta.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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é

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Nuchanard Chiannilkulchai 1999-04-26 14:57:05 substring
Previous Message Jan Wieck 1999-04-26 07:31:19 Re: [SQL] PL/pgsql questions..