cleaning up db of redundant data

前端 未结 5 1653
时光说笑
时光说笑 2020-12-07 04:54
locid   country city
39409   US  Aaronsburg
128426  US  Aaronsburg
340356  US  Aaronsburg
429373  US  Aaronsburg
422717  US  Abbeville
431344  US  Abbeville
433062           


        
5条回答
  •  旧巷少年郎
    2020-12-07 05:13

    Add unique index on table location so that no duplicate records will get inserted

    ALTER IGNORE TABLE location ADD UNIQUE KEY ix1(country, city);
    

    This will automatically remove duplicate records from the table and for future insert queries you need to use INSERT IGNORE clause to avoid getting duplicate errors.

    but as suggested by @AD7six in comments, it might not work on MySQL versions 5.1.41,5.5.1-m2, 6.0: see bug here

    or alternate safe way to remove duplicates using DELETE query:

    DELETE a
    FROM location a
         LEFT JOIN (
                    SELECT locid
                    FROM location
                    GROUP BY country, city
                   )b
                   ON a.locid = b.locid
    WHERE b.locid IS NULL;
    

    to resettle values of auto_increment column locid, you can just drop the primary key on locid and recreate it:

    ALTER TABLE location DROP column locid;
    ALTER TABLE location 
          ADD COLUMN locid INT unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
    

    or alternative way to resettle values of locid using UPDATE query:

    SET var_locid = 0;
    
    UPDATE location
    SET locid = (@var_locid := @var_locid + 1)
    ORDER BY locid ASC;
    

提交回复
热议问题