查找另一个约束引用的所有约束

时间:2016-02-25 07:36:31

标签: sql sql-server constraints

Meldung 3725, Ebene 16, Status 0, Zeile 327
The constraint 'UQ_users_email' is being referenced by table 'posts', foreign key constraint 'FK_posts_users'.
Meldung 3727, Ebene 16, Status 0, Zeile 327
Could not drop constraint. See previous errors.

除了解析错误消息之外,我如何获得阻止某个约束丢失的所有约束的列表?

1 个答案:

答案 0 :(得分:1)

FK 取决于(阻止掉)引用表的 PK / UQ ,任何类型的约束阻止列的丢弃这取决于。此查询可以列出引用目标表的表列表:

select
  object_schema_name(fk.fkeyid) + '.' + object_name(fk.fkeyid) as [REF_BY], 
  object_name(fk.constid) as [REF_NAME]
from sys.sysforeignkeys fk
inner join sys.sysconstraints cs on cs.constid = fk.constid
where fk.rkeyid = object_id('dbo.my_target_table_with_constraint_to_drop', 'u')
order by 1, 2

所有这些FK都会阻止PK / UQ从目标表中掉落