简体   繁体   English

我们如何重新使用从任何 MySQL-DB 表中删除的 id?

[英]How can we re-use the deleted id from any MySQL-DB table?

How can we re-use the deleted id from any MySQL-DB table?我们如何重新使用从任何 MySQL-DB 表中删除的 id?

If I want to rollback the deleted ID , can we do it anyhow?如果我想回滚已删除的 ID ,我们可以这样做吗?

It may be possible by finding the lowest unused ID and forcing it, but it's terribly bad practice, mainly because of referential integrity: It could be, for example, that relationships from other tables point to a deleted record, which would not be recognizable as "deleted" any more if IDs were reused.通过查找最低的未使用 ID 并强制使用它可能是可能的,但这是非常糟糕的做法,主要是因为参照完整性:例如,可能是其他表的关系指向已删除的记录,这将无法识别为如果重复使用 ID,则不再“删除”。

Bottom line: Don't do it.底线:不要这样做。 It's a really bad idea.这真是个坏主意。

Related reading: Using auto_increment in the mySQL manual相关阅读: 在 mySQL 手册中使用 auto_increment

Re your update: Even if you have a legitimate reason to do this, I don't think there is an automatic way to re-use values in an auto_increment field.重新更新:即使您有正当理由这样做,我也不认为有一种自动方法可以重用auto_increment字段中的auto_increment If at all, you would have to find the lowest unused value (maybe using a stored procedure or an external script) and force that as the ID (if that's even possible.).如果有的话,您必须找到最低的未使用值(可能使用存储过程或外部脚本)并强制将其作为 ID(如果可能的话)。

You shouldn't do it.你不应该这样做。
Don't think of it as a number at all.根本不要把它当作一个数字。
It is not a number.它不是一个数字。 It's unique identifier.它是唯一标识符。 Think of this word - unique .想想这个词——独一无二 No record should be identified with the same id.不应使用相同的 ID 标识任何记录。

1. 1.

As per your explanation provided "@Pekka, I am tracking the INsert Update and delete query..." I assume you just some how want to put your old data back to the same ID.根据您提供的解释“@Pekka,我正在跟踪插入更新和删除查询...”我假设您只是想将旧数据放回同一个 ID。

In that case you may consider using a delete-flag column in your table.在这种情况下,您可以考虑在表中使用删除标志列。

If the delete-flag is set for some row, you shall consider program to consider it deleted.如果为某行设置了删除标志,则应考虑程序将其视为已删除。 Further you may make it available by setting the delete-flat(false).此外,您可以通过设置 delete-flat(false) 使其可用。

Similar way is to move whole row to some temporary table and you can bring it back when required with the same data and ID.类似的方法是将整行移动到某个临时表,您可以在需要时使用相同的数据和 ID 将其带回来。

Prev.上一页idea is better though.想法更好。

2. 2.

If this is not what you meant by your explanation;如果这不是你解释的意思; and you want to delete and still use all the values of ID(auto-generated);并且您想删除并仍然使用 ID(自动生成)的所有值; i have a few ideas you may implement: - Create a table (IDSTORE) for storing Deleted IDs.我有一些您可以实施的想法: - 创建一个表 (IDSTORE) 来存储已删除的 ID。 - Create a trigger activated on row delete which will note the ID and store it to the table. - 创建一个在行删除时激活的触发器,它将记录 ID 并将其存储到表中。 - While inserting take minimum ID from IDSTORE and insert it with that value. - 插入时从 IDSTORE 中获取最小 ID 并使用该值插入它。 If IDSTORE is empty you can pass NULL ID to generate Auto Incremented number.如果 IDSTORE 为空,您可以传递 NULL ID 以生成自动增量编号。

Of course if you have references / relations (FK) implemented, you manually have to look after it, as your requirement is so.当然,如果您实现了引用/关系 (FK),您必须手动照看它,因为您的要求是这样。

Further Read: http://www.databasejournal.com/features/mysql/article.php/10897_2201621_3/Deleting-Duplicate-Rows-in-a-MySQL-Database.htm进一步阅读: http : //www.databasejournal.com/features/mysql/article.php/10897_2201621_3/Deleting-Duplicate-Rows-in-a-MySQL-Database.htm

Here is the my case for mysql DB:这是我的 mysql DB 案例:

I had menu table and the menu id was being used in content table as a foreign key.我有菜单表,菜单 ID 在内容表中用作外键。 But there was no direct relation between tables (bad table design, i know but the project was done by other developer and later my client approached me to handle it).但是表之间没有直接关系(糟糕的表设计,我知道但该项目是由其他开发人员完成的,后来我的客户找我来处理它)。 So, one day my client realised that some of the contents are not showing up.所以,有一天我的客户意识到有些内容没有显示出来。 I looked at the problem and found that one of the menu is deleted from menu table, but luckily the menu id exist in cotent table.我查看了问题,发现其中一个菜单从菜单表中删除,但幸运的是菜单 id 存在于 cotent 表中。 I found the menu id from content table that was deleted and run the normal insert query for menu table with same menu id along with other fields.我从已删除的内容表中找到了菜单 id,并对具有相同菜单 id 和其他字段的菜单表运行正常插入查询。 (Id is primary key) and it worked. (Id 是主键)并且它起作用了。

insert into tbl_menu(id, col1, col2, ...) values(12, val1, val2, ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM