0

I have this table:

CREATE TABLE `test` (`id` INT(11) NOT NULL AUTO_INCREMENT , `name` VARCHAR(100) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;

| id | name |
| -- | ---- |

In which I insert the following two rows:

INSERT INTO `test`(`name`) VALUES ('Oscar'), ('Alba')

| id | name  |
| -- | ----- |
| 1  | Oscar |
| 2  | Alba  |

Now, I want to update the existing rows and create new ones:

INSERT INTO `test` (`id`, `name`) VALUES (1, 'Oscar'), (2, 'Nadia'), ('', 'Pedro') ON DUPLICATE KEY UPDATE `id` = VALUES (`id`), `name` = VALUES (`name`)

| id | name  |
| -- | ----- |
| 1  | Oscar |
| 2  | Nadia |
| 3  | Pedro |

If I update existing rows again and create new ones, this is what happens:

INSERT INTO `test` (`id`, `name`) VALUES (1, 'Oscar'), (2, 'Nadia'), (3, 'Lucas'), ('', 'Maria'), ('', 'Sergio') ON DUPLICATE KEY UPDATE `id` = VALUES (`id`), `name` = VALUES (`name`)

| id | name   |
| -- | ------ |
| 1  | Oscar  |
| 2  | Nadia  |
| 3  | Lucas  |
| 6  | Maria  |
| 7  | Sergio |

There is a jump in the auto increment id. I don't know why this happens. I would like to know if there is any way to avoid this.

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.