Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Error: ORA-02291: integrity constraint

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 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')

These are the two tables. Component and TypeComponent. Component is the parent entity to TypeComponent, and I am trying to run the following INSERT statement:

INSERT INTO Component VALUES(192359823,785404309)

but it is giving me the error

This is the session that I have so far in Oracle SQL dev

like image 628
Deej Avatar asked Dec 03 '10 21:12

Deej


People also ask

What is integrity constraint violated?

Integrity constraint violations occur when an insert, update, or delete statement violates a primary key, foreign key, check, or unique constraint or a unique index. Attempt to insert duplicate key row in object object_name with unique index index_name.

What is Oracle parent key?

A parent key is either a primary key or a unique key in the parent table of a referential constraint. This key consists of a column or set of columns. The values of a parent key determine the valid values of the foreign key in the constraint.

How do I disable referential integrity constraints in Oracle?

There are multiple ways to disable constraints in Oracle. constraint_name; Another way to enable and disable constraints in Oracle would be to either use a plsql block or write a script. Execute Immediate 'alter table '||:tab_name||' disable constraint '||tabCons(numCount);

What are the integrity constraints in SQL?

Integrity Constraints are the protocols that a table's data columns must follow. These are used to restrict the types of information that can be entered into a table. This means that the data in the database is accurate and reliable. You may apply integrity Constraints at the column or table level.


1 Answers

Try inserting in your TypeComponent table first, then insert into your Component table.

According to the error:

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.

This means that there is no matching key in your referenced table.

EDIT #1

For your kind information, here is a site where you can get help with all of the Oracle error codes.

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

Here's the homepage for this site: http://www.ora-code.com/

Where you may replace the error code in the URL to suit the error code you're getting, and you'll access to the page for this error.

like image 51
Will Marcouiller Avatar answered Sep 21 '22 21:09

Will Marcouiller