使MySQL自动增量ID(重新)从1开始 [英] Make MySQL auto-increment id (re) start from 1

查看:471
本文介绍了使MySQL自动增量ID(重新)从1开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大"更新:

好吧,我得到了全部 自动增量点错误.我虽然 这将是更容易定位的方法 第一,第二,第三等行, 但这只是错误的方法.

Ok I was getting the whole auto-increment point wrong. I though this would be an easier way to target the first, second, third and so row, but it is just the wrong approach.

您应该在乎 auto_increments是唯一的并且很好... 他们增加.你应该用 为此.

You should instead care about that the auto_increments are unique and well... that they increment. You should use the for that.

我不会删除此问题,因为我 认为这可能对某人有帮助 否则有相同的错误主意,但但是 被警告!:)

I wont delete this question because I think it might be helpful for someone else with the same wrong idea, BUT BE WARNED! :)


我有一个非常简单的MySQL表,如下所示:


I have a very simple MySQL table which went like this:

id    comment    user

1     hello      name1
2     bye        name2
3     hola       name3

然后我删除了前两个评论,结果是:

Then I deleted the two first comments, the result:

id    comment    user

3     hola      name3

现在,当我添加评论时:

So now when I add comments:

id    comment    user

3     hola      name3
5     chau      name4
6     xxx       name5

我的问题是,每当删除一行时,我都需要重新开始"并看起来像这样.

My problem is that I would need that whenever a row gets deleted it should "start over" and look like this.

id    comment    user

1     hola      name3
2     chau      name4
3     xxx       name5

我想知道如何以某种方式重新启动"表,以便总是"索引1、2、3等.

I would like to know how is it possible to some how "restart" the table so that it is "always" indexed 1, 2, 3 and so on.

提前谢谢!

我希望我已经足够清楚地解释了自己,对我所有的纯英语"感到抱歉,如果您认为一个单词可能会造成混淆,请随时进行),并请您进行任何澄清!

I hope I have explained myself clear enough, I'm sorry for all my "plain english", feel free to edit if you think a word might be confusing :) and please ask for any clarification needed!

顺便说一句:我没有添加任何代码,因为这是一种简化的情况,尽管它会使人更困惑,对其他人的帮助也较小,但是我认为这将有助于(或有必要)告诉我!" /p>

BTW: I did not add any of my code because this is a simplified situation and I though it be more confusing and less helpful to others, but I you think it would help (or is necessary) tell me about it!

推荐答案

使用MySQL的自动增量功能无法完成.您可以推出自己的解决方案,例如应用程序逻辑和数据库触发器之间的混合.但是,严重的是,如果您需要回收UNIQUE ID,则会严重破坏您的设计.

Can't be done using MySQL's autoincrement feature. You could roll your own solution, e.g. a mix between application logic and database triggers. BUT, seriosly, your design is heavily broken if it requires you to recycle UNIQUE IDs.

您是否可以只创建另一个表来保存这样的引用(可以通过查询最小值来实现),然后让主表指向该辅助表?

Couldn't you just create another table where you'd save references like that (this could be done by querying the minimum) and let your main table point to that auxilliary table?

编辑
这是我搜索过的博客,涉及您的问题:请参见此处.

EDIT
Here's a blog I've googled that deals with your problem: see here.

这篇关于使MySQL自动增量ID(重新)从1开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆