Solved

Primary Key - Clustered Index - SQL Server

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

766 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