显示示例行,当删除因外键约束而失败时

发布于 2025-01-30 08:17:30 字数 1267 浏览 2 评论 0原文

我试图从带有多个表的MySQL数据库中删除一些旧的历史数据。 有一个“卷”表,还有一个“元数据”表。当然,您不能有一个不存在的卷的元数据,因此存在外键约束。

这是我要做的查询:

DELETE FROM volumes 
       WHERE deleted > 0 AND 
             deleted_at IS NOT NULL AND 
             deleted_at < '2020-01-01T00:00:00' AND 
             status = 'deleted' AND 
             attach_status = 'detached' 
       LIMIT 5000

这会导致以下错误(重新格式化以获取可读性):

Integrity error: 'DELETE FROM volumes WHERE deleted > 0 AND deleted_at IS NOT NULL AND deleted_at < '2020-01-01T00:00:00' AND status = 'deleted' AND attach_status = 'detached' LIMIT 5000' 
(1451, 'Cannot delete or update a parent row: 
        a foreign key constraint fails 
        (`volume_glance_metadata`, 
         CONSTRAINT `volume_glance_metadata_ibfk_1` FOREIGN KEY (`volume_id`) REFERENCES 
                                                                `volumes` (`id`))')

如果我尝试搜索此问题,所有答案是对不同问题的答案,例如“我如何删除此问题”或“如何删除约束”?那不是我感兴趣的。

我想知道的是:如何从表中找到一个行的示例在volume_glance_metadata表中是否仍然有相应的ID> IDvolume_glance_metadata.volume_id = folumes.id)中的一行?或换句话说:给我一个示例,使删除失败。

我认为至少有一排无法删除。也许不存在可以删除的行。 MySQL并没有竭尽所能找到其他行以删除,如果有些会失败,这似乎有些令人失望。

I am trying to delete some old historic data from a MySQL database with multiple tables.
There is a "volumes" table, and a "metadata" table. Of course, you can't have metadata for a volume that does not exist, so there is a foreign key constraint.

This is a query I'm trying to do:

DELETE FROM volumes 
       WHERE deleted > 0 AND 
             deleted_at IS NOT NULL AND 
             deleted_at < '2020-01-01T00:00:00' AND 
             status = 'deleted' AND 
             attach_status = 'detached' 
       LIMIT 5000

This results in the following error (reformatted for readability):

Integrity error: 'DELETE FROM volumes WHERE deleted > 0 AND deleted_at IS NOT NULL AND deleted_at < '2020-01-01T00:00:00' AND status = 'deleted' AND attach_status = 'detached' LIMIT 5000' 
(1451, 'Cannot delete or update a parent row: 
        a foreign key constraint fails 
        (`volume_glance_metadata`, 
         CONSTRAINT `volume_glance_metadata_ibfk_1` FOREIGN KEY (`volume_id`) REFERENCES 
                                                                `volumes` (`id`))')

All answers I find if I try to search for this are answers to different questions, like "how can I delete this anyway" or "how can I remove the constraint"? That's not what I'm interested in though.

What I would like to know is: How can I find an example of a row from the volumes table that the DELETE query tried to delete, but cannot be deleted, because there is still a row in the volume_glance_metadata table that has the corresponding id (volume_glance_metadata.volume_id = volumes.id)? Or in other words: give me an example that makes the deletion fail.

I presume that there is at least one row that cannot be deleted; or maybe no row exists that can be deleted. It seems a bit disappointing that MySQL doesn't do its best to find other rows to delete, if there are some that would fail.

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

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

发布评论

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

评论(1

末が日狂欢 2025-02-06 08:17:30

只需在卷中的行上进行等效条件的选择,然后将这些行连接到依赖的表metadata

SELECT m.*
FROM volumes AS v
INNER JOIN metadata AS m ON m.volume_id = v.id
WHERE v.deleted > 0 
 AND v.deleted_at IS NOT NULL 
 AND v.deleted_at < '2020-01-01T00:00:00' 
 AND v.status = 'deleted' 
 AND v.attach_status = 'detached' 

联接是内部连接,而不是外部连接,因此自然只能返回匹配的行。

如果此加入查询在其结果中没有返回行,则元数据不包含会阻止您删除的行。

Just do a SELECT with equivalent conditions on rows in volumes, and JOIN those rows to the dependent table metadata.

SELECT m.*
FROM volumes AS v
INNER JOIN metadata AS m ON m.volume_id = v.id
WHERE v.deleted > 0 
 AND v.deleted_at IS NOT NULL 
 AND v.deleted_at < '2020-01-01T00:00:00' 
 AND v.status = 'deleted' 
 AND v.attach_status = 'detached' 

The join is an inner join, not an outer join, so it naturally returns only matching rows.

If this join query returns no rows in its result, then metadata contains no rows that would block your delete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文