我在每年年底有12个表转移到新数据库。(db2012,db2013等) 通过以下过程,我从旧数据库中获取模式并将其创建为新模式。 但是当在每个模式的末尾创建新表时,有一行AUTO_INCREMENT =自动增量值的oldlastvalue。
如何获得1
1)我必须在传输数据之前对每个表进行TRUNCATE
2)编写一个在sql文件中更改此编号的过程。
3)?希望在从旧数据库中读取时将其设为1
有解决方案吗?
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();
}
}
}
答案 0 :(得分:2)
对于MySQL,命令是:
ALTER TABLE tablename AUTO_INCREMENT = 0