[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Primary Key - Clustered Index - SQL Server

Posted on 2010-08-24
4
Medium Priority
?
599 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 668 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 664 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 668 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

656 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