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

发布于 2024-09-16 14:09:04 字数 254 浏览 2 评论 0原文

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

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

create index index1 on mytable(firstcolumn)

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

create clustered index clusindex1 on mytable(cluscolumn)

Is it possible to convert a clustered index to non clustered index or
non clustered index to clustered index in sql server 2005.

Please convert this query into clustered index:

create index index1 on mytable(firstcolumn)

Please convert this query into non clustered index:

create clustered index clusindex1 on mytable(cluscolumn)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天邊彩虹 2024-09-23 14:09:04

意义远不止于此

创建聚集索引

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子句)

我会在开始删除和重新创建索引之前,请先了解一下在线图书中索引的工作原理

There is more to it than meets the eye

to create a clustered index

drop index mytable.clusindex1 
go

create clustered index clusindex1 on mytable(cluscolumn)

to create a non clustered index

drop index mytable.clusindex1 
go

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

having said that, you can only have one clustered index per table, so if you try to drop an index and recreate it as a clustered index it will fail if you already have a clustered index. Whenever you drop a clustered index all non clustered indexes will also be dropped and recreated pointing to the heap, and then again dropped and recreated when you create the clustered index, now pointing to the clustered index (look up the WITH DROP_EXISTING clause)

I would say lookup how indexing works in Books On Line before you start dropping and recreating indexes

素年丶 2024-09-23 14:09:04

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

drop index mytable.index1
go

create nonclustered index index1 on mytable (firstcolumn asc)
go

Those aren't queries; they are DDL commands.
Drop and recreate the indexes as desired, like so:

drop index mytable.index1
go

create nonclustered index index1 on mytable (firstcolumn asc)
go
若水般的淡然安静女子 2024-09-23 14:09:04

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

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

I also wanted to know whether a clustered index could be converted (altered) to be a non-clustered index. I don't believe this can be done. The existing clustered index has to first be dropped and then the new non-clustered index (possibly with the same name as the clustered index) has to be created. The same is true for converting a non-clustered index to a clustered index.

I have no idea why you're asking for the 'queries' to be converted, but @Tahbaza is correct in that the code you included in your question aren't really queries. They are T-SQL statements for making changes to 'data definitions' (i.e. the schema [structure] of your database).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文