[英]How to find records that violates foreign key constraints
I'm trying to find records that is violating the ORA-02291: integrity constraint::我正在尝试查找违反 ORA-02291 的记录:完整性约束::
I was running this query, But I didnt get any results back::我正在运行这个查询,但我没有得到任何结果::
SELECT child.parent_id
FROM child LEFT JOIN parent ON child.parent_id = parent.parent_id
WHERE parent.parent_id IS NULL;
Am I missing something or what are the other ways to find the records which are violating these constraints.我是不是遗漏了什么,或者还有什么其他方法可以找到违反这些限制的记录。
You can use not exists
: 您可以使用not exists
:
select parent_id
from child c
where not exists (
select 1
from parent p
where p.id = c.parent_id)
Here is the query to delete orphan records in child table to be able to make the foreign key constraints:这是删除子表中的孤立记录以便能够进行外键约束的查询:
DELETE FROM `child`
WHERE `parent_id` NOT IN (
SELECT DISTINCT `id`
FROM `parent`
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.