无法删除SQL Server中的索引

发布于 2025-02-06 00:41:24 字数 2145 浏览 2 评论 0原文

当用户激活FK索引删除选项时,为其生成了DDL,并且激活PK索引删除选项时,为此应用程序创建了DDL。

在下面的示例中创建了两个表。

CREATE TABLE [dbo].[TABLE3] 
(
    [COL] [VARCHAR](20) NOT NULL
)
GO

ALTER TABLE [dbo].[TABLE3]
    ADD CONSTRAINT [PK_TABLE3]
        PRIMARY KEY NONCLUSTERED ([COL] ASC)
GO

CREATE TABLE [dbo].[TABLE4] 
(
    [COL] [VARCHAR](20)
)
GO

ALTER TABLE [dbo].[TABLE4]
    ADD CONSTRAINT [FK_TABLE3_TO_TABLE4]
        FOREIGN KEY ([COL]) REFERENCES [dbo].[TABLE3] ([COL])
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
GO

如果选择“删除FK约束”选项和删除FK索引选项 生成以下语法。

ALTER TABLE [dbo].[TABLE4]
    DROP CONSTRAINT IF EXISTS [FK_TABLE3_TO_TABLE4]
GO

ALTER TABLE [dbo].[TABLE3]
    DROP CONSTRAINT IF EXISTS [PK_TABLE3]
GO

DROP INDEX [dbo].[TABLE3].[PK_TABLE3]
GO

此处的Drop索引语法发生错误。

sql错误[3701] [S0007]:索引'dbo.table3.pk_table3'不存在或您没有许可将其丢弃。

我的问题是:

  1. 如果SQL Server删除了约束,也会删除索引吗?

  2. drop index语句可以使用如果存在以防止错误,但是我知道如果存在,则来自SQL Server 2014或2016。

。需要支持的2005年版本的开发应用程序,如果存在语法似乎并不是一个明确的解决方案,则需要支持

请告知该怎么做。

对于每个版本的SQL Server的文档,请参阅以下链接。

SQL Server 2005: https://learn.microsoft.com/en-us/previous-versions/sql/sql/sql-server-2005/ms176118(v = sql.90)

sql Server 2008:

sql Server 2012: https://learlen.microsoft.com/en-com/en-us/en-us/previous/previous-versions/previous-versions/sql/sql/sql/sql/sqql -Server-2012/MS176118(V = SQL.110)

When the user activates the FK index removal option, DDL is generated for it, and when the PK index removal option is activated, the DDL is created for this application.

Two tables were created with the example below.

CREATE TABLE [dbo].[TABLE3] 
(
    [COL] [VARCHAR](20) NOT NULL
)
GO

ALTER TABLE [dbo].[TABLE3]
    ADD CONSTRAINT [PK_TABLE3]
        PRIMARY KEY NONCLUSTERED ([COL] ASC)
GO

CREATE TABLE [dbo].[TABLE4] 
(
    [COL] [VARCHAR](20)
)
GO

ALTER TABLE [dbo].[TABLE4]
    ADD CONSTRAINT [FK_TABLE3_TO_TABLE4]
        FOREIGN KEY ([COL]) REFERENCES [dbo].[TABLE3] ([COL])
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
GO

If you select both the Delete FK Constraints option and the Delete FK Index option
The following syntax is generated.

ALTER TABLE [dbo].[TABLE4]
    DROP CONSTRAINT IF EXISTS [FK_TABLE3_TO_TABLE4]
GO

ALTER TABLE [dbo].[TABLE3]
    DROP CONSTRAINT IF EXISTS [PK_TABLE3]
GO

DROP INDEX [dbo].[TABLE3].[PK_TABLE3]
GO

An error occurs for the DROP INDEX syntax here.

SQL Error [3701] [S0007]: Index 'dbo.TABLE3.PK_TABLE3' does not exist or you do not have permission to drop it.

My question is:

  1. If SQL Server removes the constraint, will the index be removed as well?

  2. The DROP INDEX statement can use IF EXISTS to prevent an error, but I know that IF EXISTS is supported from SQL Server 2014 or 2016.

Since the 2005 version of the application under development needs to be supported, the IF EXISTS syntax does not seem to be a clear solution.

Please advise on what to do.

For documentation for each version of SQL Server, refer to the following links.

SQL Server 2005: https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2005/ms176118(v=sql.90)

SQL Server 2008: https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2008/ms176118(v=sql.100)

SQL Server 2012: https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2012/ms176118(v=sql.110)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我只土不豪 2025-02-13 00:41:24

尝试以下代码:

ALTER TABLE dbo.TABLE4 DROP CONSTRAINT FK_TABLE3_TO_TABLE4
GO
ALTER TABLE dbo.TABLE3 DROP CONSTRAINT PK_TABLE3
Go

您可以使用以下代码在数据库中获取约束名称列表:

SELECT TABLE_NAME,
       CONSTRAINT_TYPE,CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS

SELECT TABLE_NAME,
       CONSTRAINT_TYPE,CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE LOWER(TABLE_NAME) <> N'sysdiagrams'
table_nameconstraint_typedeped_name
table3primarypk_table3
table4 forefer3_table3_to_table3_to_table3_to_table4table3

Try following code:

ALTER TABLE dbo.TABLE4 DROP CONSTRAINT FK_TABLE3_TO_TABLE4
GO
ALTER TABLE dbo.TABLE3 DROP CONSTRAINT PK_TABLE3
Go

You can use following code to get you constraints name list in your database:

SELECT TABLE_NAME,
       CONSTRAINT_TYPE,CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS

OR

SELECT TABLE_NAME,
       CONSTRAINT_TYPE,CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE LOWER(TABLE_NAME) <> N'sysdiagrams'
TABLE_NAMECONSTRAINT_TYPECONSTRAINT_NAME
TABLE3PRIMARY KEYPK_TABLE3
TABLE4FOREIGN KEYFK_TABLE3_TO_TABLE4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文