看来用户grandepivko是对的。
将 AUTO_INCREMENT 设置回 1(或至少是它的努力)会将下一个 AI 设置为正确的值。
因此,要点是将AUTO_INCREMENT 设置回 1 LOAD DATA INFILE。
ALTER TABLE product AUTO_INCREMENT=1;
drop table if exists product;
create table product
(
id int auto_increment primary key,
code int not null,
description varchar(100) not null,
name varchar(100) not null,
price decimal(10,2) not null,
stockLevel int not null
);
LOAD DATA LOCAL INFILE 'c:\\nate\\load_drinks01.txt' INTO TABLE product
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(code, description, name, price, stockLevel);
select * from product; -- has products 1,2,3 for AUTO_INC id
select `AUTO_INCREMENT`
from INFORMATION_SCHEMA.TABLES
where TABLE_SCHEMA = 'so_gibberish' -- database name
and TABLE_NAME = 'product'; -- table name
-- value is now 5
ALTER TABLE product AUTO_INCREMENT=1;
select `AUTO_INCREMENT`
from INFORMATION_SCHEMA.TABLES
where TABLE_SCHEMA = 'so_gibberish' -- database name
and TABLE_NAME = 'product'; -- table name
-- value is now 4
LOAD DATA LOCAL INFILE 'c:\\nate\\load_drinks01.txt' INTO TABLE product
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(code, description, name, price, stockLevel);
select * from product; -- has products 1,2,3,4,5,6 for AUTO_INC id
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'so_gibberish' -- database name
AND TABLE_NAME = 'product'; -- table name
-- value is now 8
ALTER TABLE product AUTO_INCREMENT=1;
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'so_gibberish' -- database name
AND TABLE_NAME = 'product'; -- table name
-- value is now 7
来自以下Bug Report,一个sn-p:
[2013 年 3 月 12 日 23:50] Don Hui Btw,还有另一种解决方法是
将表 AUTO_INCREMENT COUNTER 更改为 1,因此它重置为最大值
记录数:
ALTER TABLE 表 AUTO_INCREMENT=1
在该表的 AUTO_INCREMENT 重置为 PRIMARY KEY 的 MAX+1 值之后