Auto-increment is not resetting in MySQL

后端 未结 8 1430
花落未央
花落未央 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:05

    In non-problematic circumstances you can do

    ALTER TABLE tbl AUTO_INCREMENT = 0;
    

    which brings auto_increment value to the lowest allowed at the time.

    0 讨论(0)
  • 2020-12-03 18:09

    I've run into this problem when I've deleted middle rows from my table. My answer would be to INSERT NEW DATA TO NOT EXISTING ID. I expect that my answer still be usefull even if it's PHP not MYSQL.

    1. First fetch your data.
    2. if found not existing row Insert values and exit;
    3. else if not found in whole loop then do insertion for default value;

      $rez = mysql_query("SELECT * FROM users");
      $exists = 1;
      while($row = mysql_fetch_array($rez)){
              if ( $exists != $row{'id'} ){
                     echo "found not existing id: ".$exists;
                     INSERT INTO users VALUES($exists,.. ,..); 
                     exit;
               } $exists += 1;
       }
       INSERT INTO users VALUES(NULL,.. ,..);  ##auto_inc column converts NULL to latest
      

    I HOPE it will help somehow.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-03 18:13

    I'm sure this has been long answered but when i need to truncate and can't I just do a set foreign_key_checks = 0 then run my truncate and then set foreign_key_checks = 1.

    0 讨论(0)
  • 2020-12-03 18:17

    ALTER TABLE table_name AUTO_INCREMENT = value;
    This worked for me, I had to set it to the last record in my database while going through the operations panel never worked for me.

    0 讨论(0)
  • 2020-12-03 18:20

    Can you not drop the relevant, auto increment column and recreate it? Example follows:

    ;;assuming your column is called id and your table is tbl
    ALTER TABLE tbl DROP COLUMN id;
    ALTER TABLE tbl ADD COLUMN id BIGINT UNSIGNED DEFAULT 1 PRIMARY KEY FIRST;
    

    This should work, but I don't use MySQL, just going off the docs. If you need further help, leave a comment and I'll do my best to help out.

    0 讨论(0)
提交回复
热议问题