title: sql server theory about updating a table
body: <p>I have an asp.net web app where a user can update a table in a database. I would like to know the proper method of doing this. As you can see in the image below, the red shaded area is the data that I have in common that I do <strong><em>not</em></strong> need to update. </p>
  
  <p><img src="https://i.stack.imgur.com/2IXVn.png" alt="enter image description here"></p>
  
  <p>Here are the steps that I need to take:</p>
  
  <ol>
  <li>Don't update data if it exists</li>
  <li>Delete data that is not in the user update</li>
  <li>Add data from the user update that is not already in the table</li>
  </ol>
  
  <p>I need to audit this table also, so I will have a trigger on it. I would like to know in theory what is the best way to do this kind of update to a table?</p>
  
  <p>thank you to Aaron who introduced the concept of merge.</p>
  
  <p>Here is what the table data looks like:</p>
  
  <pre><code>foreignkey  model  primarykey
  1            AA       1
  1            AA1      2
  1            AA3      3
  23           B        4
  22           C        5
  </code></pre>
  
  <p>The user will be adding data that looks like this:</p>
  
  <pre><code>foreignkey  model  primarykey
  1            A1       1
  1            AA1      2
  1            AA3      3
  22           C        5
  </code></pre>
  
  <p>As you can see, the first row was updated, and the second to last row was deleted entirely. Can you please help me with this merge statement?</p>