Primary key without cluster index

  • Hi,

    This is an interview question can we create a primary key on table without cluster index?

    I think it not possible.............Can we forceble ignore the cluster index while creating the primary key...

    Koteswarrao

  • Yes you can create

    ALTER TABLE dbo.RFP_NSO_SEND_QUEUE ADD CONSTRAINT

    PK_RFP_NSO_SEND_QUEUE_rfp_nso_send_queue_stub PRIMARY KEY NONCLUSTERED

    (

    rfp_nso_send_queue_stub

    ) WITH( FILLFACTOR = 75, PAD_INDEX = ON, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,

    ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [SECONDARYDATA]

    GO

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • The best way of learning interview questions is to give it a shot.

    Here's the relevant syntax from Books Online (from the article on CREATE TABLE):

    [ CONSTRAINT constraint_name ]

    { { PRIMARY KEY | UNIQUE }

    [ CLUSTERED | NONCLUSTERED ]

    [

    WITH FILLFACTOR = fillfactor

    | WITH ( < index_option > [ , ...n ] )

    ]

    [ ON { partition_scheme_name ( partition_column_name )

    | filegroup | "default" } ]

    | [ FOREIGN KEY ]

    REFERENCES [ schema_name . ] referenced_table_name [ ( ref_column ) ]

    [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]

    [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]

    [ NOT FOR REPLICATION ]

    | CHECK [ NOT FOR REPLICATION ] ( logical_expression )

    }

    Here's another fun question - what's the difference between a primary key and any other unique key, regardless of whether it's clustered or not?

  • y.koteswarrao-652921 (7/15/2010)


    This is an interview question can we create a primary key on table without cluster index?

    Yes.

    I think it not possible.............Can we forceble ignore the cluster index while creating the primary key...

    Primary key and clustered index are different concepts. Primary key is the row's identifier. Clustered index is an index with the data pages at the leaf level.

    Only relation is that, by default, the primary key is enforced by a unique clustered index. This is only by default, not by requirement.

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

    thanks a lot..........for your explanation...........iam clear about it.

    Koteswarrao.y

  • Hi buvensh,

    Thank for your reply............iam clear about with your reply....

    Koteswar rao

  • PK by default creates a unique clustered index if one doesn't already exists.

    If a clusterd index already exists, you can specify NON CLUSTERED clause to create a noon clustered index.

    Thank You,

    Best Regards.

    SQLBuddy

  • sqlbuddy123 (7/15/2010)


    PK by default creates a unique clustered index if one doesn't already exists.

    If a clusterd index already exists, you can specify NON CLUSTERED clause to create a noon clustered index.

    Thank You,

    Best Regards.

    SQLBuddy

    Well said , i missed this point

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • --soap box

    I think the principle issue with misconceptions is that people are using the Table Designer in Management Studio. They "click" they "key" icon which will create the PK as a clustered index. By knowing this and knowing the difference between PK and CL as mentioned above you can still use the "designer" to create/modify your tables, just use"Manage Index and Keys" instead of the "PK" icon.

    Personally I think people should be taught scripting objects first and the fundamental concepts of db design then when using the "designers" and "wizards" they will have a better grasp and will know exactly what they want when designing their objects.

    Somehow this is being lost in the curriculum of where ever people are learning about database concepts.

    --End of soap box

    Steve

  • Jim McLeod (7/15/2010)

    Here's another fun question - what's the difference between a primary key and any other unique key, regardless of whether it's clustered or not?

    PK allows NO nulls in key columns, where a Unique constraint will allow records that contain a null in the key columns (still must be unique, however).

  • Jim McLeod (7/15/2010)


    Here's another fun question - what's the difference between a primary key and any other unique key, regardless of whether it's clustered or not?

    A primary key cannot contain NULLs (as already mentioned).

    You can only have one primary key per table.

    A primary key is clustered by default.

    Unique indexes can be filtered (in SQL Server 2008).

    A unique index can be altered.

    A unique index can be created on a view.

    There's probably lots of other differences too.

  • Sorry to be the kiss @$$ looking for an "A" 😀

    but only non-clustered indexes can be filtered

    Creates a filtered index by specifying which rows to include in the index. The filtered index must be a nonclustered index on a table. Creates filtered statistics for the data rows in the filtered index.

    http://msdn.microsoft.com/en-us/library/ms188783.aspx

  • Bradley B (7/20/2010)


    Sorry to be the kiss @$$ looking for an "A" 😀

    but only non-clustered indexes can be filtered

    A unique index can be filtered, providing it's a nonclustered index. Neither a unique constraint nor a primary key can be, regardless of what kind of index enforces them.

    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
  • Yeah okay back down to a B, but heck you only need a C average to be president

    I was waiting for it after I re-read and saw it was unique and not clustered. Agreed though you can have a unique non-clustered filtered index, but you could not have a filtered clustered index (primary key or no primary key)

  • Bradley B (7/20/2010)


    I was waiting for it after I re-read and saw it was unique and not clustered.

    I knew what I meant 😛 but yes, I should have said "non-clustered" to avoid ambiguity.

    Agreed though you can have a unique non-clustered filtered index, but you could not have a filtered clustered index (primary key or no primary key)

    Seeing as I am now in picky mode (you started it) I'm going to say that a filtered clustered index is possible (in a sense) just create it on a view that contains the filter condition 😀

Viewing 15 posts - 1 through 15 (of 19 total)

You must be logged in to reply to this topic. Login to reply