I'm trying to figure out the best way of updating the linking table in a many-to-many relationship.
I have two entities: Artist and Skill.
The relationship between these two are many-to-many, given that an artist has 1 or 1+ skills and a skill can be possessed by 1 or 1+ artists.
What I would like to ask is the best practice of updating the records in the linking table.
Example: Assume an artist has the below skills artist_id: 1 skill_id: 1, 2, 3, 4
I'd like to edit the artist to have the below skills after a while. artist_id: 1 skill_id: 2, 3, 5, 9
My current practice of updating is to delete all the old records and then insert the new records in to the linking table.
I'm thinking if the best practice is actually compare two arrays ( select old records from the linking table and compare with the new ones) and then decide what to be added and what to be deleted.
Example: From the above case, decide the difference between [1, 2, 3, 4] and [2, 3, 5, 9]. That being said [1, 4] will need to be removed from the linking table and add [5, 9] to the linking table.
I'd like to know what's a better way to deal with this, thank you.
I'm using Node.js and MySQL. I've tried wiping all the old records first and then add the new records. I don't think this is the best practice so would like to know.