Mysql auto_increment以最低值继续

发布于 2024-11-13 07:22:29 字数 634 浏览 4 评论 0原文

我的问题是:我有一个带有 auto_increment 列的表。当我插入一些值时,一切都正确。 插入第一行:ID 1 插入第二行:ID 2

现在我想在 ID 10 处插入一行。

我的问题是,在此之后仅在 ID 10 之后插入行(这是正常行为)。

但我希望数据库在执行此操作之前首先填充 ID 3-9。

有什么建议吗?

编辑:

澄清一下:这是我想为自己构建的 URL 缩短器。 我将 id 转换为单词(a-zA-z0-9)以进行搜索,并将其转换为数字(即表的 ID)以保存在数据库中。

现在的问题是: 我缩短了第一个链接(没有名称)-> ID 为 1,名称自动为 1 转换为 a-zA-Z0-9,这是

下一个相同的情况 -> ID为2,名字为b,是2转换的。

接下来有趣的是,有人想将链接命名为 test -> ID 是 4597691 ,它是转换后的 test

现在,如果有人添加另一个没有名称的链接 -> ID 为 4597692, 将是 tesu,因为数字已转换。

我希望新行将自动插入到最后形成的间隙处(此处为 3)

My problem is: I have a table with an auto_increment column. When I insert some values, all is right.
Insert first row : ID 1
Insert second row : ID 2

Now I want to insert a row at ID 10.

My problem is, that after this there are only rows inserted after ID 10 (which is the normal behaviour ).

But I want that the database first fills up ID 3-9 before making that.

Any suggestions?

EDIT:

To clarify: this is for an URL shortener I want to build for myself.
I convert the id to a word(a-zA-z0-9) for searching, and for saving in the database I convert it to a number which is the ID of the table.

The Problem is now:
I shorten the first link (without a name) -> ID is 1 and the automatically name is 1 converted to a-zA-Z0-9 which is a

Next the same happenes -> ID is 2 and the name is b, which is 2 converted.

Next interesting, somebody want to name the link test -> ID is 4597691 which is the converted test

Now if somebody adds another link with no name -> ID is 4597692 which would be tesu because the number is converted.

I want that new rows will be automatically inserted at the last gap that was made (here 3)

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

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

发布评论

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

评论(5

二智少女 2024-11-20 07:22:29

您可以使用另一个整数列作为 URL ID。

您的流程可能如下所示:

  • 如果为链接生成了默认名称,则您只需插入一个新行,用自动增量值填充 URL ID 列,然后将结果转换为相应的名称。

  • 如果为 URL 指定了自定义名称,则在插入行后,URL ID 列将填充将所选名称转换为整数所获得的数字。

等等。当查找整数 ID 时,您将使用 URL ID 列,而不是表自动增量列。

如果我遗漏了什么,请告诉我。

You could have another integer column for URL IDs.

Your process then might look like this:

  • If a default name is generated for a link, then you simply insert a new row, fill the URL ID column with the auto-increment value, then convert the result to the corresponding name.

  • If a custom name is specified for a URL, then, after inserting a row, the URL ID column would be filled with the number obtained from converting the chosen name to an integer.

And so on. When looking up for integer IDs, you would then use the URL ID column, not the table auto-increment column.

If I'm missing something, please let me know.

十二 2024-11-20 07:22:29

您可以执行 6 次虚拟插入,并在以后根据需要删除/更新它们。根据设计,自动增量的概念旨在限制应用程序或用户对数字的控制,以确保输入表中的每条记录都有唯一的值。

You could do 6 dummy inserts and delete/update them later as you need. The concept of the auto increment, by design, is meant to limit the application's or user's control over the number to ensure a unique value for every single record entered into the table.

萌︼了一个春 2024-11-20 07:22:29
ALTER TABLE MY_TABLE AUTO_INCREMENT = 3;
ALTER TABLE MY_TABLE AUTO_INCREMENT = 3;
嘿哥们儿 2024-11-20 07:22:29

您必须找到第一个未使用的 id,将其存储为用户变量,用作插入的 id。

SELECT @id := t1.id +1 
FROM sometable t1 LEFT JOIN sometable t2 
ON t2.id = t1.id +1 WHERE t2.id IS NULL LIMIT 1;

INSERT INTO sometable(id, col1, col2, ... ) VALUES(@id, 'aaa', 'bbb', ... );

如果仍然存在间隙,则必须为每个插入运行两个查询,由您决定是否值得这样做。

You would have to find first unused id, store it as user variable, use as id for insert.

SELECT @id := t1.id +1 
FROM sometable t1 LEFT JOIN sometable t2 
ON t2.id = t1.id +1 WHERE t2.id IS NULL LIMIT 1;

INSERT INTO sometable(id, col1, col2, ... ) VALUES(@id, 'aaa', 'bbb', ... );

You will have to run both queries for every insert if you still have gaps, its up to you to decide whether it is worth doing it.

我是有多爱你 2024-11-20 07:22:29

不是 100% 确定你想要实现什么,但这样的事情可能会起作用:

drop table if exists foo;
create table foo
(
id int unsigned not null auto_increment primary key,
row_id tinyint unsigned unique not null default 0
)
engine=innodb;

insert into foo (row_id) values (1),(2),(10),(3),(7),(5);

select * from foo order by row_id;

+----+--------+
| id | row_id |
+----+--------+
|  1 |      1 |
|  2 |      2 |
|  4 |      3 |
|  6 |      5 |
|  5 |      7 |
|  3 |     10 |
+----+--------+
6 rows in set (0.00 sec)

not 100% sure what you're trying to achieve but something like this might work:

drop table if exists foo;
create table foo
(
id int unsigned not null auto_increment primary key,
row_id tinyint unsigned unique not null default 0
)
engine=innodb;

insert into foo (row_id) values (1),(2),(10),(3),(7),(5);

select * from foo order by row_id;

+----+--------+
| id | row_id |
+----+--------+
|  1 |      1 |
|  2 |      2 |
|  4 |      3 |
|  6 |      5 |
|  5 |      7 |
|  3 |     10 |
+----+--------+
6 rows in set (0.00 sec)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文