#(0)自增id保存在哪里
在myisam引擎中, 保存在数据文件中
在innodb引擎中, 包括5.7之前的版本, 保存在内存中, 重启会根据max(id)+1重新计算; 8.0之后的版本自增值保存在redo log中
#(1)自增id不连续原因
1.唯一键冲突
2.事务回滚
3.insert..select语句批量申请自增id
#(2)唯一键冲突
CREATE TABLE `t` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c` int(11) DEFAULT NULL,
`d` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `c` (`c`)
) ENGINE=InnoDB;
insert into t value(null,1,1);
insert into t value(null,1,1);
insert into t value(null,2,2);
select * from t;
#(3)事务回滚
insert into t values(null,1,1);
begin;
insert into t values(null,2,2);
rollback;
insert into t values(null,2,2);
#(4)insert..select语句批量申请自增id
truncate table t;
insert into t values(null, 1,1);
insert into t values(null, 2,2);
insert into t values(null, 3,3);
insert into t values(null, 4,4);
create table t2 like t;
insert into t2(c,d) select c,d from t;
insert into t2 values(null, 5,5);
select * from t2;