title: MySQL REPLACE in an auto incremented row
body: <p>Let say I have a MySQL table which contains three columns: <code>id</code>, <code>a</code> and <code>b</code> and the column named <code>id</code> is an <code>AUTO INCREMENT</code> field. If I pass a query like the following to MySQL, it will works fine:</p>
  
  <pre><code>REPLACE INTO `table` (`id`, `a`, `b`) VALUES (1, 'A', 'B')
  </code></pre>
  
  <p>But if I skip the field <code>id</code> it will no longer works, which is expected.</p>
  
  <p>I want to know if there is a way to <code>ignore</code> some fields in the <code>REPLACE</code> query. So the above query could be something like this:</p>
  
  <pre><code>REPLACE INTO `table` (`a`, `b`) VALUES ('A', 'B')
  </code></pre>
  
  <p><strong>Why do I need such a thing?</strong></p>
  
  <p>Sometimes I need to check a database with a <code>SELECT</code> query to see if a row exists or not. If it is exists then I need to <code>UPDATE</code> the existing row, otherwise I need to <code>INSERT</code> a new row. I'm wondering if I could achieve a similar result (but not same) with a single <code>REPLACE</code> query.</p>
  
  <p>Why it couldn't be the <em>same</em> result? Simply because <code>REPLACE</code> will <code>DELETE</code> the existing row and will <code>INSERT</code> a new row, which will <em>lose</em> the current <code>primary key</code> and will increase the <em>auto incremented</em> values. In contrast, in an <code>UPDATE</code> query, <code>primary key</code> and the <code>AI</code> fields will be untouched.</p>
  
  <p>MySQL <a href="http://dev.mysql.com/doc/refman/5.0/en/replace.html" rel="noreferrer">REPLACE</a>.</p>