我有一个带有AUTO_INCREMENTING id列和名称列的数据库。如果我从数据中删除一行,我想更新所有其他行的id,这样序列就是正确的,我很失落请帮助我。
我试过了:
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT COUNT(id) FROM " + table_name_products);
int count = 0;
while(rs.next()){
count = rs.getInt("COUNT(id)");
}
stmt = conn.createStatement();
for(int i = 1; i <= count; i++){
if(i > id){
stmt.addBatch("UPDATE " + table_name_products + " SET id = "+ i-- +" WHERE id = "+ i +";");
}
}