2012-10-22 121 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

要允許關閉這個問題,我完成轉換的註釋答案。這是正確的,類似於我想寫...