Why does primary key order matter?

composite-primary-key entity-framework sql-server

Question

In a recent EntityFramework project, I created a class and assigned a few of its members as composite keys.

But when it was time to generate the database from this, an error occurred.

Unable to determine composite primary key ordering for type 'NNNNN'. Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys.

Although I am aware of the solution, I was simply curious as to why the order was important. A composite primary key doesn't require any particular ordering; it is simply a set of columns on which to key the data.

1
7
5/23/2013 11:53:16 AM

Accepted Answer

It matters because the database's primary key ordering is important. It makes a significant difference whether the key is organized as First Name, Last Name, SSN or SSN, Last Name, First Name since primary keys are (often) clustered and always indices. The PK is the only index on the table, thus if I only know the SSN when I query it, I'll get GREAT speed with the latter and a full table scan with the former.

9
5/23/2013 12:10:59 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow