简体   繁体   中英

MySQL auto increment column

I see a weird behavior where my auto-increment column number is only increasing in a step of 2 instead of 1. So I end up with row ids as 1, 3, 5, 7. I use MySQL 5.6 + InnoDB as the engine. Any idea why this weirdness?

mysql> select version();
+-----------------+
| version()       |
+-----------------+
| 5.6.20-68.0-log |
+-----------------+
1 row in set (0.00 sec)

mysql> show create table temp_table;
| superset_version | CREATE TABLE `temp_table` (
  `_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'comm1',
  `name` varchar(100) NOT NULL COMMENT 'comm2',
  `start_time` bigint(20) NOT NULL COMMENT 'comm3',
  `updated_at` bigint(20) NOT NULL COMMENT 'comm4',
  `status` varchar(50) NOT NULL COMMENT 'comm5',
  PRIMARY KEY (`_id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 |

Notice that insert incremented the _id column by a difference of 2.

mysql> insert into superset_version(name, start_time, updated_at, status) value("TEMP ROW", -1, -1, "erro");
    Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from superset_version order by _id desc limit 3;
+-----+----------+------------+------------+--------+
| _id | name     | start_time | updated_at | status |
+-----+----------+------------+------------+--------+
|  33 | TEMP ROW |         -1 |         -1 | erro   |
|  31 | TEMP ROW |         -1 |         -1 | erro   |
|  29 | TEMP ROW |         -1 |         -1 | erro   |
+-----+----------+------------+------------+--------+
3 rows in set (0.00 sec)

for double check you could try:

SELECT AUTO_INCREMENT
From `information_schema`.`TABLES`
WHERE   TABLE_NAME = '<<YOUR TABLE NAME HERE>>' AND
TABLE_SCHEMA = '<< YOUR DATABASE NAME HERE >>'

if the step is really equals 2, it's possible to replace it.

or one another way: Check values in: mysql.cnf / ini:

auto-increment-increment
auto-increment-offset

auto_increment_increment设置很可能设置为2,因此mysql将自动递增数增加2。使用show varibles like ...命令检查设置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM