sqlchild
sqlchild

Reputation: 9084

using primary keys in sql server

when i create primary key on a column 'FirstName', is a clustered index automatically created for this column?

Upvotes: 0

Views: 53

Answers (2)

nvogel
nvogel

Reputation: 25534

Depends on what you specify when you create the key. Indexes are automatically created for key constraints by SQL Server but as part of the constraint syntax you can specify either CLUSTERED or NONCLUSTERED.

Upvotes: 1

marc_s
marc_s

Reputation: 755321

Yes - this is default SQL Server behavior.

It doesn't have to be that way - if you create your PK using T-SQL script, you can also choose to make it a non-clustered PK and put the clustering key on another column.

ALTER TABLE dbo.YourTable
    ADD CONSTRAINT PK_YourTable
    PRIMARY KEY NONCLUSTERED(FirstName)

CREATE CLUSTERED INDEX CIX_YourTable
    ON dbo.YourTable(SomeOtherColumn)

In this case, FirstName would be the (non-clustered) primary key on your table, while SomeOtherColumn would be the clustering key for that same table.

Upvotes: 1

Related Questions