2015-12-30 75 views
0

加入一些外鍵約束到2臺輸出,監事時,我有一個問題:SQL錯誤:ORA-02270:沒有匹配的唯一或主鍵此列列表

ALTER TABLE OUTPUT ADD CONSTRAINT PROJECT_OUTPUT_FK 
FOREIGN KEY (proj_id) 
REFERENCES PROJECT (proj_id) 
NOT DEFERRABLE; 

ALTER TABLE SUPERVISOR ADD CONSTRAINT PROJECT_SUPERVISOR_FK 
FOREIGN KEY (proj_id) 
REFERENCES PROJECT (proj_id) 
NOT DEFERRABLE; 

錯誤顯示:

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 

在這種情況下該怎麼辦?

+1

你對錯誤有什麼不瞭解?外鍵約束需要引用被引用表中的主鍵或唯一鍵。 –

+0

PROJECT(proj_id)是主鍵嗎? – Praveen

+5

http://stackoverflow.com/questions/10802212/oracle-ora-02270-no-matching-unique-or-primary-key-for-this-column-list-erro – DanK

回答

0

確保您在表PROJECT的PROJ_ID列上定義了主鍵或唯一鍵。您可以使用此查詢這個(如果適當替換用戶/所有者),並找出哪些列的PK/UK的一部分:

select c.owner, c.constraint_name, c.constraint_type, cc.column_name 
    from all_constraints c 
    join all_cons_columns cc on cc.owner = c.owner and cc.constraint_name = c.constraint_name 
where c.constraint_type in ('P', 'U') 
    and c.table_name = 'PROJECT' 
     and c.owner = user 
order by cc.position; 

如果需要還創建一個主鍵,你有需要的權限,你可以這樣做:

ALTER TABLE PROJECT ADD CONSTRAINT PK_PROJECT PRIMARY KEY (proj_id); 
+0

但是我已經在PROJECT,OUTPUT和supervisor表中設置了proj_id作爲主鍵。爲什麼他們不能被識別? – davehyy

0

謝謝你們我想我想通了。由於PROJECT表中的主鍵是組合鍵,因此屬性的數量與SUPERVISOR和OUTPUT中的屬性不匹配,因此會彈出錯誤消息。

相關問題