USE vn; DELETE tc FROM vn.ticketCollection tc JOIN ( SELECT id, COUNT(ticketFk) counter FROM vn.ticketCollection GROUP BY ticketFk HAVING counter > 1 ) sub ON sub.id = tc.id; ALTER TABLE vn.ticketCollection DROP KEY ticketCollection_UN; ALTER TABLE vn.ticketCollection ADD CONSTRAINT ticketCollection_UN UNIQUE KEY (ticketFk);