user386167
user386167

Reputation:

Column order of a composite clustered index in a ALTER TABLE statement

I need to add a composite primary key (2 columns) to an already existing table. This key will also be a clustered index, so the order of the columns is important.

I am using the following script:

ALTER TABLE [Table] 
  ADD CONSTRAINT [PK_Table] 
  PRIMARY KEY CLUSTERED ([Col1] ASC, [Col2] ASC)

I need Col1 to be the first column of the clustered index, followed by Col2.

My question is if this script will do it (or do I need to explicitly set the order somehow?).

Appreciate it.

Upvotes: 1

Views: 329

Answers (1)

marc_s
marc_s

Reputation: 754973

That T-SQL statement is doing exactly what you say you need.

The order of the columns is the order in which you write them down in your T-SQL statement - there's no need nor any way to otherwise specify their order.

Your T-SQL statement will create a clustered index with Col1 first, followed by Col2 - just as you want it to be.

Upvotes: 2

Related Questions