2016-05-12 164 views
0

我是SQL新手,正在使用Microsoft SQL Server练习一些练习。我创建了这个索引,然后我试图删除这个索引,但是它说这不是一个约束。但custID栏显示它是主键。尝试删除索引时出错

这是我做过什么:

CREATE INDEX indexcustInfoID ON tblCustomerIDInfo (CustID) 

ALTER TABLE tblCustomerIDInfo 
DROP CONSTRAINT indexcustInfoID 

错误:

Msg 3728, Level 16, State 1, Line 26
'indexcustInfoID' is not a constraint.

Msg 3727, Level 16, State 0, Line 26
Could not drop constraint. See previous errors.

请帮帮我!

+2

你并不需要在主键添加一个索引 - 一个将被自动创建。 –

+0

哦,我知道了。我正在关注一个他们使用MySQL的视频,这个人也是这么做的,但我使用的是sql server。创建和删除索引时,两者是否有区别? – Gracie

回答

2

想要DROP INDEX而不是DROP CONSTRAINTDROP CONSTRAINT仅适用于为主键和UNIQUE约束创建的索引。这听起来像你已经在主键列上添加了另一个索引,这不是主键约束本身。

MSDN

The DROP INDEX statement does not apply to indexes created by defining PRIMARY KEY or UNIQUE constraints. To remove the constraint and corresponding index, use ALTER TABLE with the DROP CONSTRAINT clause.