Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Unique index in sql server 2005 Expand / Collapse
Author
Message
Posted Tuesday, April 14, 2009 9:56 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, July 5, 2013 12:58 AM
Points: 16, 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.
Post #696709
Posted Tuesday, April 14, 2009 10:37 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 5:49 AM
Points: 12,887, Visits: 31,833
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #696765
Posted Tuesday, April 14, 2009 11:21 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 25, 2010 6:09 AM
Points: 1,621, 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



Post #696817
Posted Tuesday, April 14, 2009 11:44 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:46 PM
Points: 42,462, Visits: 35,525
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 2008, MVP
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

Post #696843
Posted Tuesday, April 14, 2009 11:50 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 6:20 PM
Points: 33,078, Visits: 15,192
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
Post #696850
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse