简体   繁体   English

如何删除聚集属性但保留表中的主键。 SQL Server 2005

[英]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.所以我在 ID 列上有聚集索引和主键。 Now i need to drop clustered index (i want to create new clustered index on another column), but retain primary key.现在我需要删除聚集索引(我想在另一列上创建新的聚集索引),但保留主键。 Is it possible?是否可以?

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:在一个语句中这是不可能的,但是因为 DDL 在 MSSQL 中是事务性的,所以您可以简单地在事务中执行所有操作,以防止其他会话在没有主键的情况下访问该表:

begin tran
alter table dbo.[Table] drop constraint pk_id
alter table dbo.[Table] add constraint pk_id primary key nonclustered (id)
commit tran

这是不可能的,因为索引是约束的物理实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM