6

The limit of my understanding how a database stores a primary key is a btree based on the clustered key with each node storing the rest of the columns.

I have not updated this understanding since university last century and would love to know how SQL Server stores a composite primary key compared to a single primary key.

Can anyone help point me to some detail on this?

2
  • 2
    I don't have time to give a proper answer now but will just mention that there are actually 3 different possibilities to consider here (1) The PK is the clustered index (2) The PK is a non clustered index on a heap (3) The PK is a non clustered index on a table with another clustered index. Aug 2, 2010 at 0:38
  • So the clustered index is the storage, not the PK. I guess I had not think of these items as synonymous anymore.
    – Nat
    Aug 2, 2010 at 3:47

1 Answer 1

5

A composite key is exactly the same as a ordinary, single column, key, but is longer and composed of multiple values. Consider you have a B-Tree on a single column, say A. The non-leaf pages contains slots with values of A column, and pointers to leaf-pages. The leaf pages contain slots with the column A values (the key), followed by all the other columns of the row. A composite key is exactly the same, but the values in the slots will be the composite values, in the order they are declared in the key.

There is a great description of the internals of a SQL Server data page at Anatomy of a Page. Also, the Chapter 6 from Kelan Delaney's old SQL 7.0 book is available online on Technet: Tables. Is still a great resource, the basics still apply to SQL 2008 R2 (things that are changed are mostly around max types and compression settings, but the bulk of info is still valid).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.