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

nav*_*mar 4 sql sql-server clustered-index non-clustered-index

是否可以在sql server 2005中将聚簇索引转换为非聚簇索引或非聚簇索引转换为聚簇索引.

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

create index index1 on mytable(firstcolumn)
Run Code Online (Sandbox Code Playgroud)

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

create clustered index clusindex1 on mytable(cluscolumn)
Run Code Online (Sandbox Code Playgroud)

SQL*_*ace 5

它不仅仅是满足于眼睛

创建聚簇索引

drop index mytable.clusindex1 
go

create clustered index clusindex1 on mytable(cluscolumn)
Run Code Online (Sandbox Code Playgroud)

创建非聚集索引

drop index mytable.clusindex1 
go

create index clusindex1 on mytable(cluscolumn) --non clustered is default
Run Code Online (Sandbox Code Playgroud)

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

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


Tah*_*aza 3

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

drop index mytable.index1
go

create nonclustered index index1 on mytable (firstcolumn asc)
go
Run Code Online (Sandbox Code Playgroud)