Let's say that from all the duplicates in contact table, we'll leave only those with the lowest ROWIDs.
So first, let's switch the records in address table that belong to duplicates, to the ones that will remain in contact table:
Code:
UPDATE addrtbl SET contact_uid =
(SELECT contact_uid FROM contacttbl c1
WHERE name = (SELECT name FROM contacttbl c2
WHERE c2.contact_uid = addrtbl.contact_uid)
AND NOT EXISTS (SELECT null FROM contacttbl c3
WHERE c3.rowid < c1.rowid)
)
WHERE EXISTS (SELECT null FROM contacttbl c4
WHERE contact_uid = addrtbl.contact_uid
AND EXISTS (SELECT null FROM contacttbl c5
WHERE c4.name = c5.name
AND c5.rowid > c4.rowid));
And finaly, let's delete the duplicates from the contact table:
Code:
DELETE FROM contacttbl c1
WHERE EXISTS (SELECT null FROM contacttbl c2
WHERE c2.name = c1.name
AND c2.rowid < c1.rowid);