I have a heap table - no clustered index defined - (call it table A
), with a unique non clustered index on a non nullable column (call the column ID
and the index IX
).
I would like to use index IX
when defining the primary key constraint on column ID
for table A
.
The documentation somewhere says this:
The Database Engine automatically creates a unique index to enforce the uniqueness requirement of the PRIMARY KEY constraint. If a clustered index does not already exist on the table or a nonclustered index is not explicitly specified, a unique, clustered index is created to enforce the PRIMARY KEY constraint.
I've read through the entire ALTER TABLE
documentation but there seems to be no syntax for "nonclustered index is ... explicitly specified, ".
Have tried defining the nonclustered index IX
and specifying primary key, and have also tried various combinations of the alter table ... add constraint ... primary key statement to no avail.
It makes sense that my index IX is equivalent to the nonclustered index that SQL Server creates when I simply specify the ID column in the alter table .... add constraint .... primary key (ID) statement, but I would prefer not having this redundant index which SQL Server creates for me, and rather make it use the index IX
which also consists of a include list of columns.
If I drop the index that SQL Server creates then the primary key constraint also vanishes.
If it were possible to alter the index that SQL Server creates my problem would be solved, but the alteration I would like to do to it requires a drop and recreate.
NOT NULL
andUNIQUE
constraints is equivalent to a column withPRIMARY KEY
constraint. Why change it anyway? – ypercubeᵀᴹPRIMARY KEY
constraint. – ypercubeᵀᴹ[ CLUSTERED | NONCLUSTERED ]
part that followsPRIMARY KEY
in the primary key declaration (which is probably what @ypercube is talking about too). The syntax diagrams show no way of specifying an existing index to use with the primary key constraint, you can only specify whether the [new] primary key index is going to be clustered or non-clustered. – Andriy M