The value to be used by the following INSERT or ALTER TABLE statement when inserting an AUTO_INCREMENT value. This is mainly used with the binary log.

insert_id 是mysql session变量,设置该变量会影响下一条INSERT 或 ALTER TABLE 操作的 AUTO_INCREMENT 值。

比如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
CREATE TABLE `btest` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `age` int(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

mysql> set session insert_id=0;

mysql> insert into btest(age, name) values(100,'zhaohui');

此时插入的数据id将为0,而非1;

参考