操作失败,因为表“ Y”上已经存在名称为“ X”的索引或统计信息 [英] The operation failed because an index or statistics with name 'X' already exists on table 'Y'
问题描述
我是SQL的新手,我有一个SQL文件,执行该文件时会发生以下错误:
I am new to SQL and I have a SQL file and when I execute it the following error occurs:
Msg 1913,级别16,状态1,行463
操作失败,因为表'DAILY_DATA_TYPE'上已经存在名称为'DDT_PK'的索引或统计信息。
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
我正在使用Microsoft SQL Server Management Studio。
I am using Microsoft SQL Server Management Studio.
推荐答案
主键自动为 INDEX
。如果要创建 NONCLUSTERED
主键,请使用:
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]);
这篇关于操作失败,因为表“ Y”上已经存在名称为“ X”的索引或统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!