如何找到哪些外键引用了表上的索引 [英] How to find what foreign key references an index on table

查看:37
本文介绍了如何找到哪些外键引用了表上的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要删除的非聚集索引(它是聚集索引的副本).但是,它正被外键约束使用.我希望能够确定哪个约束正在使用索引,以便我可以更新它以使用主键索引.

I have a non clustered index I would like to drop (it is a duplicate of the clustered index). However, it is being used by a foreign key constraint. I would like to be able to identify which constraint is using the index, so I can update it to use the primary key index.

当我试图放下它时:

DROP INDEX [idx_duplicate] ON [dbo].[MyTable]

我收到一个错误:

索引dbo.MyTable.idx_duplicate"上不允许使用显式的 DROP INDEX.它用于强制执行 FOREIGN KEY 约束.

An explicit DROP INDEX is not allowed on index 'dbo.MyTable.idx_duplicate'. It is being used for FOREIGN KEY constraint enforcement.

我试图通过以下查询找到罪魁祸首,但没有成功:

I tried to find the culprit with the following query but no luck:

SELECT name
FROM sys.foreign_keys
WHERE OBJECT_NAME (referenced_object_id) = 'idx_duplicate'

推荐答案

类似的东西

Select
    f.name,
    object_name(f.parent_object_id)
From
    sys.foreign_keys f
        inner join
    sys.indexes i
        on f.referenced_object_id = i.object_id and
           f.key_index_id = i.index_id
Where
    i.name = 'idx_duplicate' and
    i.object_id = object_id('[dbo].[MyTable]')

这篇关于如何找到哪些外键引用了表上的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆