title: Updating a many-to-many connector table all at once
body: <p>Say I have two tables:</p>
  
  <pre><code>articles
  categories
  </code></pre>
  
  <p>There's a many to many table that connects them.</p>
  
  <pre><code>CREATE TABLE cat2art (
    article_id INT,
    category_id INT
  );
  </code></pre>
  
  <p>For a specific article, I have a 'new list' of category id's, and we need to update the cat2art table with these.</p>
  
  <p>Some categories got removed, some got added and some stayed where they were. What is the most effective way to update this table?</p>
  
  <p>I could naively delete all records with the specified article_id and simply add them again. However, if I were to record a date in that same table that tracks when an article was linked to a category, that information will now be destroyed.</p>
  
  <p>I'm looking for a great pattern that easily solves this issue. This question is specifically for PHP and MySQL, but answers in other languages are also fine provided they are applicable to PHP+MySQL as well.</p>