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).

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.







