I want to keep my m:n table in sync with another mysql database table of another system.
Lets assume an employee can work in n departments and a department can have n employees:
Table Department(id(pk), name)
Table Employee(id(pk), name)
Table employee_department(employee_id(fk), department_id(fk))
For all other tables I used "insert into...on duplicate key" which is working great as Mysql is updating found items with new values. If I use this statement on my employee_department table its inserting duplicate values(sure because those are fks only). I thought about changing the fks to pks so that I have a composite key. But than mysql is not inserting again but not removing deleted references from the other system.
What do I need to change that the complete tables keeps in sync?
You do not have to remove current foreign keys, just add primary or unique composite key:
or
However, keep in mind that you still have to manually remove the relationships that were deleted in original system that have their related records intact.
2 reliable methods would be either to do a complete database wipe/dump or use replication