Unique index in sql server 2005

  • 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.

  • 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!

  • 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

  • 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
  • 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