Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Auto increment primary key increases by 10

Tags:

mysql

azure

On azure I created a new MySQL Database instance. In this db I create a table using this script:

CREATE TABLE ROLES(
  ID INTEGER PRIMARY KEY AUTO_INCREMENT,
  ROLE_NAME VARCHAR(30) NOT NULL
);

Then I insert values using this script:

INSERT INTO `beezzy`.`roles` (`ROLE_NAME`) VALUES ('admin');
INSERT INTO `beezzy`.`roles` (`ROLE_NAME`) VALUES ('owner');
INSERT INTO `beezzy`.`roles` (`ROLE_NAME`) VALUES ('consultant');

after execution table contains such rows:

enter image description here

Why DB generates IDs like '11' and '21'? I run the same script on my local machine and everything works fine. IDs was '1', '2', '3'

like image 238
Oleh Kurpiak Avatar asked Mar 10 '16 06:03

Oleh Kurpiak


People also ask

Does primary key auto increment MySQL?

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.

What happens if MySQL auto increment reaches limit?

When the AUTO_INCREMENT column reaches the upper limit of data type then the subsequent effort to generate the sequence number fails. That is why it is advised to use a large enough integer data type for the AUTO_INCREMENT column to hold the maximum sequence value required by us.

Should I use auto increment for primary key?

The advantages to using numeric, auto incremented primary keys are numerous, but the most impactful benefits are faster speed when performing queries and data-independence when searching through thousands of records which might contain frequently altered data elsewhere in the table.

Why does auto increment jump 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.


2 Answers

Please run the following query.

SELECT @@auto_increment_increment

If the value is more than 1 then set it to 1 by the following query:

SET @@auto_increment_increment=1;

Note: This change is visible for the current connection only.

EDIT:

In order to set it globally so that other connections can also see the change you need to set it for global and session too.

SET @@GLOBAL.auto_increment_increment = 1;

SET @@SESSION.auto_increment_increment = 1;

So other connections can see this change now.

More:

This value will be reset if you restart your MySQL server. In order to make this change permanent you need to write this variable under [mysqld] secion in your my.cnf [for linux] or my.ini [for windows] file.

[mysqld]
auto-increment-increment = 1
like image 62
1000111 Avatar answered Sep 21 '22 21:09

1000111


Your autoincrement is probably 10, however this is probably by design. Azure uses ClearDB which uses an autoincrement of 10 with a reason: namely replication.

When I use auto_increment keys (or sequences) in my database, they increment by 10 with varying offsets. Why?

ClearDB uses circular replication to provide master-master MySQL support. As such, certain things such as auto_increment keys (or sequences) must be configured in order for one master not to use the same key as the other, in all cases. We do this by configuring MySQL to skip certain keys, and by enforcing MySQL to use a specific offset for each key used. The reason why we use a value of 10 instead of 2 is for future development.

You should not change the autoincrement value.

cleardb faq

like image 29
Peter Avatar answered Sep 23 '22 21:09

Peter