SQL Server primary and clustered index keys

Sometimes I find it helpful to go ‘back to basics’ and review fundamentals that I assume I know. In doing that I might discover something new, or reinforce an understanding that was not as strong as I thought.

Most of the applications we build as developers need some kind of data store. Often this is a relational data store, or an RDBMS (Relational database management system). Microsoft SQL Server, and more recently its cloud-hosted version Azure SQL Database are the RDBMS I’ve had the most experience with. In these posts when I say ‘SQL Server’ assume that also applies to Azure SQL Database (unless I mention otherwise).

SQL Server logo

I would not describe myself as a data expert. I think I know just enough to realise how much I don’t know! I’ve both worked with experts, and heard experts present at user groups and conferences enough to recognise genuine deep data expertise. But that doesn’t stop me wanting to learn more, and so here we go…

Most RDBMSs store data in tables. In SQL Server, it is most common for tables to store their data in a clustered index. The key values of the clustered index determine the sort order of the rows in the index, and therefore data rows for the table (though you should always use ORDER BY in your queries to guarantee the order of query results).

Here’s the definition of an index from the SQL Server documentation:

An index is an on-disk structure associated with a table or view that speeds retrieval of rows from the table or view. An index contains keys built from one or more columns in the table or view.

For some reason, in my mind tables and indexes were two quite separate things - probably because you often refer to a table having indexes. But in SQL Server at least, when your table has a clustered index, that index is actually where all the rows of the table are really stored.

If a table has no clustered index, then its rows are stored in an unordered structure called (rather descriptively!) a ‘heap’. Most of the time having a ‘heap’ is definitely not ‘heaps good’!

Primary key != clustered index

Don’t be caught conflating primary keys (or more correctly a ‘primary key constraint’) with clustered indexes, or just focus on primary keys and completely overlook clustered indexes.

This confusion is understandable. When you create a primary key on a table, if there is no existing clustered index then one will be created for you automatically by default.

CREATE TABLE dbo.Customers (
    CustomerId INT IDENTITY(1,1) NOT NULL,
    Name NVARCHAR(100) NOT NULL,
    CONSTRAINT PK_Customers PRIMARY KEY (CustomerId)
);

The dbo.Customers table has a CustomerId column with a primary key constraint. A clustered index is created by default. In fact if we use tooling to generate a ‘create script’ for this table, it will show the following:

 CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED

We didn’t originally include the CLUSTERED keyword, but it is effectively there.

You can have a primary key on column(s) that are in a non-clustered index, and have a separate clustered index keyed on different column(s). It’s less common, but perfectly valid. In this case you do need to include the NONCLUSTERED keyword. eg.

CREATE TABLE dbo.Customers (
    CustomerId INT IDENTITY(1,1) NOT NULL,
    Name NVARCHAR(100) NOT NULL,
    CONSTRAINT PK_Customers
        PRIMARY KEY NONCLUSTERED (CustomerId)
);
GO
CREATE CLUSTERED INDEX IX_Customers_Name
     ON dbo.Customers (Name);

Impact of primary keys

Primary keys uniquely identify each row in a table.

Once a table has a primary key, then another table can refer to that primary key using a Foreign key constraint. This is a way of enforcing data integrity (often known as ‘referential integrity’).

Every foreign key that links to the primary key will have the same type of column (or columns) that the primary key does. So however many bytes are needed for the primary key will also need to be allowed for each foreign key.

CREATE TABLE dbo.Orders (
    OrderId INT IDENTITY(1,1) NOT NULL,
    CustomerId INT NOT NULL,
    CONSTRAINT PK_Orders PRIMARY KEY (OrderId),
    CONSTRAINT FK_Orders_Customers
        FOREIGN KEY (CustomerId) REFERENCES dbo.Customers (CustomerId)
);

In the dbo.Orders table, CustomerId column is configured as a foreign key to the dbo.Customers table, and must refer to an existing primary key from that table.

Impact of keys of clustered index

Every non-clustered index will automatically include the clustered index key - you don’t need to explicitly include it in the index definition. So however many bytes are needed for the clustered index key will need to be provisioned in each non-clustered index row.

CREATE NONCLUSTERED INDEX IX_Orders_CustomerId
    ON dbo.Orders (CustomerId);

Take a look at Estimate the size of a clustered index for more details on estimating the amount of space that is required to store data in a clustered index

Clustered index key recommendations

The Clustered index design guidelines list the following ‘desirable properties’ for clustered index keys. They’re not mandatory (as in you can choose to ignore them), but there’s a price to pay if you do. It is important that you understand the trade-off if you do go against this recommendation.

Property Why
Narrow Reduces the storage, I/O, and memory overhead of all indexes on a table.
Unique Avoids the storage, I/O, and memory overhead of an additional uniqueifier column added to all indexes
Ever-increasing Avoids page splits in the middle of the index, which reduce page density and decrease performance.
Immutable When a key column of a clustered index is modified, a change must also be made in all nonclustered indexes, which adds a CPU, logging, I/O, and memory overhead.
Not-nullable columns only Avoids overhead of additional 3-4 byte NULL block per row
Fixed-width columns only Columns using variable width data types use an additional 2 bytes per value.

I’ve summarised the descriptions but check out the original table using the link above for more details.

Next time, I want to take a closer look at some specific data types as choices for primary keys and clustered index keys. I’m also interested to find out whether there are any types you can’t use for either of these.

SQL

DDD Adelaide 2026

On Saturday DDD Adelaide 2026 happened! A conference for the software developer community of Adelaide, South Australia. It was the result of a lot of planning and preparation from the organising team (Andrew, Claire, Harnoor, Ryan, Joel, Will and myself).

DDD Adelaide 2026 logo

A fantastic bunch of local and interstate speakers joined us at Adelaide University to share their knowledge and experience.

The turnout again was overwhelming. We sold out a few days early, and there was a wait-list (but I think most of those people got in).

Special thanks to the tireless group of volunteers (including my wife and daughter) who helped the day run smoothly.

Attendees milling around the atrium

People seated in a lecture theatre listening to a presentation

The food continues to amaze me. Cargo Catering knocked it out of the park again. Yummo is all I can say!

Morning tea ready to be consumed

I was a room host for 3 of the sessions:

Callum Whyte - Designed To Fail: Building Resilient Applications (which was a nice refresher as I’d also caught his talk at DDD Melbourne earlier in the year)

Callum presenting in a lecture theatre with two large screens behind showing his talk’s title slide

Tom Tuddenham - AI, Agile and the Collapse of Meaning: Why Modern Development Sucks and How We Can Fix It. I really appreciated Tom’s presentation and the different perspectives he brought.

Tom speaking

Alex Corcoran - The Beautiful Mess of Humans @ Work. Another really interesting talk, and I liked the ‘Winnipeg’ reference. I was not expecting quilting to be a presentation topic at DDD but it worked.

Alex speaking behind a lectern

The day ended with our traditional thank-yous and prize draws. I got right into the spirit!

David with one of the prizes

(This photo was taken by frankie the creative)

As an organiser it’s an intense day. You’re focused almost entirely on making the day an enjoyable one for all those who have attended. Even so, I did manage to catch myself later in the day and think “We did this”. But it’s really often a few days later when you’ve had time to process what happened and reflect back to appreciate that yes, it was a good thing we did.

We hope to be back again for 2027!

More recent posts

Older posts

All posts (currently 1202) can be found in the archive. Jump to recent years in 2026 , 2025 and 2024 .