显示示例行,当删除因外键约束而失败时
我试图从带有多个表的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> ID
(volume_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在
卷中的行上进行等效条件
的选择,然后将这些行连接到依赖的表metadata
。联接是内部连接,而不是外部连接,因此自然只能返回匹配的行。
如果此加入查询在其结果中没有返回行,则
元数据
不包含会阻止您删除的行。Just do a SELECT with equivalent conditions on rows in
volumes
, and JOIN those rows to the dependent tablemetadata
.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.