Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert a non-clustered index on a Primary Key to a clustered one? (SQL Server 2008)

With Management Studio I can't alter the index. Dropping it doesn't work because it's being used as a foreign key in a lot of other tables. Can I somehow alter it? Or how would you do this?

like image 963
Lieven Cardoen Avatar asked Mar 12 '10 21:03

Lieven Cardoen


People also ask

How can we change non clustered primary key to clustered primary key?

Taking all this into consideration, you cannot alter an index to make it clustered. However, you can create a new one so long as one does not already exist. Either that or drop the clustered index, create your new clustered index and then create your old clustered index as a non clustered index.

Can you create a table with primary key not as the clustered index?

Yes you can, by specifying the primary key be nonclustered. ALTER TABLE TableName ADD CONSTRAINT PK_name PRIMARY KEY NONCLUSTERED (ID); You make another index clustered by specifying a clustered index.

Can we create clustered index on primary key?

When you create a PRIMARY KEY constraint, a unique clustered index on the column or columns is automatically created if a clustered index on the table does not already exist and you do not specify a unique nonclustered index. The primary key column cannot allow NULL values.


2 Answers

If your existing PK is referenced by many other tables then you're going to spend many tedious and error-prone minutes writing the script to drop all the FK references and recreate them.

SQL Server Management Studio can do this for you. What you may not have realized is that you can only have one clustered index on a table, because the clustered index represents the physical ordering of rows; this means that you first have to bring up the clustered index and turn off clustering. Then and only then can you pull up another index and turn on clustering.

You need to do this from the table designer, then right-click and choose Indexes/Keys.... First find the existing clustered index (probably the primary key) and change Create as Clustered to No. Then go to the other index and change Create as Clustered to Yes for that. If the table is large, the operation is liable to time out while you save; you can get around this by having SSMS generate a change script (right-click on the designer after changing the indexes and you'll see the option). Then you can run this script in a query window with no timeout.

If you look at this change script, you'll see all of the work it's doing creating staging tables and switching keys around; it's a pain to write this manually. Let SSMS do it for you.

like image 181
Aaronaught Avatar answered Oct 13 '22 22:10

Aaronaught


You can't convert it in-place - you need to drop the primary key constraint first (which will also automatically drop the nonclustered index "behind" the primary key constraint), and then re-create it as a clustered index:

ALTER TABLE dbo.YourTable
DROP CONSTRAINT PK_YourTable

and then re-create it as clustered:

ALTER TABLE dbo.YourTable
ADD CONSTRAINT PK_YourTable
PRIMARY KEY CLUSTERED (YourPKField)
like image 42
marc_s Avatar answered Oct 13 '22 23:10

marc_s