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;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…