如何对主索引重新编号 [英] How to renumber primary index

查看:39
本文介绍了如何对主索引重新编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的MySQL表,并且主索引(id)并未一一编号(1、31、35、100等).我希望它们被编号为(1、2、3、4).请告诉我该怎么做.我还要指出,我知道该操作可能造成的后果,但我只是想整理一下桌子.

I have got a simple MySQL table and primary index (id) is not numbered one by one (1, 31, 35, 100 etc.). I want them to be numbered like (1, 2, 3, 4). Please tell me how to do it. I would also like to point that I am aware of possible consequences of the operation, but I just want to tidy up the table.

推荐答案

我同意其他方法也可以,但是我只是给出了一个不同的想法.这将不需要任何临时表创建要求:

I agree other methods will work but I'm just giving a different idea. This will do without any temp table creation requirements::

SET @i=0;
UPDATE table_name SET column_name=(@i:=@i+1);

这篇关于如何对主索引重新编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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