SQL Server Developer Center >
SQL Server Forums
>
SQL Server Database Engine
>
creating a clustered index
creating a clustered index
I created a primary key on a table with the default options. I want to create an index with clustered index I have to drop the primary key and reccreate a nonclustered index and alter it to clustered index. Why can't I create an clustered index after dropping the index? Why should I creaete nonclustered index first and alter it?
Answers
- If you declared a primary key, you already have a clustered index.
Phil Brammer | http://www.ssistalk.com | Twitter: http://twitter.com/PhilBrammer- Marked As Answer byXiao-Min Tan – MSFTMSFT, ModeratorTuesday, November 10, 2009 2:18 AM
- You pretty much answered your own question.If you create a primary key using default options it will default to creating it as a clustered key.Create your primary key as a non-clustered key then you can create a separate clustered index:
alter table dbo.test add constraint pk_test primary key nonclustered (col1, col2)
ajmer dhariwal || eraofdata.com- Marked As Answer byXiao-Min Tan – MSFTMSFT, ModeratorTuesday, November 10, 2009 2:18 AM
Phil,
Are you saying that if I create a table with a primary key a clustered index will be automatically created?
Don't I have to create a clustered index separately through an explicit create index command?
When you create a table and define column(s) as PRIMARY KEY, a clustered index is automatically created.
See the BOL page for CREATE TABLE: http://msdn.microsoft.com/en-us/library/ms174979.aspx (Specifically the options for PRIMARY KEY and CLUSTERED, where it states that CLUSTERED indexes are the default for PRIMARY KEYs)
Phil Brammer | http://www.ssistalk.com | Twitter: http://twitter.com/PhilBrammer- Marked As Answer byXiao-Min Tan – MSFTMSFT, ModeratorTuesday, November 10, 2009 2:18 AM
- Yes that's correct, just run the below query on your table you can see Cluster index in the index description column.
sp_helpindex TableName
- Marked As Answer byXiao-Min Tan – MSFTMSFT, ModeratorTuesday, November 10, 2009 2:18 AM
All Replies
- If you declared a primary key, you already have a clustered index.
Phil Brammer | http://www.ssistalk.com | Twitter: http://twitter.com/PhilBrammer- Marked As Answer byXiao-Min Tan – MSFTMSFT, ModeratorTuesday, November 10, 2009 2:18 AM
- You pretty much answered your own question.If you create a primary key using default options it will default to creating it as a clustered key.Create your primary key as a non-clustered key then you can create a separate clustered index:
alter table dbo.test add constraint pk_test primary key nonclustered (col1, col2)
ajmer dhariwal || eraofdata.com- Marked As Answer byXiao-Min Tan – MSFTMSFT, ModeratorTuesday, November 10, 2009 2:18 AM
- Phil,
Are you saying that if I create a table with a primary key a clustered index will be automatically created?
Don't I have to create a clustered index separately through an explicit create index command? Phil,
Are you saying that if I create a table with a primary key a clustered index will be automatically created?
Don't I have to create a clustered index separately through an explicit create index command?
When you create a table and define column(s) as PRIMARY KEY, a clustered index is automatically created.
See the BOL page for CREATE TABLE: http://msdn.microsoft.com/en-us/library/ms174979.aspx (Specifically the options for PRIMARY KEY and CLUSTERED, where it states that CLUSTERED indexes are the default for PRIMARY KEYs)
Phil Brammer | http://www.ssistalk.com | Twitter: http://twitter.com/PhilBrammer- Marked As Answer byXiao-Min Tan – MSFTMSFT, ModeratorTuesday, November 10, 2009 2:18 AM
- Yes that's correct, just run the below query on your table you can see Cluster index in the index description column.
sp_helpindex TableName
- Marked As Answer byXiao-Min Tan – MSFTMSFT, ModeratorTuesday, November 10, 2009 2:18 AM


