简体   繁体   中英

INSERT changes auto_increment, but UPDATE not changes auto_increment?

We have a table:

id int(11) auto_increment
name varchar(255)

auto_increment equals 1. Insert row:

INSERT INTO `projects` ( `id` , `name`) VALUES ('350',  'project one');

Now auto_increment equals 351. Update row:

UPDATE `projects` SET `id` = '351' WHERE `id` = 350 LIMIT 1 ;

auto_increment still equals 351. And we get error if try to insert a row:

#1062 - Duplicate entry '351' for key 1 

How we can see INSERT changes auto_increment and UPDATE not changes auto_increment .

My goal is to update row and set id greater then auto_increment .

How to do it?

First of all why are you trying to set the auto increment value? Just let it do its job (clue - it is automatic).

So the best solution is when you insert a row let the auto increment chose the appropriate value for you and let that value be an invariant for that rows life time.

Otherwise just remove the auto_increment bit from the table definition and implement an appropriate system yourself.

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