Solved

Primary Key - Clustered Index - SQL Server

Posted on 2010-08-24
4
565 Views
Last Modified: 2012-05-10
Hi,

I thought that when you create a primary key, clustered index is automatically created. Am i correct or wrong ? But here is what i have got:

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.

Please explain me on detail manner whether i am correct or wrong. Provide simple examples to understand it better.

I did not follow this statement: and you do not specify a unique nonclustered index => Please explain in detail with example.

Thanks
0
Comment
Question by:milani_lucie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 167 total points
ID: 33517678
-- specifically stated as nonclustered
CREATE TABLE TEST1 ( ID INT NOT NULL PRIMARY KEY NONCLUSTERED )

-- not stated, so it will default to clustered
CREATE TABLE TEST2 ( ID INT NOT NULL PRIMARY KEY )


CREATE TABLE TEST3 ( ID INT NOT NULL)

-- not stated, so it will default to clustered
Alter table Test3 add constraint pk_test1 primary key(ID)
     or
-- specifically stated as nonclustered
Alter table Test3 add constraint pk_test1 primary key nonclustered(ID)


CREATE TABLE TEST4 ( ID INT NOT NULL, X INT NOT NULL UNIQUE CLUSTERED)

-- the following will not be clustered because Test4 already has a clustered index
Alter table Test4 add constraint pk_test1 primary key(ID)

-- the following won't even work
Alter table Test4 add constraint pk_test1 primary key clustered(ID)
0
 
LVL 7

Assisted Solution

by:rashmi_vaghela
rashmi_vaghela earned 166 total points
ID: 33517956
0
 
LVL 5

Expert Comment

by:ThakurVinay
ID: 33518811
When you create a primary key unique cluster index automatically creates but it is not necessory you can create a primary key with out unique cluster index.

if you try to create primary key with GUI you will understand it better.

HTH
Vinay
0
 
LVL 22

Assisted Solution

by:dportas
dportas earned 167 total points
ID: 33524987
No a clustered index is not ncessarily created automatically. If you don't specify the type of index for the PRIMARY KEY column(s) then a clustered index will be created unless a clustered index is specified for some other column(s).
If you want to be sure, then it's best to specify whether you want a CLUSTERED or NONCLUSTERED index. The primary key isn't necessarily the best choice for a clustered index.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question