Unix Technical Forum

Deleting duplicate entries from database table

This is a discussion on Deleting duplicate entries from database table within the MySQL forums, part of the Database Server Software category; --> NOTE: The same message is also posted at comp.lang.php, mailing.database.mysql, mysql on July 7. The scenario is as: 1. ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 08:55 AM
Manish
 
Posts: n/a
Default Deleting duplicate entries from database table


NOTE: The same message is also posted at comp.lang.php,
mailing.database.mysql, mysql on July 7.


The scenario is as:
1. A table is created
2. No Primary Key, Unique Key, Validation process.
3. Various duplicate entries gor inserted in the table (all fields
contain same data)

May be programming logic bug/not handled the case.

In the edit process, nothing is edited, (all data are same), and
instead of updating the record, new row get inserted.

4. Now it is desired that all the entries with duplicate entries gets
deleted abd only 1 record (within duplicate entry) remains in the
table.

As in shown in example.

Suppose a table "address" contains the following records

-------------------------------------------------------
| name | address | phone |
-------------------------------------------------------
| mr x | 8th lane | 124364 |
| mr x | 6th lane | 435783 |
| mrs x | 6th lane | 435783 |
| mr x | 8th lane | 124364 |
-------------------------------------------------------

>> Execute single query (MySQL Version: No Restriction), with sub-query or some other method
>> After executing the query


-------------------------------------------------------
| name | address | phone |
-------------------------------------------------------
| mr x | 8th lane | 124364 |
| mr x | 6th lane | 435783 |
| mrs x | 6th lane | 435783 |
-------------------------------------------------------

Here instead of deleting both duplicate entry

| mr x | 8th lane | 124364 |

only one gets deleted (out of two) and no duplicate entries are there
in table.

Hope it will clarify my question.

Thanks.

Manish

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 08:55 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Deleting duplicate entries from database table

Manish wrote:
> NOTE: The same message is also posted at comp.lang.php,
> mailing.database.mysql, mysql on July 7.
>
>
> The scenario is as:
> 1. A table is created
> 2. No Primary Key, Unique Key, Validation process.
> 3. Various duplicate entries gor inserted in the table (all fields
> contain same data)
>
> May be programming logic bug/not handled the case.
>
> In the edit process, nothing is edited, (all data are same), and
> instead of updating the record, new row get inserted.
>
> 4. Now it is desired that all the entries with duplicate entries gets
> deleted abd only 1 record (within duplicate entry) remains in the
> table.
>
> As in shown in example.
>
> Suppose a table "address" contains the following records
>
> -------------------------------------------------------
> | name | address | phone |
> -------------------------------------------------------
> | mr x | 8th lane | 124364 |
> | mr x | 6th lane | 435783 |
> | mrs x | 6th lane | 435783 |
> | mr x | 8th lane | 124364 |
> -------------------------------------------------------
>
>
>>>Execute single query (MySQL Version: No Restriction), with sub-query or some other method
>>>After executing the query

>
>
> -------------------------------------------------------
> | name | address | phone |
> -------------------------------------------------------
> | mr x | 8th lane | 124364 |
> | mr x | 6th lane | 435783 |
> | mrs x | 6th lane | 435783 |
> -------------------------------------------------------
>
> Here instead of deleting both duplicate entry
>
> | mr x | 8th lane | 124364 |
>
> only one gets deleted (out of two) and no duplicate entries are there
> in table.
>
> Hope it will clarify my question.
>
> Thanks.
>
> Manish
>


Manish,

Is this a homework assignment? It sure looks like one.

I would recommend a primary key for the table. It really helps prevent
this from happening in the first place. And if it does happen it's
easier to delete.

For a single statement, though, the only way I can think of is to use a
delete with LIMIT 1 and a subselect checking for count > 1. But it
would be a bit complicated.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:52 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com