Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql azure db删除pk并添加新pk_Sql_Sql Server_Azure_Azure Sql Database - Fatal编程技术网

Sql azure db删除pk并添加新pk

Sql azure db删除pk并添加新pk,sql,sql-server,azure,azure-sql-database,Sql,Sql Server,Azure,Azure Sql Database,我在Azure上有MS SQL数据库,我想删除我的主键约束并添加另一列作为主键,每次尝试运行脚本删除主键时,我都会收到错误: “此版本的SQL Server不支持没有聚集索引的表。请创建聚集索引,然后重试。” 通过运行此脚本: IF EXISTS (SELECT * FROM sys.key_constraints WHERE type = 'PK' AND parent_object_id = OBJECT_ID('dbo.Paypaltransaction') AND Name = 'PK_

我在Azure上有MS SQL数据库,我想删除我的主键约束并添加另一列作为主键,每次尝试运行脚本删除主键时,我都会收到错误:

“此版本的SQL Server不支持没有聚集索引的表。请创建聚集索引,然后重试。”

通过运行此脚本:

IF EXISTS (SELECT * FROM sys.key_constraints WHERE type = 'PK' AND parent_object_id = OBJECT_ID('dbo.Paypaltransaction') AND Name = 'PK_PaypalTransaction')
   ALTER TABLE dbo.PaypalTransaction
   DROP CONSTRAINT PK_PaypalTransaction
GO
然后我尝试创建另一个主键:

-- add new primary key constraint on new column   
ALTER TABLE dbo.PaypalTransaction 
ADD CONSTRAINT PK_PaypalTransactionId
PRIMARY KEY CLUSTERED (PaypalTransactionId)
GO
我得到了这个错误: “表'PaypalTransaction'上已定义主键。”

我理解错误,但我不能删除主键,因为它显然必须有一个,我不能添加一个新的,因为我已经有一个。我是不是永远被错误的列作为主键卡住了(


我明白了,他们最终放弃了这个表并创建了一个新的表-这不是唯一的方法,这很愚蠢。

从DBA复制过来。Se回答:

Q5)如果填充了表,通过聚集索引强制执行时,是否可以修改表的主键?
答:否。任何将填充的聚集索引转换为堆的操作都将在SQL Azure中被阻止,即使该表为空:

另请注意:如果表被截断,则可以修改约束

更改已填充表的PK约束的解决方法是使用旧技巧:


sp_rename
方法存在一些问题,最重要的是,在重命名过程中,表上的权限以及外键约束不会继续存在。

我刚刚测试过,似乎在SQL AZURE V12上是可能的

看来,这只是出于故意,那么愚蠢。看这也V12是完全不同于V.pre12的技术。它几乎支持单机版支持的所有功能。
create table Friend (
    UserId int not null,
    Id int not null identity(1,1),
    constraint pk_Friend primary key clustered (UserId, Id));
go
insert into Friend (UserId) values (1);
delete from Friend;
go
alter table Friend drop constraint pk_Friend;
create table Friend (
    UserId int not null,
    Id int not null identity(1,1),
    constraint pk_Friend primary key clustered (UserId, Id));
go
insert into Friend (UserId) values (1);
go

create table FriendNew (
    UserId int not null,
    Id int not null identity(1,1),
    constraint pk_Friend_New primary key clustered (Id, UserId));
go

set identity_insert FriendNew on;
insert into FriendNew (UserId, Id) 
select UserId, Id
from Friend;
set identity_insert FriendNew off;
go

begin transaction
exec sp_rename 'Friend', 'FriendOld';
exec sp_rename 'FriendNew', 'Friend';
commit;
go

sp_help 'Friend';