What is the SQL for having a primary key consisting of 4 columns but only include 3 of these columns in a clustered index?
Is it possible to do this in SQL Server Management Studio as well?
What is the SQL for having a primary key consisting of 4 columns but only include 3 of these columns in a clustered index?
Is it possible to do this in SQL Server Management Studio as well?
You'll need to define two separate things:
a non-clustered primary key on your four columns
ALTER TABLE dbo.YourTable
ADD CONSTRAINT PK_YourTable
PRIMARY KEY NONCLUSTERED (Col1, Col2, Col3, Col4)
a separate clustering index on three of your four columns:
CREATE CLUSTERED INDEX CIX_YourTable
ON dbo.YourTable(Col1, Col3, Col4)
-- adapt this to use those three columns you want
And yes - of course you can execute those T-SQL scripts in your SQL Server Management Studio :-)
Update: to do this in the GUI using clicky-mousy-GUI-tools, you need to do these steps:
in Object Explorer, on your table, you need to first define the clustered index by going to Indexes > New Index > Clustered Index
- give it a name and add the columns you want
after you've done this, then you can define your primary key in the usual table designer view, and since there already is a clustered index, it will become a nonclustered primary key
I haven't found any other way - if you create the primary key first, it becomes a clustered primary key, and I found no way to change that after it's been created