2010-12-03 119 views
7

我正在創建試圖從外鍵訪問值的數據庫。我製作了以下兩張表格SQL錯誤:ORA-02291:完整性約束

CREATE TABLE Component(
    ComponentID varchar2(9) PRIMARY KEY 
    , TypeID varchar2(9) REFERENCES TypeComponent(TypeComponentID) 
) 

INSERT INTO Component VALUES(192359823,785404309) 
INSERT INTO Component VALUES(192359347,785404574) 
INSERT INTO Component VALUES(192359467,785404769) 
INSERT INTO Component VALUES(192359845,785404867) 
INSERT INTO Component VALUES(192359303,785404201) 
INSERT INTO Component VALUES(192359942,785404675) 


CREATE TABLE TypeComponent (
    TypeComponentID varchar2(9) PRIMARY KEY 
    , Type_Description varchar2(30) CONSTRAINT Type_Description 
     CHECK(Type_Description IN('Strap', 'Buckle', 'Stud')) NOT NULL 
) 

INSERT INTO TypeComponent VALUES(785404309, 'Strap') 
INSERT INTO TypeComponent VALUES(785404574, 'Stud') 
INSERT INTO TypeComponent VALUES(785404769, 'Buckle') 
INSERT INTO TypeComponent VALUES(785404867, 'Strap') 
INSERT INTO TypeComponent VALUES(785404201, 'Buckle') 
INSERT INTO TypeComponent VALUES(785404675, 'Stud') 

這些是兩張表格。 ComponentTypeComponentComponent是父實體TypeComponent,我試圖運行下面的INSERT語句:

INSERT INTO Component VALUES(192359823,785404309) 

,但它給我的錯誤

這是我迄今爲止在甲骨文的SQL開發會議

+0

您可以請張貼您的整個會議,並重新格式化您的文章有點? – 2010-12-03 21:07:02

+0

無論何時輸入SQL語句和錯誤日誌,請選擇語句,然後單擊代碼(例如,使用`1010`的圖標,以便清晰。謝謝。 – Sathya 2010-12-03 21:14:59

回答

6

嘗試在你的TypeComponent表中插入,然後再插入到前你的Component表。

根據錯誤:

ORA-02291:    integrity constraint (string.string) violated - parent key not found
Cause:            A foreign key value has no matching primary key value.
Action:            Delete the foreign key or add a matching primary key.

這意味着在你的參照表中沒有匹配的密鑰。

編輯#1

爲了您的實物資料,這裏是一個網站,你可以得到幫助,所有的Oracle錯誤代碼。

http://[ora-02291].ora-code.com/

下面是這個網站的主頁:http://www.ora-code.com/

下,您可能在網址替換錯誤代碼,以滿足你得到的錯誤代碼,你會接觸到該錯誤的頁面。

2

請發佈您的整個SQLPLUS會話,以便錯誤很容易重現。

看起來像插入到子表中插入到子表中導致此錯誤之前完成。

更改插入順序並重新運行您的代碼。

SQL> CREATE TABLE TypeComponent(
    2 TypeComponentID varchar2(9) PRIMARY KEY, 
    3 Type_Description  varchar2(30) 
    4 CONSTRAINT Type_Description CHECK(Type_Description IN('Strap', 'Buckle', 'Stud')) NOT NULL 
    5 ) 
    6 ; 

Table created. 

SQL> CREATE TABLE Component(
    2 ComponentID varchar2(9) PRIMARY KEY, 
    3 TypeID varchar2(9) REFERENCES TypeComponent(TypeComponentID) 
    4 ) 
    5 ; 

Table created. 

SQL> INSERT INTO Component VALUES(192359823,785404309); 
INSERT INTO Component VALUES(192359823,785404309) 
* 
ERROR at line 1: 
ORA-02291: integrity constraint (COAMGR.SYS_C002513823) violated - parent key 
not found 

SQL> INSERT INTO TypeComponent VALUES(785404309, 'Strap'); 

1 row created. 

SQL> INSERT INTO Component VALUES(192359823,785404309); 

1 row created. 

SQL> commit; 

Commit complete. 
1

component表你有

TypeID REFERENCES TypeComponent(TypeComponentID) 

然後你

INSERT INTO Component VALUES(192359823,785404309) 

如果執行這個第一,那麼甲骨文將拋出Integrity constraint錯誤,因爲你試圖插入進行component表中的值不存在於TypeComponent表中。

這是因爲你曾提到,TypeID是一個外鍵,即問題的TypeID值需要存在於TypeComponent插入Component

+3

值得指出一個通用規則:填充數據時,填充*首先是引用*數據(所以component * type *是引用數據),然後是主數據 – araqnid 2010-12-03 21:20:31

1

這個錯誤是出現了一些與引用鍵(外鍵在我的情況下)有錯誤。請重新創建表中引用的密鑰。

相關問題