2014-03-04 79 views
1

目前正在努力外鍵適用於我創建的表,但我得到了SQL錯誤:小學及外鍵約束混亂

SQL Error: ORA-02270: no matching unique or primary key for this column-list 
02270. 00000 - "no matching unique or primary key for this column-list" 
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement 
     gives a column-list for which there is no matching unique or primary 
     key constraint in the referenced table. 
*Action: Find the correct column names using the ALL_CONS_COLUMNS 
     catalog view 

我在哪裏上從這裏走一個巨大的損失,我按順序思考一切,但我不能指出什麼是錯的。

下面是我使用的relavent創建和修改腳本,如果有人能指出我要去哪裏錯了:結果

創造& DopingTest:

CREATE TABLE Results 
(
RaceID NUMBER, 
HorseID NUMBER, 
JockeyID NUMBER, 
Position numeric(2) 
); 

CREATE TABLE DopingTest 
(
RaceID NUMBER, 
HorseID NUMBER, 
TakenBy varchar2(60) 
); 

和約束的加入:

ALTER TABLE Results 
ADD(
    CONSTRAINT pk_ResultsID 
    PRIMARY KEY (RaceID,HorseID)); 

ALTER TABLE DopingTest 
ADD(
    CONSTRAINT pk_DopingTest 
    PRIMARY KEY (RaceID, HorseID)); 

ALTER TABLE Results 
ADD(
    CONSTRAINT fk_raceID 
    FOREIGN KEY (RaceID) 
    REFERENCES Race(RaceID), 
    CONSTRAINT fk_horseID 
    FOREIGN KEY (HorseID) 
    REFERENCES Horse(HorseID), 
    CONSTRAINT fk_JockeyID 
    FOREIGN KEY (JockeyID) 
    REFERENCES Jockey(JockeyID)); 

ALTER TABLE DopingTest 
ADD(
    CONSTRAINT fk_RaceIDDT 
    FOREIGN KEY (RaceID) 
    REFERENCES Results(RaceID), 
    CONSTRAINT fk_HorseIDDT 
    FOREIGN KEY (HorseID) 
    REFERENCES Results(HorseID)); 

任何幫助將不勝感激,謝謝。

回答

3

如果results有一個組合主鍵,則外鍵必須引用鍵的兩個組件。

ALTER TABLE DopingTest 
ADD(
    CONSTRAINT fk_RaceIDDT 
    FOREIGN KEY (RaceID, HorseID) 
    REFERENCES Results(RaceID, HorseID) 
); 
+0

是的!感謝它,謝謝你:)。 – Naffel