Categorized | News

Avoiding Duplicate Rows in MySQL

Posted on 12 June 2002 by Demian Turner

Add a field to your table called md5hash or something similar. Make this field unique. Then when adding a new record, concatenate the other fields into a string and md5 it. Then write the md5 hash into the md5hash field.

This way if the record already exists MySQL will ignore the duplicate entry.

Bookmark and Share

3 Comments For This Post

  1. Anonymous Says:

    i have been searching the net for a simple way to do this. thank you so


  2. Anonymous Says:

    because so far I did a SELECT statement first to check for duplicate entries, which creates almost the double work for the DB!
    Now I can skip that.

  3. Anonymous Says:

    The easy way is to do a
    REPLACE into table (f1, f2, f3) Values(1,2,3)
    That way if there is a duplicate, It will replace, otherwise it behaves like a INSERT.

    -Byron Whitlock

Leave a Reply



Demian Turner's currently-reading book recommendations, reviews, favorite quotes, book clubs, book trivia, book lists



PHPkitchen recommends you also check out the following sites :

Accounting for Small Businesses

FreeAgent sign-up