为什么 MySQL auto_increment 会跳过数字

发布于 2024-09-17 20:23:08 字数 179 浏览 6 评论 0原文

我们正在将现有产品表导入到我们自己的新表中。我们编写的导入脚本运行完美,并插入了正确数量的行(6000 左右)。但是,导入后,下一个自动递增的主键/id 比表中的行数高出 1500 个条目(左右)。

我们无法理解 MySQL 为什么要这样做,我们希望它停止。我已经在网上进行了常规搜索以寻求帮助,但我一片空白 - 有什么想法吗?

We are running an import of an existing product table into a new table of our own. The import script we've written runs perfectly and inserts the right amount of rows (6000 or so). However, after the import the next auto incremented primary key/id is 1500 entries (or so) above the number of rows in the table.

We can't understand why MySQL is doing this and we'd like it to stop. I've done the usual searches online looking for help but I am drawing a blank - any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2024-09-24 20:23:08

让我们以这个简单的表为例:

CREATE TABLE `products` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(64) NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;

导入文件如下所示:

"id","name"
1,"product 1"
2,"product 2"
5,"product 3"
102,"product 4"

然后您将数据导入到两列中,因此自动递增机制不起作用。
导入所有行后,表的自动增量值将设置为 MAX(id)+1 [在本例中为103],以确保下一个自动增量 id 是唯一的。如果它等于插入的行数,则下一个自动增量值将为 5 并且将与第 3 行发生冲突。

如果您希望干净的开始和最后 id 等于行数,则必须从 .csv 文件中删除“id”列,或者创建不带 AUTO_INCRMENT 的表对于id,导入数据并运行这个简单的sql:

SET @i=0;
UPDATE `products` SET id=@i:=@i+1 ORDER BY id;
ALTER TABLE `products`  CHANGE COLUMN `id` `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT FIRST;

第一个查询设置辅助变量,该变量将在更新记录之前递增。
第二个更新记录,使其 id 等于行号。
第三步将更改 id 列以自动递增,并为下一个自动索引设置适当的值。

但在更改任何主键之前,请确保它们没有在任何其他表中用作外键!

Let's take this simple table for example:

CREATE TABLE `products` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(64) NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;

And import file that look like:

"id","name"
1,"product 1"
2,"product 2"
5,"product 3"
102,"product 4"

Then you are importing data to both columns, so auto incrementing mechanism does not work.
After importing all rows, autoincrement value for table is set to MAX(id)+1 [103 in this case] to ensure next autoincremented id is unique. If it was equal to number of rows inserted, then next autincrement value would be 5 and would colide with row #3.

If you want to have clean start and last id equal to number of rows you have to either get rid of "id" column from .csv file, or create table without AUTO_INCREMENT for id, import data and run this simple sql:

SET @i=0;
UPDATE `products` SET id=@i:=@i+1 ORDER BY id;
ALTER TABLE `products`  CHANGE COLUMN `id` `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT FIRST;

First query sets auxiliary variable, that will be incremented before updating the record.
Second one updates record to have id equal to row number.
Third will change id column to be autoincremented and set proper value for next autoindex.

But before changing any primary keys ensure that they are not used in any other tables as foreign keys!

恋竹姑娘 2024-09-24 20:23:08

如果执行此命令:

show create table Foo

那么您也会看到 AUTO_INCRMENT= 的设置。

我的猜测是它没有设置为以 0 开头。

然后您应该创建新表以将 AUTO_INCRMENT 设置为 0(或 1,我记不清了)。这应该可以解决问题。

If you perform this command:

show create table Foo

Then you will see what the AUTO_INCREMENT= is set too.

My guess is that it is not set to start with 0.

You should then create your new table to have the AUTO_INCREMENT set to 0 (or 1, I cannot remember from the top of my head). This should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文