mysql delete,autoincrement

后端 未结 4 1573
攒了一身酷
攒了一身酷 2020-12-11 07:25

I have a table in MySQL using InnoDB and a column is there with the name \"id\".

So my problem is that whenever I delete the last row from the table and then insert

4条回答
  •  情歌与酒
    2020-12-11 07:50

    You should stop fighting this, even using SELECT max(id) will not fix this properly when using transactional database engine like Innodb.

    Why you might ask? Imagine that you have 2 transactions, A and B, that started almost at the same time, both doing INSERT. First transaction A needs new row id, and it will use it from invisible sequence associated with this table (known as AUTOINCREMENT value), say 21. Another transaction B will use another successive value (say 22) - so far so good.

    But, what if transaction A rolls back? Value 21 cannot be reused, and 22 is already committed. And what if there were 10 such transactions?

    And max(id) can assign the same value to both A and B, so this is not valid as well.

提交回复
热议问题