如何删除外键引用的唯一索引?
我有一个表,我们将其称为Users
。该表有一个名为 Id
的主键。尽管将 Id
作为主键(唯一聚集),但它在同一列(Id
)上有其他索引(唯一非聚集)。
我想删除此约束,但外键引用此唯一的非聚集索引,并且出现 Theconstraint ... is being referenced by table...
错误。
删除此类索引的最佳方法是什么?您是否有任何脚本可以在特定表的特定列上删除、执行某些操作并重新创建外键?有很多外键,所以如果我能自动完成就好了。我可以使用 INFORMATION_SCHEMA 和其他系统对象来提取有关这些键的信息,但我不想写已经写过的内容或可以以其他方式完成的内容。
I have a table, let's call it Users
. This table has primary key called Id
. Despite having Id
as primary key (unique clustered), it has other index (unique nonclustered) on the same column(Id
).
I would like to drop this constraint, but foreign keys reference this unique nonclustered index and I get The constraint ... is being referenced by table...
error.
What is the best way to drop such index? Do you have any scripts that drop, do something, and recreate foreign key on specific column in specific table? There is a lot of foreign keys, so it would be nice if I could do it automatically. I could use INFORMATION_SCHEMA and other system object to extract information about these keys, but I don't want to write, what have already been written or can be done in other way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了删除外键引用的非聚集索引,您必须首先删除外键约束。
请查看 SQL Server Central 上的海报中提供的以下脚本。它们可能需要针对您的“确切”要求进行一些调整,但是它们提供了编写脚本并随后重建外键的基础。
抄写外键
In order to drop a nonclustered index that is referenced by a foreign key you must furst drop the foreign key constraint.
Take a look at the following scrips available from a poster over at SQL Server Central. They may require some tweaking for your "exact" requirements however they provide the basis for scripting out and then subsequently rebuilding foreign keys.
Scrip out Foreign Keys
双索引方法是有意义的:
要删除第二个索引,您将必须首先删除引用它的所有外键。这是我用来删除&的脚本重新创建外键。
The two-index approach can make sense:
For dropping the second index, you'll have to drop all foreign keys that refer to it first. Here is a link to the script I use to drop & recreate foreign keys.