重新排序/重置自动增量主键 [英] Reorder / reset auto increment primary key

查看:206
本文介绍了重新排序/重置自动增量主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动增量主键的MySQL表。我删除了表中间的一些行。现在我有,例如,ID列中的这样:12,13,14,19,20.我删除了15,16,17和18行。

I have a MySQL table with an auto increment primary key. I deleted some rows in the middle of the table. Now I have, for example, something like this in the ID column: 12, 13, 14, 19, 20. I deleted the 15, 16, 17 and 18 rows.

我想重新分配/重置/重新排序主键,以便我有连续性,即使19 a 15,20 a 16等。

I want to reassign / reset / reorder the primary key so that I have continuity, i.e. make the 19 a 15, the 20 a 16, and so on.

推荐答案

您可以删除主键列并重新创建它。所有的ids应该按顺序重新分配。

You could drop the primary key column and re-create it. All the ids should then be reassigned in order.

但是这在大多数情况下可能是一个坏主意。如果你有其他表有外键的表,那么它肯定不会工作。

However this is probably a bad idea in most situations. If you have other tables that have foreign keys to this table then it will definitely not work.

这篇关于重新排序/重置自动增量主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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