The operation failed because an index or statistics with name 'X' already exists on table 'Y' [duplicate]

[亡魂溺海] 提交于 2019-12-11 16:05:37

问题


I am new to SQL and I have a SQL file and when I execute it the following error occurs:

Msg 1913, Level 16, State 1, Line 463
The operation failed because an index or statistics with name 'DDT_PK' already exists on table 'DAILY_DATA_TYPE'.

The error occurs when running this statement:

CREATE UNIQUE NONCLUSTERED INDEX DDT_PK 
    ON [DATE_DATA_TYPE]([TYPE_ID])
GO

ALTER TABLE [DATE_DATA_TYPE] 
    ADD CONSTRAINT [DDT_PK] PRIMARY KEY ([TYPE_ID])
GO

I know that it already exists but I want to add a constraint and not create the index again so what am I doing wrong?

Maybe u have to know how the table was created too so here is the statement:

CREATE TABLE [DATE_DATA_TYPE] ([TYPE_ID] [numeric](18, 0) NOT NULL,
[TYPE_NAME] [varchar](400) NOT NULL, [UNIT] [varchar](32) NOT NULL,
[CHART_TYPE] [varchar](32) NOT NULL, [RES_ID] [numeric](18, 0) NOT NULL)
GO

I am using Microsoft SQL Server Management Studio.


回答1:


A Primary Key is automatically an INDEX. If you want to create a NONCLUSTERED Primary Key then use:

ALTER TABLE [DATE_DATA_TYPE]
ADD CONSTRAINT [DDT_PK] PRIMARY KEY NONCLUSTERED ([TYPE_ID]);


来源:https://stackoverflow.com/questions/50911280/the-operation-failed-because-an-index-or-statistics-with-name-x-already-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!