主键递增不连续解决方法
解决方法:只需要在执行新的插入操作时,加上这么一句就可以了
alter table copy1_orders AUTO_INCREMENT=1;
问题和代码我放在下面,可以自行查看:
问题:
要求:
代码:
40万条数据
-- 建表
create table orders1(
id int not null auto_increment primary key,
num int,
hashcode smallint
);
-- 插数据
DELIMITER $$
create procedure insertData()
begin
declare i int default 1;
declare num_o int ;
declare hashnum smallint;
while i<=400000 do
set i=i+1;
set num_o=floor(rand()*5000001);
set hashnum=floor(rand()*10);
insert into orders1(num,hashcode) values(num_o,hashnum);
end while;
end;$$
DELIMITER
/*drop procedure insertData; */
call insertData();
copy400万条数据
create table copy1_orders like orders1;
insert into copy1_orders select * from orders1 ;
DELIMITER $$
create procedure insertData2()
begin
insert into copy1_orders(id,num,hashcode) select null,num,hashcode from orders1;
end;$$
DELIMITER ;
/*drop procedure insertData2; */
-- 解决主键递增不连续的问题
alter table copy1_orders AUTO_INCREMENT=1; -- *************
call insertData2(); -- 连续调用9次即可生成4000000条数据