Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

azure - MySQL Auto increment primary key increases by 10

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'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...