我在设计数据库的时候有遇到在将自动增长列删除后, 以后插入的时候该列值就不可以用了,如何设置可以重复利用该值呢?
(如果我将id为10 的行删掉,然后又插入一行,那么id就自动增长为 11了,如何让id 为10呢?)请教......

解决方案 »

  1.   

    alter table tb_name auto_increment=xxx
      

  2.   

    alter table Item auto_increment=1;
    我试过,但是只是把替代以前删掉的哪一行啊,但是id没有还原啊.
      

  3.   


    刚刚引用错了...alter table Item auto_increment=1; 
    我试过,但是只是把替代以前删掉的哪一行啊,但是id没有还原啊.
      

  4.   

    可以啊,测试如下,注意 |  3 |   40 |mysql> create table t_rickypc(id int primary key auto_increment,col int);
    Query OK, 0 rows affected (0.08 sec)mysql> insert into t_rickypc values (null,10);
    Query OK, 1 row affected (0.03 sec)mysql> insert into t_rickypc values (null,20);
    Query OK, 1 row affected (0.03 sec)mysql> insert into t_rickypc values (null,30);
    Query OK, 1 row affected (0.02 sec)mysql> select * from t_rickypc;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |   10 |
    |  2 |   20 |
    |  3 |   30 |
    +----+------+
    3 rows in set (0.00 sec)mysql> delete from t_rickypc where id=3;
    Query OK, 1 row affected (0.08 sec)mysql> select * from t_rickypc;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |   10 |
    |  2 |   20 |
    +----+------+
    2 rows in set (0.00 sec)mysql> alter table t_rickypc auto_increment=3;
    Query OK, 2 rows affected (0.19 sec)
    Records: 2  Duplicates: 0  Warnings: 0mysql> insert into t_rickypc values (null,40);
    Query OK, 1 row affected (0.03 sec)mysql> select * from t_rickypc;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |   10 |
    |  2 |   20 |
    |  3 |   40 |
    +----+------+
    3 rows in set (0.00 sec)mysql>
      

  5.   

    alter table 表名 auto_increment=自增开始的数字
    楼上的很详细了
      

  6.   

    我在网上查了一个 好像是sql sever的语法:
    set identity_insert A on
    insert A(ID,Name) values(vid,vname)
    set identity_insert A off请问mysql有没有这样的用法呢? 
    反正我试用过. 但是提示语法错误:Unknown system variable 'identity_insert'...
      

  7.   


    处理的列数很多的时候.可能都不知道上次什么时候删除的.但是我插入的时候肯定知道啊,那么上次的auto_increment 怎么来呢?
      

  8.   


    没看懂什么意思。last_insert_id() 可以得到刚插入记录的auto_incrementmysql> insert into t_rickypc values (null,50);
    Query OK, 1 row affected (0.06 sec)mysql> select last_insert_id();
    +------------------+
    | last_insert_id() |
    +------------------+
    |                4 |
    +------------------+
    1 row in set (0.00 sec)mysql>
      

  9.   

    我在网上查了一个 好像是sql sever的语法:
    set identity_insert A on
    insert A(ID,Name) values(vid,vname)
    set identity_insert A offMYSQL中更简单,你可以直接指定,不需要去 set identity_insert A off[
    mysql> select * from t_rickypc;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |   10 |
    |  2 |   20 |
    |  3 |   40 |
    |  4 |   50 |
    +----+------+
    4 rows in set (0.00 sec)mysql> delete from t_rickypc where id=3;
    Query OK, 1 row affected (0.06 sec)mysql> select * from t_rickypc;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |   10 |
    |  2 |   20 |
    |  4 |   50 |
    +----+------+
    3 rows in set (0.00 sec)mysql> insert into t_rickypc values (3,30);
    Query OK, 1 row affected (0.08 sec)mysql> select * from t_rickypc;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |   10 |
    |  2 |   20 |
    |  3 |   30 |
    |  4 |   50 |
    +----+------+
    4 rows in set (0.00 sec)mysql>
      

  10.   

    因为涉及到数据库的删除和插入.  在删除的时候是会指定id来删除的, 但是插入的时候如何将上次或者以前删除过的id 再次利用起来呢(一般不会指定id的吧)??? 
      

  11.   

    插入前先选出最小的不存在的空号select id+1
    from xxx a
    where not exists (select 1 from xxx where id=a.id+1)