相信许多小伙伴都遇到过删除某个表的时候出现外键约束的问题,在这里给大家提供一个查询本表外键关系的sql,如果本表被很多表引为外键,这时候笔者建议大家做假删除
select zjb.name as ‘主键表’, wjb.name as ‘外键表’,zml.name as ‘外键字段’
from sys.foreign_key_columns fkey join sys.objects wjb on fkey.parent_object_id=wjb.object_id
join sys.columns col on wjb.object_id=col.object_id and parent_column_id=col.column_id
join sys.objects zjb on fkey.referenced_object_id=zjb.object_id
join sys.columns zml on zjb.object_id=zml.object_id and referenced_column_id=zml.column_id
–and zjb.name=’被引用的表名’
order by zjb.name
原创文章,作者:Carrie001128,如若转载,请注明出处:https://blog.ytso.com/244774.html