【发布时间】:2020-03-02 12:17:21
【问题描述】:
我正在一个表上创建聚集索引,如果它已经存在则删除。
我正在使用这个查询。
DROP INDEX IF EXISTS CLX_Enrolment_StudentID_BatchID
ON Enrollment
CREATE INDEX CLX_Enrolment_StudentID_BatchID
ON Enrollment(Studentid, BatchId ASC);
现在,我想知道这里创建了哪个集群:- 这是集群还是非集群?
CREATE INDEX CLX_Enrolment_StudentID_BatchID
ON Enrollment(Studentid, BatchId ASC);
因为使用:-
DROP clustered INDEX IF EXISTS CLX_Enrolment_StudentID_BatchID
ON Enrollment
我收到此错误:-
Incorrect syntax near the keyword 'clustered'.
而且,如果我使用:-
DROP INDEX IF EXISTS CLX_Enrolment_StudentID_BatchID
ON Enrollment
go
CREATE clustered INDEX CLX_Enrolment_StudentID_BatchID
ON Enrollment(Studentid, BatchId ASC);
我收到此错误:-
Cannot create more than one clustered index on table 'Enrollment'. Drop the existing clustered index 'PK__enrollme__DE799CE1E4649295' before creating another.
现在,我想知道在我的第二个查询中创建了哪个索引。而且,如果两者都没有被创建,那么如何删除聚集索引(如果存在)并创建一个新索引。
【问题讨论】:
-
由于现有的聚集索引是主键索引,在创建新的聚集索引之前需要使用
ALTER TABLE...DROP CONSTRAINT和ALTER TABLE...ADD CONSTRAINT将现有的主键索引改为非聚集索引。跨度>
标签: sql sql-server-2008 indexing