将聚簇索引转换为非聚集索引?

时间:2010-08-29 17:32:05

标签: sql sql-server clustered-index non-clustered-index

是否可以将聚簇索引转换为非聚簇索引或 sql server 2005中非聚集索引到聚簇索引。

请将此查询转换为聚集索引:

create index index1 on mytable(firstcolumn)

请将此查询转换为非聚集索引:

create clustered index clusindex1 on mytable(cluscolumn)

3 个答案:

答案 0 :(得分:5)

除此之外还有更多的东西

创建聚簇索引

drop index mytable.clusindex1 
go

create clustered index clusindex1 on mytable(cluscolumn)

创建非聚集索引

drop index mytable.clusindex1 
go

create index clusindex1 on mytable(cluscolumn) --non clustered is default

话虽如此,每个表只能有一个聚簇索引,因此如果您尝试删除索引并将其重新创建为聚簇索引,则如果已有聚簇索引,则会失败。每当您删除聚簇索引时,所有非聚簇索引也将被删除并重新指向堆,然后在创建聚簇索引时再次删除并重新创建,现在指向聚簇索引(查找WITH DROP_EXISTING子句)

我会说在开始删除和重新创建索引之前,查找索引在Books On Line中的工作原理

答案 1 :(得分:3)

那些不是查询;它们是DDL命令。 根据需要删除并重新创建索引,如下所示:

drop index mytable.index1
go

create nonclustered index index1 on mytable (firstcolumn asc)
go

答案 2 :(得分:0)

我还想知道聚簇索引是否可以转换(更改)为非聚簇索引。我不相信这可以做到。必须首先删除现有的聚簇索引,然后必须创建新的非聚簇索引(可能与聚簇索引同名)。将非聚集索引转换为聚簇索引也是如此。

我不知道您为什么要求转换'查询',但@Tahbaza是正确的,因为您在问题中包含的代码不是真正的查询。它们是用于更改“数据定义”(即数据库的模式[结构])的T-SQL语句。