sqlduplicates

How can I delete one of two perfectly identical rows?


I am cleaning out a database table without a primary key (I know, I know, what were they thinking?). I cannot add a primary key, because there is a duplicate in the column that would become the key. The duplicate value comes from one of two rows that are in all respects identical. I can't delete the row via a GUI (in this case MySQL Workbench, but I'm looking for a database agnostic approach) because it refuses to perform tasks on tables without primary keys (or at least a UQ NN column), and I cannot add a primary key, because there is a duplicate in the column that would become the key. The duplicate value comes from one...

How can I delete one of the twins?


Solution

  • One option to solve your problem is to create a new table with the same schema, and then do:

    INSERT INTO new_table (SELECT DISTINCT * FROM old_table)
    

    and then just rename the tables.

    You will of course need approximately the same amount of space as your table requires spare on your disk to do this!

    It's not efficient, but it's incredibly simple.