小弟在一个数据表里用到自增长作为id,开始10个数据是好,每次增1,但是从11开始,每次增长变成了2。即在数据库表里是这样的:
id
1
2
3
...
8
9
10
11
13
15
17
19
十分诡异,开始以为是我jdbc代码问题,后来发现不是,我用insert语句手动插入一条数据也是得到奇数的 id !!
用desc看表结构,该字段是int not null primary key auto_increment,也看不出什么不对的地方。
不知哪位高人也遇到过此类问题,不吝赐教。
(后来发现另一张用到自增长id的表也是1、2、3、4、5、6、7、8、9、11、13、15、17、19……只是没出现10,其它都一样;
肯定是有问题,就是找不出来,在搜索引擎里搜索都不知道打什么关键字……)
try:
set @@global.auto_increment_increment = 1;
set @@auto_increment_increment =1;
你肯定删过某一条, 在14的时候 肯定被删过,在添加的话 就不是14了,就是15了。
show create table tableName; --看看是否是修改表结构了?
show variables like '%increment%';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| [color=red]auto_increment_increment | 1 |[/color]
[color=red]| auto_increment_offset | 1 |[/color]
| div_precision_increment | 4 |
| innodb_autoextend_increment | 8 |
| ndb_autoincrement_prefetch_sz | 32 |
+-------------------------------+-------+
5 rows in set (0.01 sec)
看看上面两个红色的field的value.
如果想永久性的改回为1,在my.cnf or my.ini中查找这个field,然后修改,然后restart mysql.
如果只想在当前测试一下当前的Session(在Console下的一个连接),可以直接修改
set auto_increment_increment = 1
设置方法:
[code="cmd"]SET @@auto_increment_increment=1;[/code]
关于auto_increment_increment,auto_increment_offset 的详细说明和[color=red]具体的例子[/color]可以参考mysql的document.
[url=http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_increment]http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_increment[/url]
另外第五行的那个属性是说明insert的快慢。
E文如下说:
[quote]•ndb_autoincrement_prefetch_sz
mysql Version Introduced 4.1.8
Determines the probability of gaps in an autoincremented column. Set it to 1 to minimize this. Setting it to a high value for optimization — makes inserts faster, but decreases the likelihood that consecutive autoincrement numbers will be used in a batch of inserts. Default value: 32. Minimum value: 1.
[/quote]
补充:mysql技术还是在发展中,免不了存在bug。
关于Replication and AUTO_INCREMENT ,AUTO_INCREMENT的bug参考如下:
在使用AUTO_INCREMENT时,需要注意这个问题:
mysql表的种类不同,使用也不同。
[quote]对于AUTO_INCREMENT类型的字段,InnoDB中必须包含只有该字段的索引,但是在MyISAM表中,可以和其他字段一起建立联合索引。[/quote]
用了set auto_increment_increment = 1,然后,你在mysql的mysql>
insert into table table name values.....
可能这里第一次没有成功,
多执行几次insert,就看到效果了。
既然你熟悉jdbc,就应该知道Connection. 在Console里面设置,是只对该Connection有用。
同样,你也可以用相关的statement执行: set auto_increment_increment = 1, 然后在同一个事务中执行其他的语句.