• sameerchachad 69959 (3/9/2010)


    A Primary Key is always enforced by a clustered index at creation?

    Correct answer (SQL Server Central) : False

    I would just like to add a small point, whenever you create a Primary Key, by default its ALWAYS going to be Clustered. Unless you create a Non Clustered Index Primary Key.

    So, when you are saying that IF a Clustered Index already exists, then your what you are mentioning is valid. Else the Primary key, which is usually created at when you are creating the table, would be Clustered index unless specified.

    Kindly reply if there is something else to add or I haev misquoted anywhere.

    Warm regards,

    Sameer Chachad.

    The answer given by Paul is correct but incomplete. The explanation correctly describes what happens if you do not specify a clustering option for the PRIMARY KEY constraint, but it doesn't include the extra possibilities that arise from adding the keyword CLUSTERED or NONCLUSTERED to the constraint.

    These are the scenarios that are possible when a PRIMARY KEY constraint is created:

    1) No clustered index exists for the table, no clustering option specified for the constraint --> Clustered index is created. (This is probably the most common scenario)

    2) No clustered index exists for the table, CLUSTERED option specified for the constraint --> Clustered index is created.

    3) No clustered index exists for the table, NONCLUSTERED option specified for the constraint --> Non-clustered index is created.

    4) A clustered index already exists for the table, no clustering option specified for the constraint --> Non-clustered index is created.

    5) A clustered index already exists for the table, CLUSTERED option specified for the constraint --> No index created; error 1902 raised; constraint not created.

    6) A clustered index exists for the table, NONCLUSTERED option specified for the constraint --> Non-clustered index is created.

    Still, a great question. I really hope that Paul and Kimberly continue to submit questions, even if they don't have a seminar to promote.

    (edit: small but important typo in option 6 - thanks, Shaiju C.K., for alerting me to it)


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/