2012-10-22 115 views
1

是否可以在已具有唯一非聚簇索引的表中添加主键?Sybase唯一索引和主键

我有一张表cargo_car,我需要将cd_cargo_car定义为PK。

我尝试这样做:

ALTER TABLE dbo.cargo_car ADD PRIMARY KEY (cd_cargo_car) 

,但我收到的错误:

Error (1921) An index with the same columns inthe same order alredy exists onthe table 

这个表有很多的依赖性,而当我尝试删除索引,我收到:

Error (3712) Cannot drop index 'cargo_car.XPKcargo_car' because it still has referential integrity constraints. 

这是创建脚本:

CREATE TABLE dbo.cargo_car 
    (
    cd_cargo_car  SMALLINT NOT NULL, 
    nm_cargo_car  VARCHAR (40) NOT NULL, 
    cd_refini_car  CHAR (4) NOT NULL, 
    cd_reffin_car  CHAR (4) NOT NULL, 
    cd_jornada1_car CHAR (2) NOT NULL, 
    cd_jornada2_car CHAR (2) NOT NULL 

    ) 
GO 

CREATE UNIQUE NONCLUSTERED INDEX XPKcargo_car 
    ON dbo.cargo_car (cd_cargo_car) 
GO 

关于如何做到这一点的建议?

韩国社交协会

+4

我想你将不得不放弃其他表上的所有外键约束,删除该索引,创建主键(我想你想要它被聚集?),然后重新创建外键约束。 – MatBailie

+0

您的解决方案非常完美我不得不放弃索引和所有参考,创建PK并重新创建我的约束。谢谢@Dems – meurer

回答

1

you're going to have to drop all the foreign key contraints on the other tables, drop that index, create the Primary Key (I guess you want it to be clustered?), then re-create the foreign key constraints. – Dems

要允许关闭这个问题,我完成转换的注释答案。这是正确的,类似于我想写...