mysql建立一個自增列,初始為1,新建一個記錄,其值為2,如果刪除掉這條記錄,那麼下次新建的記錄值是2還是3,如果是2,怎麼是sql新建的記錄值跳過原有的值呢
不知道你有沒有試過,刪除所有數據,再插入時id是3,但清空數據表時,再插入又是從1開始的。你可以每次刪除某條記錄是,重新排序一下,給你一個存儲過程看一下
1、傳參:傳遞兩個參數進去,每次刪除數據時調用一次
CREATE DEFINER=root
@localhost
PROCEDURE reorder
(IN tablename
CHAR(50), IN orderid
CHAR(50))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
set @tab=tablename;
set @id=orderid;
set @aa:=concat("ALTER TABLE ",@tab," drop " , @id);
set @bb:=concat("ALTER TABLE ",@tab," ADD ", @id," INT( 8 ) NOT NULL FIRST;");
set @cc:=concat("ALTER TABLE ",@tab," MODIFY COLUMN ",@id," INT( 8 ) NOT NULL AUTO_INCREMENT,ADD PRIMARY KEY(",@id,")");
prepare pa from @aa;
prepare pb from @bb;
prepare pc from @cc;
execute pa;
execute pb;
execute pc;
END
2、靜態不含參
CREATE DEFINER=root
@localhost
PROCEDURE reset
()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
ALTER TABLE user DROP useid;
ALTER TABLE user ADD useid INT( 8 ) NOT NULL FIRST;
ALTER TABLE user MODIFY COLUMN useid INT( 8 ) NOT NULL AUTO_INCREMENT,ADD PRIMARY KEY(useid);
END