SQL Error: ORA-02291: integrity constraint

后端 未结 4 2043
走了就别回头了
走了就别回头了 2020-12-09 21:00

I am creating a database that is trying to access values from a foreign key. I have created two following tables

CREATE TABLE Component(
    ComponentID var         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-09 21:24

    Please post your entire SQLPLUS session so that the error is easily reproducible.

    Looks like the insert into the child table is being done before the insert into the parent table which is causing this error.

    Change the order of inserts and rerun your code.

    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.
    

提交回复
热议问题