Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL primary keys are auto incrementing odd numbers only - Why?

Tags:

mysql

I developed a backend on a local MySQL server and everything was normal. After I moved it to the production server all of my primary keys in the database are auto incrementing to odd numbers only (it could also be that it is auto incrementing by 2) - So 1,3,5,7,9 ...

I know it is possible to change the auto increment amount, but I didn't do that. So how could I check to see if that was set somehow?

like image 752
Adam Meyer Avatar asked Apr 24 '13 15:04

Adam Meyer


People also ask

Does primary key automatically auto increment?

AUTO INCREMENT Field Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

Does MySQL primary key auto increment?

One of the important tasks while creating a table is setting the Primary Key. The Auto Increment feature allows you to set the MySQL Auto Increment Primary Key field. This automatically generates a sequence of unique numbers whenever a new row of data is inserted into the table.

Why does auto increment jumps by more than the number of rows inserted?

It could have multiple causes: Check if the auto_increment value on the table itself, has the next highest value. Mind that if you have transactions where you INSERT a row and rollback the transaction, that auto_increment value will be gone/skipped.

Can a primary key not be auto increment?

There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value. So you can indeed have an AUTO_INCREMENT column in a table that is not the primary key.


2 Answers

It's likely that the auto_increment_increment was changed: http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_increment

You can check like this:

mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 10    |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.01 sec)
like image 67
Wolph Avatar answered Nov 03 '22 00:11

Wolph


This is probably due to an INSERT statement being sent twice, but with an IGNORE thus causing only odd numbers.

like image 33
Kermit Avatar answered Nov 03 '22 00:11

Kermit