Auto-increment is not resetting in MySQL

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

    From what I can see about the alter table statement.

    You can reset auto increment value by using the ALTER TABLE statement. The syntax of the ALTER TABLE statement to reset auto increment value is as follows:

    ALTER TABLE table_name AUTO_INCREMENT = value;
    

    You specify the table name after the ALTER TABLE clause and the value which we want to reset to in the expression AUTO_INCREMENT = value.

    Notice that the value must be greater than or equal to the current maximum value of the auto-increment column.

    Which is where your problem lies I suspect. So basically you are left with a couple of options as follows:

    1. TRUNCATE TABLE: which according to our discussion is not a option
    2. DROP and RECREATE the table: A long and painful experience
    3. ALTER auto number column: I have not tried this but you could theoretically alter the primary key column from auto number to a int and then make it a auto number again. Something like:

      ALTER TABLE tblName MODIFY COLUMN pkKeyColumn  BIGINT NOT NULL;
      ALTER TABLE tblName MODIFY COLUMN pkKeyColumn  BIGINT AUTONUMBER NOT NULL;
      

    Hope these help a little.

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

    ALTER TABLE tbl DROP COLUMN id;
    ALTER TABLE tbl ADD id INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id);
    in your phpMyAdmin

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