Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop clustered property but retain primary key in a table. SQL Server 2005

i have the following key:

ALTER TABLE dbo.Table ADD  CONSTRAINT PK_ID PRIMARY KEY CLUSTERED 
(
ID ASC
)

so i have clustered index and primary key on ID column. Now i need to drop clustered index (i want to create new clustered index on another column), but retain primary key. Is it possible?

like image 716
skaeff Avatar asked Jul 28 '10 06:07

skaeff


2 Answers

It's not possible in one statement, but because DDL is transactional in MSSQL, you can simply do everything inside a transaction to prevent other sessions accessing the table while it has no primary key:

begin tran
alter table dbo.[Table] drop constraint pk_id
alter table dbo.[Table] add constraint pk_id primary key nonclustered (id)
commit tran
like image 135
Pondlife Avatar answered Oct 13 '22 02:10

Pondlife


It is not possible, as the index is a physical implementation of the constraint.

like image 45
ceth Avatar answered Oct 13 '22 03:10

ceth