无法删除SQL Server中的索引
当用户激活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'不存在或您没有许可将其丢弃。
我的问题是:
如果SQL Server删除了约束,也会删除索引吗?
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 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:
If SQL Server removes the constraint, will the index be removed as well?
The
DROP INDEX
statement can useIF EXISTS
to prevent an error, but I know thatIF 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下代码:
您可以使用以下代码在数据库中获取约束名称列表:
或
Try following code:
You can use following code to get you constraints name list in your database:
OR