Auto-increment is not resetting in MySQL

后端 未结 8 1451
花落未央
花落未央 2020-12-03 17:53

I am trying to set up a script to generate a particular set of test data into my database, at the beginning of which I want to clear the tables concerned without dropping co

8条回答
  •  自闭症患者
    2020-12-03 18:12

    MySQL does not permit you to decrease the AUTO_INCREMENT value, as specified here: http://dev.mysql.com/doc/refman/5.6/en/alter-table.html

    You cannot reset the counter to a value less than or equal to the value that is currently in use. For both InnoDB and MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column, the value is reset to the current maximum AUTO_INCREMENT column value plus one.

    Even with your constraints, I would try one of the following:

    1. Explicitly insert your identities for your test data. MySQL doesn't have problems with this, unlike some other database engines
    2. Delete and recreate your identity column (or just change it from being an identity), if the constraints aren't on it itself.
    3. Not use an Identity column and use another method (such as a procedure or outside code) to control your Identity. This is really a last resort and I wouldn't generally recommend it...

    Note from OP: It was (1) that was what I needed.

提交回复
热议问题