I have defined the primary key on a table as nonclustered
. With non-clustered indexes which are explicitly created by create nonclustered index
it is possible to include additional (non-indexed) columns. Is the same also possible for the implicitly created primary key non-clustered index?
The nonclustered index can be unique or non-unique. Is it not available for ADD CONSTRAINT, so you cannot INCLUDE any columns with a Primary Key, even if it is non-clustered. A PRIMARY KEY is useful as a UNIQUE identifier for the record, and is a candidate key available for REFERENTIAL constraints.
By including nonkey columns, you can create nonclustered indexes that cover more queries. This is because the nonkey columns have the following benefits: They can be data types not allowed as index key columns.
Non-Clustered index is created by adding key columns that are restricted in the number, type and size of these columns. To overcome these restrictions in the index keys, you could add a non-key columns when creating a non-clustered index, which are the Included Columns.
The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
Although one cannot specify included columns with a primary key or unique constraint, a workaround is a unique non-clustered index with the same key along with included columns. Not only does this provide the same uniqueness guarantee as a primary key constraint, foreign keys can reference a unique constraint/index key too, providing the same referential integrity as a referenced primary key.
The upside with this method is performance for specialized use cases, such as when the best clustered index choice is not the primary key and included columns are desirable for performance.
Downsides are one can only specify declarative cascading DRI with a primary key constraint and not intuitive.
The syntax for INCLUDE columns is only available for CREATE NONCLUSTERED INDEX, specifically
INCLUDE (column [ ,... n ] ) Specifies the non-key columns to be added to the leaf level of the nonclustered index. The nonclustered index can be unique or non-unique.
Is it not available for ADD CONSTRAINT, so you cannot INCLUDE any columns with a Primary Key, even if it is non-clustered.
A PRIMARY KEY is useful as a UNIQUE identifier for the record, and is a candidate key available for REFERENTIAL constraints. However, for performance reasons, if you have a clustering key on another column and you have no other candidate keys to promote as a PK, you can always create an additional non-clustered index on the primary key column(s) and INCLUDE other columns onto the index.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With