如何删除外键引用的唯一索引?

发布于 2024-08-15 17:28:15 字数 387 浏览 7 评论 0原文

我有一个表,我们将其称为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2024-08-22 17:28:15

为了删除外键引用的非聚集索引,您必须首先删除外键约束。

请查看 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

久伴你 2024-08-22 17:28:15

双索引方法是有意义的:

  • 第二个索引可能比聚集索引小得多,并且更容易装入内存
  • 第二个索引可能包含有利于特定查询的列的选择

要删除第二个索引,您将必须首先删除引用它的所有外键。这是我用来删除&的脚本重新创建外键。

The two-index approach can make sense:

  • The second index is probably much smaller than the clustered index, and would more easily fit into memory
  • The second index might include a selection of columns that benefit specific queries

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文