+ -
当前位置:首页 → 问答吧 → 关于MySQL auto_increment的探讨

关于MySQL auto_increment的探讨

时间:2011-09-29

来源:互联网

SQL code

create table student(stu_id int primary key auto_increment,
course_id int unique);
insert into student values(null,1);
insert into student values(null,2);
select * from student;
insert into student values(null,2);
insert into student values(null,3);
select * from student;
insert into student values(null,4);
select * from student;


其中第二条insert into student values(null,2);
这条语句是不能正确执行的
但执行之后stu_id竟然自动增长了,这是为什么呢?

作者: liuanswer   发布时间: 2011-09-29

即使由于某些原因导致插入失败,但auto_increment 还是会被计数的。

MYSQL是先通过内部函数生成你的auto_increment值,然后插入,插入的时候产生unique键冲突,导致插入失败。

作者: ACMAIN_CHM   发布时间: 2011-09-29

相关阅读 更多