Click here to Skip to main content
Licence 
First Posted 18 Sep 2007
Views 20,226
Bookmarked 16 times

Diff cluster vs noncluster Index in sql

By | 18 Sep 2007 | Article
basi difference between cluster and non clusterd index
 
Part of The SQL Zone sponsored by
See Also

Introduction

In this article i am going to say about the way of creating index which are cluster and nonclusterd index,along with some description about both.

First create two table named
* clusterindex * nonclusterindex

code:

create table nonclusterindex(Emid int,data varchar(800))
create table clusterindex(Emid int,data varchar(800))

insert data to nonclusterindex in same order you can jest copy and past it

code:

insert into nonclusterindex values (4,replicate ('b',100))
insert into nonclusterindex values (1,replicate ('z',100))
insert into nonclusterindex values (2,replicate ('a',100))
insert into nonclusterindex values (0,replicate ('y',100))

In the same way insert data into clusterindex table

code:

insert into clusterindex values (4,replicate ('b',100))
insert into clusterindex values (1,replicate ('z',100))
insert into clusterindex values (2,replicate ('a',100))
insert into clusterindex values (0,replicate ('y',100))

create noncluster index for the table nonclusterindex

code:

create unique nonclustered index nonclusterindex_emid on nonclusterindex (Emid)


create cluster index for the table clusterindex

code:

create unique clustered index clusterindex_emid on clusterindex (Emid)


after that select clusterindex data

code:select * from clusterindex----------------the out put will be of sorted one

output:

0 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
1 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

you me see that out put is been sorted by Emid.

select nonclusterindex data from that table

code:
select * from nonclusterindex-------------the output will be nonsorted one

output:

4 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
1 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
0 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

this is the mager difference between two indexing
as clustered index output is sorted one,we can index only one row,
but we can index more than one row in nonclusterd indexing

we can also use primary key for unique key

by default index will be of nonclustered index type
like this eg..
--create unique index nonclusterindex_emid on nonclusterindex (Emid)

Know insert more data to it

code:

insert into nonclusterindex values (-1,replicate ('zz',100))

the output will be in same order as was inserted

In the same way insert data into clusterindex table

code:

insert into clusterindex values (-1,replicate ('zz',100))

the output will be as sorted one

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Balaji Manoharan

Web Developer

India India

Member

Hai this is Balaji from India,presently working as a software programmer in asp.net
using c# and vb.Net.
 
I am interested in sharing information among peoples ,I think this is one the way to spread my ideas.
I am alse have Knowledge in DataBase.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNic article. PinmemberMadhuri Patil2:25 10 Apr '12  
GeneralMy vote of 1 Pinmembermilesganesh19:18 4 May '11  
General..... Pinmemberrilov2:36 19 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 19 Sep 2007
Article Copyright 2007 by Balaji Manoharan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid