我在mysql中建了一个数据库,然后在这个数据库中建了一个表,表中每一个记录有一个id字段,属性设为了auto_increment,然后用PHP操作数据库。
当我插入了若干个记录后,假如id到了10,然后我把这10个记录都删除(表还在,只是空了),然后,如果我再新插入一个记录,那么它的id从11开始,没问题,但是,如果在删除了那10个记录(表空了)后,我关机重启了,重启后再插入新的记录,id号又从1开始了...郁闷!按理说id号不应该是每个记录都唯一、不重复的吗?纠结...哪位高手懂啊,教教我吧!

解决方案 »

  1.   

    不可能啊?
    你用的什么存储引擎?贴出你的 show create table xxx;
      

  2.   

    第二次你用的truncate删数据的吧
      

  3.   

    truncate删数据的话,才会使自增字段的初始位置回到1.DELETE不影响子增字段的最后位置。
      

  4.   

    innodb的表类型就是如楼主所说(可以认为每一次重新启动服务器,都会执行一次select max(id) from table 得到的就是最大的ID),
    使用myisam就不会这样!
      

  5.   

    如果表空的,那么自然就是从1开始计数;不是空的max(id)+1开始!
      

  6.   

    http://dev.mysql.com/doc/refman/5.1/en/innodb-auto-increment-handling.htmlIf you specify an AUTO_INCREMENT column for an InnoDB table, the table handle in the InnoDB data dictionary contains a special counter called the auto-increment counter that is used in assigning new values for the column. This counter is stored only in main memory, not on disk. 
    InnoDB uses the following algorithm to initialize the auto-increment counter for a table t that contains an AUTO_INCREMENT column named ai_col: After a server startup, for the first insert into a table t, InnoDB executes the equivalent of this statement: 
    SELECT MAX(ai_col) FROM t FOR UPDATE;
    InnoDB increments by one the value retrieved by the statement and assigns it to the column and to the auto-increment counter for the table. If the table is empty, InnoDB uses the value 1. 
      

  7.   

    楼主看样子是用Truncate进行了去尾操作,那样即使没有重新启动电脑,紧接着插入数据也会是从1开始的。