Click here to Skip to main content
15,888,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
At each Directory change, I store the filenames in a table.
Reset table and set AUTO_INCRTEMENT = 0

The first time the counter is = 0
id = 0
id = 1
id = 2
...

The following times I empty the table and set AUTO_INCREMENT = 0 the value of the first id is always = 3

id = 3
id = 4
id = 5
....

tb_temporary - InnoDb - utf8mb4 - utf8mb4_900_ai_ci
id = INT(11) - PK - NN - AI

What I have tried:

int Contatore = 0;
            Query = "ALTER TABLE tb_temporary AUTO_INCREMENT = @contatore";
            gesdb.AlterCounter(Query, conn, Contatore); 


public void AlterCounter(string Query, MySqlConnection MyConn2, int contatore)
       {
           try
           {
               MyConn2 = Connection;

               if (MyConn2.State != ConnectionState.Open)
               {
                   MyConn2.Open();
               }

               MySqlCommand cmd = new MySqlCommand(Query, MyConn2); //assegna MySqlCommand

               cmd.Parameters.AddWithValue("@contatore", contatore);

               //trs = MyConn2.BeginTransaction();
               //cmd.Transaction = trs;
               try
               {
                   cmd.ExecuteNonQuery();
                   //trs.Commit();
               }
               catch
               {
                   //trs.Rollback();
               }
           }
           catch
           {

           }
       }
Posted
Updated 19-Aug-20 5:47am

You can only set an auto increment value to greater than the largest value already in the table - if you think about it that makes total sense: if you didn;t then duplicate index values become a high probability and that's a very bad idea.
So the second time you try, the largest value in the table is 2, and your attempt to set it to zero is ignored. You can set it to 3 or greater, but not below that.
 
Share this answer
 
SQL
ALTER TABLE some_table AUTO_INCREMENT = 0
Quote:
As of MySQL 5.6, documentation[^] now states: 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.

Quote:
How To Reset MySQL Autoincrement Column. Autoincrement Number Reseting[^] : Note that you cannot reset the counter to a value less than or equal to any that have already been used

Will reset the auto_increment value to be the next value based on the highest existing value in the auto_increment column. It will not just default reset to 0 as you are expecting.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900