简体   繁体   中英

How to set autoincrement to 1 when getting the schemas

I have 12 tables transfered at the end of each year, with some conditions, to the new database.(db2012,db2013 etc.) With the below procedure I get the schemas from the old data base and create them into the new. But when creating new tables at the end of each schema there is a line AUTO_INCREMENT=oldlastvalue of autoincrement value.

How can get it 1

1) I have to TRUNCATE each table before transfering the data

2) Write a procedure for changing this number in the sql file.

3) ? hope getting it as 1 when reading from the old database

Is there a solution ?

private void CopySchemas(string pathname)
    {
        string [] MyArray = new string[12];
        int index = 0;
        using (MySqlConnection conn = new MySqlConnection(PublicVariables.cs))
        {
            using(MySqlCommand cmd = new MySqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='" + PublicVariables.DbName+"'",conn))
                {
                    conn.Open();
                    MySqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        MyArray[index] = reader.GetValue(0).ToString();   // getting only the name of the table for creating my filename.sql
                        ++index;
                    }
                    reader.Dispose();
                }
            for (int i = 0; i < MyArray.Length; ++i)
            {
                string tblname = MyArray[i];
                string fname = pathname  +"\\" + tblname + ".sql";
                MySqlCommand cmd = new MySqlCommand("SHOW CREATE TABLE " + PublicVariables.DbName + "." + tblname, conn);
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    for (int a = 0; a < reader.FieldCount; ++a)
                       string Schemas = reader.GetValue(a).ToString();
                    File.WriteAllText(fname,Schemas );
                }
                reader.Dispose();
            }
        }
    }

对于MySQL,命令为:

ALTER TABLE tablename AUTO_INCREMENT = 0

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