title: InnoDB: custom auto-increment using insert select. Can there be duplicate-key error?
body: <p>I have a table like: <code>idx (PK)   clmn_1</code></p>
  
  <p>Both are INTs. <code>idx</code> is not 
  defined as auto-increment, but I am trying to simulate it. To
  insert into this table, I am using:</p>
  
  <pre><code>"INSERT INTO  my_tbl (idx, clmn_1)   \
   SELECT IFNULL(MAX(idx), 0) + 1, %s  \
   FROM my_tbl", val_clmn_1
  </code></pre>
  
  <p>Now, this works. The query that I have is about atomicity. Since I read and then insert to the same table, when multiple inserts happen simultaneous can there potentially be a
  duplicate-key error?</p>
  
  <p>And, how can I test it myself?</p>
  
  <p>I am using Percona XtraDB server 5.5.</p>