ORA-02291完整性约束错误

时间:2014-10-17 13:05:15

标签: sql oracle foreign-keys

我对SQL很新;我目前正在尝试使用一些示例数据填充我创建的表,但每当我这样做时它会执行错误消息:

Error starting at line 1 in command:
INSERT INTO Customer (CustomerID, AccountID, CustomerType, CustomerStatus) 
VALUES (123485, 227482, 'Residential', 'Eligible')
Error report:
SQL Error: ORA-02291: integrity constraint (T31833821.SYS_C0041532) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) 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*.

下面是我用来创建Customer表的代码(我还为AccountID创建了另一个表,它们目前都是空的)。

CREATE TABLE Customer
(
CustomerID NUMBER NOT NULL,
AccountID NUMBER(20) NOT NULL,
CustomerType VARCHAR(10) NOT NULL,
CustomerStatus VARCHAR(10) NOT NULL,    
CONSTRAINT customer_pk PRIMARY KEY (CustomerID),
CONSTRAINT check_customer_status CHECK(CustomerStatus IN ('Ineligible', 'Eligible')),
CONSTRAINT check_customer_type CHECK(CustomerType IN ('NonResident', 'Residential'))
);

我试图建立外键并插入样本数据,但上述错误仍然出现。

ALTER TABLE Customer ADD FOREIGN KEY (AccountID)
REFERENCES Account(AccountID);

INSERT INTO Customer (CustomerID, AccountID, CustomerType, CustomerStatus) 
VALUES (123485, 227482, 'nonresidential', 'eligible');

无论如何我可以解决这个问题?我使用的是Oracle SQL

0 个答案:

没有答案