• Primary key never affects the row order in data pages. It's the clustered index that does that. Now, by default the primary key is a clustered index, but that is not required.

    Why would you not want a primary key on a table with no clustered index?

    What SQL does when a page fills and needs to be split is to allocate a new page somewhere in the data file. Where is not important. It then adjusts the next and previous pointers of the original page so that the new one is linked in in the correct logical order. The physical order of the pages may well not correspond to the ogical order. That's fragmentation

    Let's say we have a table with 5 pages, with a clustered index (hence row order matters). (Under 8 pages, so mixed extents

    1st page - pageID 250, previous page pointer = null, next page pointer = 251

    2nd page - pageID 251, previous page pointer = 250, next page pointer = 252

    3rd page - pageID 252, previous page pointer = 251, next page pointer = 264

    4th page - pageID 264, previous page pointer = 252, next page pointer = 275

    5th page - pageID 275, previous page pointer = 264, next page pointer = null.

    Now, page 3 is full and needs, for whatever reason, to be split. SQL allocates a new page. Say everything below page 300 is used, so the new page is 301. Now the table looks like this.

    1st page - pageID 250, previous page pointer = null, next page pointer = 251

    2nd page - pageID 251, previous page pointer = 250, next page pointer = 252

    3rd page - pageID 252, previous page pointer = 251, next page pointer = 301

    4th page - pageID 301 (new), previous page pointer = 252, next page pointer = 264

    5th page - pageID 264, previous page pointer = 301, next page pointer = 275

    6th page - pageID 275, previous page pointer = 264, next page pointer = null.

    Only the pages before and after the new page have to change. It would take waaay too long to adjust all the pages in the table (imagine a table with a few hundred thousand pages)

    When you have a heap (no cluster) the order of rows has no meaning, and new rows are just added to the last page of the table.

    Does that help?

    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