SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Unique index in sql server 2005


Unique index in sql server 2005

Author
Message
mauryakamal
mauryakamal
Old Hand
Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)

Group: General Forum Members
Points: 362 Visits: 191
Is it unique index is cluster index or non cluster index?

If we define the unique index column not null then what type of the index.

If unique index column is null then what type of the index???

Please elaborate about the unique index.

Thanks in advance.
Lowell
Lowell
SSC Guru
SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)

Group: General Forum Members
Points: 139327 Visits: 41521
a unique index is just that: a constraint and index that prevents duplicate values.

unique means you can't have 2 nulls in the table., one NULL max.

it is up to you to decide whether it is a clustered index or not when you create it; the clustered index decides in what order the data will be stored, so queries using that column in the WHERE statemetn will be the fastest:

CREATE TABLE TBSTATE(STATEID INT,STATECODE CHAR(2) )
--both of these are valid
ALTER TABLE TBSTATE ADD CONSTRAINT UQ_STATECODE UNIQUE CLUSTERED(STATECODE)
ALTER TABLE TBSTATE ADD CONSTRAINT UQ_STATECODE UNIQUE NONCLUSTERED(STATECODE)

--or
ALTER TABLE TBSTATE ADD CONSTRAINT UQ_STATEID UNIQUE CLUSTERED(STATEID )
ALTER TABLE TBSTATE ADD CONSTRAINT UQ_STATEID UNIQUE NONCLUSTERED(STATEID )


so if most searches on this simple table are on STATECODE, it makes sense to put the clustered index on that....but if most searches are on STATEID, then the clusted index belongs on that instead.

does that help a bit?

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Vijaya Kadiyala
Vijaya Kadiyala
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4427 Visits: 409
mauryakamal (4/14/2009)
1) Is it unique index is cluster index or non cluster index?

2) If we define the unique index column not null then what type of the index.

3) If unique index column is null then what type of the index???

Please elaborate about the unique index.

Thanks in advance.


Answer to Question 1) ==> it can be either way. When you create a primary Key it creates the Unique Clustered Index. When you create Unique Key it creates the Non-Clustered Index.

Answer to Question 2) ==> Non-Clustered Index.

Answer to Question 3) ==> Non-Clustered Index.

Check out the below link on Unique Index
http://msdn.microsoft.com/en-us/library/ms187019.aspx

Thanks -- Vijaya Kadiyala
www.dotnetvj.com
SQL Server Articles For Beginers



GilaMonster
GilaMonster
SSC Guru
SSC Guru (417K reputation)SSC Guru (417K reputation)SSC Guru (417K reputation)SSC Guru (417K reputation)SSC Guru (417K reputation)SSC Guru (417K reputation)SSC Guru (417K reputation)SSC Guru (417K reputation)

Group: General Forum Members
Points: 417962 Visits: 47141
The answer to all three is whatever you specify, nonclustered by default.

ALTER TABLE ... ADD CONSTRAINT ... UNIQUE CLUSTERED -- creates a clustered index to support the unique constraint. Will fail if there's an existing clustered index
ALTER TABLE ... ADD CONSTRAINT ... UNIQUE NONCLUSTERED -- creates a nonclustered index



Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Steve Jones
Steve Jones
SSC Guru
SSC Guru (251K reputation)SSC Guru (251K reputation)SSC Guru (251K reputation)SSC Guru (251K reputation)SSC Guru (251K reputation)SSC Guru (251K reputation)SSC Guru (251K reputation)SSC Guru (251K reputation)

Group: Administrators
Points: 251402 Visits: 19818
Is there something you are concerned about with an application or are you just trying to learn?

Perhaps we have some advice if you have an issue.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search