在一个表中我设置到autoid为自动增长列
例如有如下数据1 张三 男 20
2 王五 男 22
3 李四 男 25
4 陈大 男 19现在我把 autoid=3 和 autoid=4 的两条记录删除
然后我再插入一条数据,例如:insert into userinfo(autoid,username,sex,age) values('null','春哥','男','27');但是面临一个问题,编号是从5开始了,而不是接着从3开始.毕竟我 第三条和第四条记录已经删除的了.请问如何让autoid 从3开始,请高手赐教.

解决方案 »

  1.   

    自动增长列设置成不自动的
    或者insert into tb values (3);mysql> select auto_increment from information_schema.tables where table_name="te
    st";
    +----------------+
    | auto_increment |
    +----------------+
    |              4 |
    +----------------+
    1 row in set (0.02 sec)
    或者设置这个值
      

  2.   

    alter table t_Myxiao7 AUTO_INCREMENT 3;mysql> create table t_Myxiao7(id int not null auto_increment primary key ,name v
    archar(10), genda varchar(10),age int);
    Query OK, 0 rows affected (0.08 sec)mysql> insert into t_Myxiao7 values
        -> (null,'张三','男',20),
        -> (null,'王五','男',22),
        -> (null,'李四','男',25),
        -> (null,'陈大','男',19);
    Query OK, 4 rows affected (0.05 sec)
    Records: 4  Duplicates: 0  Warnings: 0mysql>
    mysql> select * from t_Myxiao7;
    +----+------+-------+------+
    | id | name | genda | age  |
    +----+------+-------+------+
    |  1 | 张三 | 男    |   20 |
    |  2 | 王五 | 男    |   22 |
    |  3 | 李四 | 男    |   25 |
    |  4 | 陈大 | 男    |   19 |
    +----+------+-------+------+
    4 rows in set (0.01 sec)mysql> delete from t_Myxiao7 where id=3 or id =4;
    Query OK, 2 rows affected (0.08 sec)mysql> select * from t_Myxiao7;
    +----+------+-------+------+
    | id | name | genda | age  |
    +----+------+-------+------+
    |  1 | 张三 | 男    |   20 |
    |  2 | 王五 | 男    |   22 |
    +----+------+-------+------+
    2 rows in set (0.00 sec)mysql> alter table t_Myxiao7 AUTO_INCREMENT 3;
    Query OK, 2 rows affected (0.19 sec)
    Records: 2  Duplicates: 0  Warnings: 0mysql> insert into t_Myxiao7 values
        -> (null,'春哥','男','27');
    Query OK, 1 row affected (0.06 sec)mysql> select * from t_Myxiao7;
    +----+------+-------+------+
    | id | name | genda | age  |
    +----+------+-------+------+
    |  1 | 张三 | 男    |   20 |
    |  2 | 王五 | 男    |   22 |
    |  3 | 春哥 | 男    |   27 |
    +----+------+-------+------+
    3 rows in set (0.00 sec)mysql>
      

  3.   

    尽量不要一会自增,一会给予变量。
    会弄乱数据库的。
    宁可对错误的数据进行update修正,或者delete,而不要insert一个跟老数据有相同ID的新数据。