簡體   English   中英

SQL錯誤:ORA-02270:此列列表沒有匹配的唯一鍵或主鍵

[英]SQL Error: ORA-02270: no matching unique or primary key for this column-list

將一些外鍵約束添加到2個表OUTPUT和SUPERVISOR中時遇到問題:

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

在這種情況下我該怎么辦?

確保在表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);

謝謝大家,我想我已經知道了。 因為PROJECT表中的主鍵是組合鍵,所以屬性的數量與SUPERVISOR和OUTPUT中的屬性不匹配,因此會彈出錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM