首先我的建表sql是这样的:
create table if not exists `test`(
`id` bigint(20) not null AUTO_INCREMENT COMMENT 'id',
`userId` bigint(20) not null DEFAULT 0 COMMENT '学生id',
`reportId` bigint(20) not null DEFAULT 0 COMMENT '',
`comment` varchar(256) not null DEFAULT '' COMMENT '文字评价',
PRIMARY KEY (`id`),
UNIQUE KEY `index_userId_reportId` (`userId`,`reportId`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='';
然后我插入语句:
insert into test(userId,reportId) values(1,1);
成功插入一条!
然后我搞事情:
insert into test(userId,reportId) values(1,1);
聪明的你肯定看出来了,这个是会造成唯一索引冲突的!
这时候我看看表里面的数据:
嗯,没错。
然后我再插入:
insert into test(userId,reportId) values(2,1);
这时候没错,插入成功,再看表中的数据:
咦,id跳过2直接到3了!
我猜应该是在发生唯一索引冲突的时候,id计数器没有回退的原因。
以上!谢谢。
【工作中写单元测试的时候发现的,没有看mysql源码,有大神懂的可以留言其中的细节,谢谢啊】