sql - 模拟 DELETE 外键检查
问题描述
有什么方法可以模拟运行 DELETE 查询时发生的外键检查?
就像您尝试删除在其他表中引用的对象一样,您在运行 DELETE 查询时会遇到错误。但是有没有办法查询某个表中某个行的所有“依赖项”?
解决方案
但是如果您想检查是否有对特定行的引用,您可以使用与所有引用的表连接的查询:
select m.pk
, case when max(t1.fk) is not null then 1 else 0 end "refrenced in t1"
, case when max(t2.fk) is not null then 1 else 0 end "refrenced in t2"
from MainTable m
left join childtable1 t1
on m.pk = t1.fk
left join childtable1 t2
on m.pk = t2.fk
where m.pk = 10;
group by m.pk
如果要获取主表和所有子表的行:
select *
from MainTable m
left join childtable1 t1
on m.pk = t1.fk
left join childtable1 t2
on m.pk = t2.fk
where m.pk = 10;
推荐阅读
- javascript - 我下面的代码自动加载一个模态,在加载的模态我有一个复选框
- f# - F# 是否提供任何未排序的地图/字典?
- python - 错误 Unicode 对象必须在散列之前进行编码
- java - Camel smb:// - 从一个文件夹中消耗大量文件
- bash - 将目录中的最后一个数字加 1?
- jquery - IDE 在代码片段上产生语法错误
- java - 将 `@UsingDataSet` 转换为 `@Sql`
- autohotkey - Revit中的ControlClick不一致
- python - 如何在不损失效率的情况下将此代码变成 1 行?
- c# - UWP 数据绑定:事件处理程序中的属性更改不会更新 UI