4

为什么当我使用 jdbc 插入我的数据库时,我的表 auto_increments 会被抬高。

填充完全空表的示例:

狗桌

DogId DogName
3     Woofer
4     Kujo
5     Spike

所有者表

OwnerId DogID OwnerName
6       3     George
7       4     John
8       5     Sam

期望的结果

狗桌

DogId DogName
1     Woofer
2     Kujo
3     Spike

所有者表

OwnerId DogID OwnerName
1       1     George
2       2     John
3       3     Sam

实际代码:

 public void insertStuff(Something d)
  {
    Connection con = null;

    try
    {
      Class.forName("com.mysql.jdbc.Driver");
      con = (Connection) DriverManager.getConnection(
          "jdbc:mysql://" + this.getServer() + "/" + this.getDatabase(), user,
          password);
      con.setAutoCommit(false);

      Statement s1 = (Statement) con.createStatement();
      s1.executeUpdate("INSERT IGNORE INTO DOG (DOG_NAME) VALUES(\""
          + d.getDogName() + "\")");

      Statement s2 = (Statement) con.createStatement();
      s2.executeUpdate("INSERT IGNORE INTO OWNER (DOG_ID,OWNER_TITLE) VALUES ("
          + "(SELECT DOG_ID FROM DEVICE WHERE DOG_NAME =\""
          + d.getDogName()
          + "\"),\"" + d.getOWNER() + "\")");

      Statement s3 = (Statement) con.createStatement();
      s3.executeUpdate("INSERT IGNORE INTO KENNEL " + "("
          + "KENNEL_NAME,+ "OWNER_ID) " + "VALUES " + "( \""
          + d.getKennelName()
          + "\","
          + "\""
          + ","
          + "(SELECT OWNER_ID FROM OWNER WHERE OWNER_TITLE=\""
          + d.getOWNER() + "\")" + ")");

      }

      con.commit();

    }
    catch (Exception e)
    {
      if (con != null)
        try
        {
          con.rollback();
        }
        catch (SQLException e1)
        {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }

      e.printStackTrace();
    }
    finally
    {
      if (con != null)
        try
        {
          con.close();
        }
        catch (SQLException e)
        {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
    }
  }
4

5 回答 5

3

在事务中插入一行,然后退出事务也会影响自动增量值。

您始终可以auto_increment直接重置 mysql 上的值:

alter table <tablename> auto_increment = <some_number>;

但老实说,这有什么关系?这些值必须是唯一的,但它们不应指示顺序。

于 2012-04-03T20:24:34.210 回答
3
ALTER TABLE Owner AUTO_INCREMENT = 1;
于 2016-01-28T06:41:14.830 回答
2

Only two scenarios I know about are:

(1) some records have been deleted

(2) there is some trigger on table that modify such id

Please note that even you made fresh inserts to empty table, if there were previously some rows, emptying table don't reset auto-increment ID counter and it continues on order from last issued number, not from the number of actual records in table...

于 2012-04-03T20:26:04.163 回答
2

很可能您以前从 Dog/Owner 表中删除了增加自动增量数的行。您需要重置它(或者只是对这些表执行 DROP/CREATE 并重新插入数据):

ALTER TABLE Dog AUTO_INCREMENT = 1;
ALTER TABLE Owner AUTO_INCREMENT = 1;

另一种实现相同目的的方法是运行 truncate:

TRUNCATE Dog;
TRUNCATE OWNER;

这将删除数据并重置索引。

于 2012-04-03T20:21:33.843 回答
1

AUTO_INCREMENT 变量是新插入记录的 id。如果您插入了 10 条记录,AUTO_INCREMENT 将设置为 11。现在,如果您从表中删除所有记录并尝试插入新记录,它将使用 AUTO_INCREMENT 值。所以新的 id 将是 11 。您可以通过更改表来更新它

ALTER TABLE <tablename> AUTO_INCREMENT = 1;
于 2015-06-11T07:26:32.173 回答