5

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

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

create index index1 on mytable(firstcolumn)

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

create clustered index clusindex1 on mytable(cluscolumn)
4

3 回答 3

6

它的意义远不止眼前

创建聚集索引

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 中查找索引是如何工作的

于 2010-08-29T17:48:58.470 回答
3

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

drop index mytable.index1
go

create nonclustered index index1 on mytable (firstcolumn asc)
go
于 2010-08-29T17:37:54.753 回答
-1

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

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

于 2012-04-20T15:55:22.783 回答