April 14, 2009 at 9:56 am
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.
April 14, 2009 at 10:37 am
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
April 14, 2009 at 11:21 am
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
April 14, 2009 at 11:44 am
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
April 14, 2009 at 11:50 am
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.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply