开发者

converting clustered index into non-clustered index?

开发者 https://www.devze.com 2023-01-14 02:59 出处:网络
Is it possible to convert a clustered index to non clustered index or non clustered index to clustered index in sql server 2005.

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 cl开发者_StackOverflowustered index:

create clustered index clusindex1 on mytable(cluscolumn)


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


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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消