假设我有两个具有外键关系的表。如何在“父”表中找到没有相应“子”行的行?
例如:
create table a(x number primary key);
create table b(x number);
alter table b add constraint b_fk foreign key(x) references a(x) enable;
insert into a values(1);
insert into b values(1);
insert into a values(2);
commit;
delete from a where x = 2; -- will succeed
delete from a where x = 1; -- will fail, because there is a child row.
select ??? as "rows with no children" from a;
rows with no children
---------------------
2
表达这一点的语义上适当的方式是:
NOT EXISTS 使用反半连接,它允许优化器对表和连接基数非常敏感。
鉴于缺乏样本表,我必须弥补......
parent_table.foreign_key_id 可能与 child_table.id 匹配的位置