大家好,感謝您抽出寶貴時間提供幫助。 我一直在做一個SQL腳本工作正常據我所知除了一個名爲費用的表,我一直試圖修復它3天,我仍然得到一個錯誤, ORA-02291:完整性約束(完整性約束MARTE71.EXPENSE_PROJECT_FK)違反 - 父鍵找不到。ORA-02291:違反了完整性約束(MARTE71.EXPENSE_PROJECT_FK) - 未找到父鍵
我假設我用外鍵犯了一個錯誤,但是我已經嘗試了所有我知道的,但仍然失敗,請你看看我的邏輯,如果可能的話,找出錯誤和可能的解決方案。
我真的很感激任何幫助和建議。 我將在下面附上我的創建和填充腳本。
非常感謝。
CREATE TABLE project
(
project_no NUMBER(4),
project_code VARCHAR2(8),
description VARCHAR2(50),
client_no NUMBER(4),
mgr_no NUMBER(4),
CONSTRAINT project_pk PRIMARY KEY (project_no),
CONSTRAINT project_employee_fk FOREIGN KEY (mgr_no)
REFERENCES employee(employee_no)
);
-- planned duration is expressed as a number of weeks
-- fees-basis can take only the following values VS, VN, CS, CN, FQ
CREATE TABLE stage
(
stage_id NUMBER(4),
project_no NUMBER(4),
stage_no NUMBER(4),
description VARCHAR2(50),
start_date DATE,
planned_duration NUMBER(4,1),
fees_basis VARCHAR2(2),
estimate_date DATE,
CONSTRAINT stage_pk PRIMARY KEY (stage_id),
CONSTRAINT stage_project_fk FOREIGN KEY (project_no)
REFERENCES project (project_no),
CONSTRAINT p_no_in_stage_nn CHECK (project_no IS NOT NULL)
);
CREATE TABLE expense
(
expense_id NUMBER(4),
description VARCHAR2(50),
quantity NUMBER(2),
unit_cost NUMBER(5,2),
chargeable CHAR(1),
project_no NUMBER(4),
invoice_no NUMBER(4),
CONSTRAINT expense_pk PRIMARY KEY (expense_id),
CONSTRAINT expense_project_fk FOREIGN KEY (project_no)
REFERENCES project (project_no),
CONSTRAINT expense_invoice_fk FOREIGN KEY (invoice_no)
REFERENCES invoice (invoice_no),
CONSTRAINT expense_proj_no_nn CHECK (project_no IS NOT NULL)
);
和填入腳本
-- data represents: expence_id, description, quantity, unit_cost, chargeable, project_no, invoice_no
INSERT INTO expense VALUES (1, 'total expence for building system', 2, 400.00, 'Y', 1, 1);
-- data represents: project_no, project_code, description, mgr_no, client_no
INSERT INTO project VALUES (1,'MISTEST', 'HR System Development',1, 1);
INSERT INTO project VALUES (2,'ECOM_MS', 'E-commerce System',1, 2);
-- data represents: stage_id, project_no, stage_no, description,
-- start_date, planned_duration, fees_basis, estimate_date
INSERT INTO stage VALUES (1, 1, 1,'Development of system', TO_DATE('08-MAY-09','DD-MON-YY'), 40.0, 'VS', TO_DATE('17-JAN-09','DD-MON-YY'));
INSERT INTO stage VALUES (2, 1, 2,'Testing of system', TO_DATE('15-JUL-09','DD-MON-YY'), 40.0, 'VS', TO_DATE('17-JAN-09','DD-MON-YY'));
INSERT INTO stage VALUES (3, 2, 1,'Rquirements Analysis', TO_DATE('10-JAN-10','DD-MON-YY'), 40.0, 'VS', TO_DATE('17-JUL-10','DD-MON-YY'));
你給了我們太多的代碼,至少有100行或更多。您的錯誤可能是由於嘗試插入指向父母的記錄而導致的。請把你的問題縮小到最小。 –
好吧,我只是不確定問題在哪裏,因此我可以刪除,所以我會做一個濃縮版本 –