比如有一张表,ID为自增的,目前自增到了10,现在我删除ID大于5的数据,那表里面的数据只有1-5,现在继续向该表里插入数据的话,ID从10开始算起了,问题是:如何把ID从5开始算起呢,就是ID号继续是连续的,而不是删除了之后就没有这些ID号了..

解决方案 »

  1.   

    将数据存入其它表中,删除记录,修改起始为1(ALTER TABLE dd AUTO_INCREMENT = 1),再导入数据
      

  2.   

    只有这一个办法吗,想sql server 是直接可以用set identity_insert on/OFF ,mysql没有类似的吗.
      

  3.   

    alter table tbname auto_increment = 6;[
      

  4.   

    mysql不需要设置set identity_insert on tbname
    直接
    insert into tb(id,column) values(XX,XX)
      

  5.   


    alter table tbname auto_increment = 6;
      

  6.   

    看来是alter table tbname auto_increment = last_ID 这个方法最好了,谢谢各位~
      

  7.   

    [email protected]_monitor>select * from test;
    +----+-------+
    | id | value |
    +----+-------+
    |  1 |     1 | 
    |  2 |     2 | 
    |  5 |     5 | 
    +----+-------+
    3 rows in set (0.00 sec)[email protected]_monitor>delete from test where id=5;
    Query OK, 1 row affected (0.00 sec)[email protected]_monitor>alter table test auto_increment=3;
    Query OK, 2 rows affected (0.07 sec)
    Records: 2  Duplicates: 0  Warnings: [email protected]_monitor>insert into test(value) values(3);
    Query OK, 1 row affected (0.00 sec)[email protected]_monitor>select * from test;
    +----+-------+
    | id | value |
    +----+-------+
    |  1 |     1 | 
    |  2 |     2 | 
    |  3 |     3 | 
    +----+-------+
    3 rows in set (0.00 sec)[email protected]_monitor>