Why INSERT IGNORE increments the auto_increment primary key?

前端 未结 4 1235
挽巷
挽巷 2020-12-05 02:50

I wrote a java program that accesses a MySQL innodb database.

Whenever an INSERT IGNORE statement encounters a duplicate entry the Auto Increment primary key is inc

4条回答
  •  盖世英雄少女心
    2020-12-05 03:39

    I think this behaviour is reasonable. The auto-increment should not be relied upon to give sequences that don't have gaps.

    For example, a rolled back transaction still consumes IDs:

     INSERT INTO t (normalcol, uniquecol) VALUES 
       ('hello','uni1'),
       ('hello','uni2'),
       ('hello','uni1');
    

    Generates a unique key violation obviously, and inserts no rows into the database (assuming transactional engine here). However, it may consume up to 3 auto-inc values without inserting anything.

提交回复
热议问题