Solved

Primary Key - Clustered Index - SQL Server

Posted on 2010-08-24
4
528 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article I will describe the Backup & Restore 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now