2016-12-05 90 views
-1

大家好,感謝您抽出寶貴時間提供幫助。 我一直在做一個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')); 
+0

你給了我們太多的代碼,至少有100行或更多。您的錯誤可能是由於嘗試插入指向父母的記錄而導致的。請把你的問題縮小到最小。 –

+0

好吧,我只是不確定問題在哪裏,因此我可以刪除,所以我會做一個濃縮版本 –

回答

2

您試圖project之前填充expense

CREATE TABLE expense 
    (
    ... 

    CONSTRAINT expense_project_fk FOREIGN KEY (project_no) 
            REFERENCES project (project_no) 
    ... 
    ); 

DDL腳本中的表的順序看起來很適合DML腳本。

+0

Upvote只是花時間篩選這麼多的代碼。 –

+0

@TimBiegeleisen :-) –

+0

爲了澄清,將用於費用的插入語句移至項目插入後。或者將順序設置爲與刪除語句相反的順序。 – xQbert

0

謝謝Dudu Markovitz,你是正確的我填充表的順序是問題,我在項目之後放置了開銷,現在一切都很好。我的SQL老師從來沒有涉及這個事實,我不知道這是一個問題。謝謝你幫助我,特別是Dudu Markovitz。

相關問題