Mysql auto increment jumps when insert-select

巧了我就是萌 提交于 2019-11-30 08:44:16

You can change innodb_autoinc_lock_mode=0 (“traditional” lock mode) from my.ini to avoid skipping values in primary key in some cases. See the manual mysql manual for innodb auto increment handling for more details.

As per manual 'The traditional lock mode option is provided for backward compatibility, performance testing, and working around issues with “mixed-mode inserts”, due to possible differences in semantics'.

'In this lock mode, all “INSERT-like” statements obtain a special table-level AUTO-INC lock for inserts into tables with AUTO_INCREMENT columns. This lock is normally held to the end of the statement (not to the end of the transaction) to ensure that auto-increment values are assigned in a predictable and repeatable order for a given sequence of INSERT statements, and to ensure that auto-increment values assigned by any given statement are consecutive'.

Another thing to check is the value of the auto_increment_increment config variable. It's 1 by default, but you may have changed this. it is very uncommon to set it to something higher than 1 or 2, but possible.

Or if it dont work in your case you can also use query like the answer of AnandPhadke in this same page like :

ALTER TABLE tablename AUTO_INCREMENT = 1;
INSERT INTO tablename (col1,col2,col3) SELECT col1,col2,col3 FROM tablename;

Put in your My.cnf:

innodb_autoinc_lock_mode=0
AnandPhadke

You can reset the auto_increment value to 1 every time before inserting values into your table:

ALTER TABLE `test` AUTO_INCREMENT = 1;
INSERT INTO test (a,b) SELECT a,b FROM test_current;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!