Ошибка oracle 2291

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

ORA-02291: integrity constraint violated – parent key not found error occurs when a foreign key value in the child table does not have a matching primary key value in the parent table, as stated by a foreign key constraint. You try to insert a row into a child table that does not have a corresponding parent row. The column value you supplied for the child table did not match the primary key in the parent table.

You try to insert or update a row in the child table. The value in the child table’s reference column should be available in the parent table’s primary key column. If the primary key column does not have a value, the row cannot be inserted or updated in the child table. The parent key’s integrity constraint was violated.

The value of the child table’s foreign key column should be the same as the value of the parent table’s primary key column. If the value does not exist in the parent table, an error ORA-02291: integrity constraint violated – parent key not found will be thrown.

Cause

A foreign key value has no matching primary key value.

Action

Delete the foreign key or add a matching primary key.

The Problem

When two tables in a parent-child relationship are created, a referential foreign key constraint is generated and enforces the relationship between the two tables. The value of the foreign key column in the child table is decided by the value of the primary key column in the parent table.

A value that is not available in the parent table cannot be inserted or updated in the child table. If you try to insert or update a value in the foreign key column of a child table, Oracle will throw the parent key integrity constraint violation error.

create table dept
(
 id numeric(5) primary key,
 name varchar2(100)
);

create table employee
(
  id numeric(5) primary key,
  name varchar2(100),
  deptid numeric(5) references dept(id)
);

insert into employee values(1,'Yawin',1);

Error

Error starting at line : 17 in command -
insert into employee values(1,'Yawin',1)
Error report -
ORA-02291: integrity constraint (HR.SYS_C0012551) violated - parent key not found

Solution 1

If the integrity constraint is violated, knowing the parent and child tables involved in the foreign key relationship is important. The parent and child table names, as well as the column names, may be retrieved using the integrity constraint name. The parent table, child table, parent column name, child column name, and integrity constraint name will be shown in the following sql query.

select r.constraint_name Foreign_key_constraint,
    p.owner parent_owner, p.table_name parent_table, pc.column_name parent_column_name, 
    r.owner child_owner, r.table_name child_table, rc.column_name child_colum_name
from user_constraints p
join user_cons_columns pc on p.owner=pc.owner 
        and p.table_name=pc.table_name and p.constraint_name = pc.constraint_name
        and p.constraint_type='P'
join user_constraints r on p.constraint_name=r.r_constraint_name and r.constraint_type='R'
join user_cons_columns rc on r.owner=rc.owner 
        and r.table_name=rc.table_name and r.constraint_name = rc.constraint_name
        and r.constraint_type='R'
where r.constraint_name='SYS_C0012551' 
order by p.owner, p.table_name, pc.column_name, rc.position;

Output

Foreign_key_constraint | parent_owner |parent_table | parent_column_name |child_owner | child_table | child_colum_name
SYS_C0012548	HR	DEPT	ID	HR	EMPLOYEE	DEPTID

Solution 2

The value you are trying to put into the child table reference column does not exist in the parent table. You must first enter the value that you intended to insert into the child table into the parent table. After inserting the value as a parent row, you may go back and enter it into the child table.

insert into dept values (1, 'sales');

insert into employee values(1,'Yawin',1)

Output

1 row inserted.

1 row inserted.

Solution 3

You are attempting to insert a row into a child table for which the primary key does not exist in the parent table. Before you enter a child, make sure you have a parent key for that child in the parent table.

insert into employee values(1,'Yawin',1)

insert into employee values(1,'Yawin',1)
Error report -
ORA-02291: integrity constraint (HR.SYS_C0012551) violated - parent key not found


insert into employee values(1,'Yawin',100) -- the value 100 exist in the dept table.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 CREATE TABLE DEPARTMENT(
DEPARTMENTS_ID NUMBER NOT NULL, 
DEPARTMENTS_OTDEL NUMBER,
DEPARTMENTS_NAME VARCHAR2(100 BYTE), 
PRIMARY KEY (DEPARTMENTS_ID)
     );
     
INSERT INTO DEPARTMENT VALUES('1','1','АДМИНИСТРАЦИЯ');
INSERT INTO DEPARTMENT VALUES('2','16','КАФЕДРА АГРОНОМИИ');
INSERT INTO DEPARTMENT VALUES('3','22','КАФЕДРА ИНФОРМАТИКИ');
INSERT INTO DEPARTMENT VALUES('4','25','ПЛАНОВЫЙ ОТДЕЛ');
INSERT INTO DEPARTMENT VALUES('5','29','БУХГАЛТЕРИЯ');
INSERT INTO DEPARTMENT VALUES('6','46','МАТЕРИАЛЬНЫЙ ОТДЕЛ');
INSERT INTO DEPARTMENT VALUES('7','50','ОТДЕЛ ОХРАНЫ ТРУДА');
INSERT INTO DEPARTMENT VALUES('8','52','ОТДЕЛ КАДРОВ');
INSERT INTO DEPARTMENT VALUES('9','66','КАФЕДРА ХИМИИ');
INSERT INTO DEPARTMENT VALUES('10','92','ХОЗЧАСТЬ');
INSERT INTO DEPARTMENT VALUES('11','96','КАФЕДРА ФИЗИКИ');
INSERT INTO DEPARTMENT VALUES('12','97','БИОЛОГИЧЕСКИЙ ФАКУЛЬТЕТ');     
 
CREATE TABLE POSIT(
POSITIONS_ID NUMBER NOT NULL,
POSITIONS_DOLJNOST NUMBER, 
POSITIONS_NAME VARCHAR2(120 BYTE), 
PRIMARY KEY (POSITIONS_ID)
     );
          
INSERT INTO POSIT VALUES ('1','5','ДОЦЕНТ');
INSERT INTO POSIT VALUES ('2','7','ЗАВЕДУЩИЙ ЛАБОРАТОРИЕЙ');
INSERT INTO POSIT VALUES ('3','8','ЛАБОРАНТ');
INSERT INTO POSIT VALUES ('4','9','КАНЦЕЛЯРИСТ');
INSERT INTO POSIT VALUES ('5','10','ИНЖЕНЕР');
INSERT INTO POSIT VALUES ('6','12','НАЧАЛЬНИК ОТДЕЛА');
INSERT INTO POSIT VALUES ('7','14','МАСТЕР');
INSERT INTO POSIT VALUES ('8','15','ПРОГРАММИСТ');
INSERT INTO POSIT VALUES ('9','16','ПРОФЕССОР');
INSERT INTO POSIT VALUES ('10','18','СЛЕСАРЬ-ЭЛЕКТРИК');
INSERT INTO POSIT VALUES ('11','20','СТАРШИЙ ПРЕПОДАВАТЕЛЬ');
INSERT INTO POSIT VALUES ('12','22','ЭЛЕКТРИК');
INSERT INTO POSIT VALUES ('13','100','ДИРЕКТОР');
 
CREATE TABLE STAFF(
STAFF_ID NUMBER NULL, 
STAFF_NOMER NUMBER, 
STAFF_SURNAME VARCHAR2(100 BYTE), 
STAFF_NAME VARCHAR2(100 BYTE), 
STAFF_PATRONYMIC VARCHAR2(100 BYTE), 
STAFF_DATEBIRTH DATE, 
STAFF_DATEWORK DATE, 
POSITIONS_DOLJNOST NUMBER, 
DEPARTMENTS_OTDEL NUMBER, 
STAFF_STATUS VARCHAR2(100 BYTE), 
STAFF_OKLAD NUMBER, 
STAFF_KEY_MAIN NUMBER, 
PRIMARY KEY (STAFF_ID),
FOREIGN KEY (POSITIONS_DOLJNOST) REFERENCES POSIT, 
FOREIGN KEY (DEPARTMENTS_OTDEL) REFERENCES DEPARTMENT
);
 
INSERT INTO STAFF VALUES('1','2061','ГУБИН','АЛЕКСАНДР','МИХАЙЛОВИЧ','30.01.1963','02.09.1995','16','8','Р','1500','2309');
INSERT INTO STAFF VALUES('2','2072','КОНДРАТЬЕВ','НИКОЛАЙ','НИКОЛАЕВИЧ','28.08.1996','17.06.1996','22','8','Р','1000','2409');
INSERT INTO STAFF VALUES('3','2081','ШИРЯЕВ','ЮРИЙ','БОРИСОВИЧ','25.01.1961','08.08.1988','16','20','Р','3000','2402');
INSERT INTO STAFF VALUES('4','2084','ВЕТРОВА','ЛЮДМИЛА','ВЛАДИМИРОВНА','05.12.1951','01.09.1996','97','16','Р','1500','2332');
INSERT INTO STAFF VALUES('5','2160','НИКИТИНА','МАРИНА','АЛЕКСЕЕВНА','07.02.1955','01.05.1995','50','10','Р','3500','2332');
INSERT INTO STAFF VALUES('6','2198','ШУЛЬЖЕНКО','НАИНА','ИВАНОВНА','28.07.1969','01.05.1995','52','9','Р','1000','2332');
INSERT INTO STAFF VALUES('7','2202','ГРЕБЕШКОВА','СВЕТЛАНА','СЕРГЕЕВНА','20.02.1949','01.05.1995','52','12','Р','1500','2397');
INSERT INTO STAFF VALUES('8','2271','БУРАНОВ','АЛЕКСАНДР','АЛЕКСАНДРОВИЧ','04.06.1941','23.05.1988','25','14','Р','3000','2409');
INSERT INTO STAFF VALUES('9','2276','ДЕРБЕНЕВ','ВЛАДИМИР','АЛЕКСАНДРОВИЧ','11.03.1960','01.08.2004','92','22','Р','3000','2297');
INSERT INTO STAFF VALUES('10','2290','ИВАНОВА','ЛЮБОВЬ','АНДРЕЕВНА','20.11.1949','01.08.1996','92','8','Р','1000','2297');
INSERT INTO STAFF VALUES('11','2297','ГУЩИНА','ТАМАРА','МИХАЙЛОВНА','29.10.1949','18.11.1995','92 ','8','Р','1000','2332');
INSERT INTO STAFF VALUES('12','2309','ЛАРЦЕВА','ИННА','ВЛАДИМИРОВНА','07.10.1958','01.11.1995','16','7','Р','3000','2372');
INSERT INTO STAFF VALUES('13','2314','АЛИМБЕКОВ','РУСЛАН','СЕРГЕЕВИЧ','21.06.1970','01.03.2016','92','18','Р','3000','2297');
INSERT INTO STAFF VALUES('14','2351','БОЧКИН','ВАЛЕРИЙ','АЛЕКСАНДРОВИЧ','20.10.1956','23.05.1988','25','14','Р','1000','2409');
INSERT INTO STAFF VALUES('15','2353','АНДРИАНОВ','ВЛАДИМИР','ВЛАДИМИРОВИЧ','01.04.1960','01.09.1996','96','5','Р','1000','2414');
INSERT INTO STAFF VALUES('16','2368','БУШУЕВ','КИРИЛЛ','АНАТОЛЬЕВИЧ','10.11.1972','01.09.1995','29','5','Р','3000','2332');
INSERT INTO STAFF VALUES('17','2372','АШИРОВ','АНВАР','РАШИДОВИЧ','28.07.1969','01.11.1995','16','5','Р','1000','2332');
INSERT INTO STAFF VALUES('18','2379','ЗВЕРЕВ','НИКОЛАЙ','ТИМОФЕЕВИЧ','09.02.1960','01.09.1995','46','15','Р','1000','2332');
INSERT INTO STAFF VALUES('19','2381','АНТОШКИН','ОЛЕГ','ИВАНОВИЧ','29.08.1963','01.09.1993','29','22','Р','1000','2368');
INSERT INTO STAFF VALUES('20','2382','ЗАЙЦЕВА','РАИСА','МИХАЙЛОВНА','20.04.1983','02.09.1996','29','20','Р','2500','2368');
INSERT INTO STAFF VALUES('21','2397','СТЕЖКИН','АНДРЕЙ','ВЛАДИМИРОВИЧ','30.09.1973','01.05.2003','52','9','Р','1500','2198');
INSERT INTO STAFF VALUES('22','2402','ПОВЕТКИНА','НИНА','МИХАЙЛОВНА','20.02.1949','01.09.2010','16','5','Р','1500','2309');
INSERT INTO STAFF VALUES('23','2409','ОВСЯННИКОВ','МИХАИЛ','КОНСТАНТИНОВИ','11.12.1976','15.01.2010','22','5','Р','1000','2332');
INSERT INTO STAFF VALUES('24','2414','ВАСИЛЬЕВА','ГАЛИНА','ИВАНОВНА','01.04.1981','01.09.2001','96','5','Р','500','2332');
INSERT INTO STAFF VALUES('25','2419','КАПЕЛИНА','ДИНАРА','АМИРОВНА','22.12.1971','01.09.1999','16','5','Р','1000','2372');
INSERT INTO STAFF VALUES('26','2420','ДОБРЫНИН','СЕРГЕЙ','СЕРГЕЕВИЧ','05.01.1962','01.05.1995','50','12','Р','1500','2332');
INSERT INTO STAFF VALUES('27','2436','ЧЕРНОВА','МАРИНА','НИКОЛАЕВНА','29.08.1974','02.09.1996','22','5','Р','1200','2309');
INSERT INTO STAFF VALUES('28','2332','СОБОЛЕВ','ИВАН','ИВАНОВИЧ','18.12.1969','01.10.1996','1','100','Р','10000','0');

SO here is my Database:

CREATE TABLE courses ( 
ccode     VARCHAR2(10)  CONSTRAINT courses_PK  PRIMARY KEY, 
cname     VARCHAR2(50), 
coursefee NUMBER(6)
);

CREATE TABLE faculty (
fcode     VARCHAR2(5)  CONSTRAINT faculty_PK PRIMARY KEY,
name      VARCHAR2(50)
);

CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,   
ccode varchar2(5) CONSTRAINT batches_ccode_FK  REFERENCES COURSES(ccode),  
fcode varchar2(5) CONSTRAINT batches_fcode_FK  REFERENCES FACULTY(fcode),   
stdate date CONSTRAINT batches_stdate_nn not null,  
enddate date,  
timing number(1) CONSTRAINT batches_timing_chk  check( timing in (1,2,3) ),   
CONSTRAINT batches_date_chk check ( stdate <= enddate) 
);   

CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,   
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),   
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),   
dj date,   
phone varchar2(10),   
email varchar2(30) 
); 

Insert Table:

INSERT INTO batches VALUES('A0001','A0016','A0031','08-05-13','08-06-17',1); 
INSERT INTO batches VALUES('B0002','E0017','B0032','08-05-13','08-06-26',2); 
INSERT INTO batches VALUES('C0003','C0018','C0033','08-06-27','08-06-28',2); 
INSERT INTO batches VALUES('D0004','D0019','D0034','08-06-27','08-08-11',3);
INSERT INTO batches VALUES('E0005','E0020','E0035','08-07-01','08-08-09',2); 
INSERT INTO batches VALUES('F0006','F0021','F0036','08-07-21','08-08-11',1);
INSERT INTO batches VALUES('G0007','V0022','G0037','08-08-31','08-09-21',3); 
INSERT INTO batches VALUES('H0008','H0023','H0038','08-09-17','08-10-14',3);
INSERT INTO batches VALUES('I0009','I0024','I0039','08-09-23','08-10-20',2); 
INSERT INTO batches VALUES('J0010','P0025','J0040','08-10-04','08-11-26',1);
INSERT INTO batches VALUES('K0011','F0026','K0041','08-10-10','08-11-17',2); 
INSERT INTO batches VALUES('L0012','C0027','L0042','08-11-02','09-02-23',2);
INSERT INTO batches VALUES('M0013','M0028','M0043','08-12-09','09-01-02',3); 
INSERT INTO batches VALUES('N0014','N0029','N0044','08-12-11','09-01-08',3);
INSERT INTO batches VALUES('O0015','O0030','O0045','08-12-15','09-01-12',2); 

INSERT INTO students VALUES(001,'A0001','Andrew Johnson','M','08-01-10','677-0323','AndyJ@hotmail.com');
INSERT INTO students VALUES(002,'B0002','Dionne Peterson','F','08-01-10','324-9705','dionnepet@gmail.com');
INSERT INTO students VALUES(003,'C0003','Sydney Peters','F','08-01-10','624-9664','SydersP@yahoo.com');
INSERT INTO students VALUES(004,'D0004','Tsian Figaro','F','08-09-01','310-4957','tsianfigaro@hotmail.com');
INSERT INTO students VALUES(005,'E0005','Jewel Forbes','F','08-09-01','666-5948','Jewel@gmail.com');
INSERT INTO students VALUES(006,'F0006','Jason Fitzpatrick','M','08-01-02','657-4958','JasonFitz@hotmail.com');
INSERT INTO students VALUES(007,'G0007','Ali Maharaj','M','08-01-02','674-4437','AliMaharaj@gmail.com');
INSERT INTO students VALUES(008,'H0008','Mariah Sunderstone','F','08-01-02','707-4056','mariahS@yahoo.com');
INSERT INTO students VALUES(009,'I0009','Ariah Baron','F','08-01-02','673-9685','AriBar@yahoo.com');
INSERT INTO students VALUES(010,'J0010','Joe Zheng ','M','08-09-01','348-4567','Zhengdo@hotmail.com');
INSERT INTO students VALUES(011,'K0011','Donald Yen','M','08-09-01','328-4749','DonnieYen@gmail.com');
INSERT INTO students VALUES(012,'L0012','Jackson Chang','M','08-08-27','345-6847','JackieChang@yahoo.com');
INSERT INTO students VALUES(013,'M0013','Scott Smith','M','08-01-02','702-9903','Scottsmith@hotmail.com');
INSERT INTO students VALUES(014,'N0014','Michelle Shareef','F','08-02-27','321-0589','michelleShareef@gmail.com');
INSERT INTO students VALUES(015,'O0015','Jean Louis Pierre ','M','08-09-01','428-8378','JLPierre@gmail.com');

The error that keeps coming up for batches Table is :
ORA-02290: check constraint (EJAY.BATCHES_DATE_CHK) violated

ANd the error that comes up for students Table is:
ORA-02291: integrity constraint violated — parent key not found

What I’m confused about is that it only shows up for some rows in the tables. faculty and courses are fine, it’s just these two.
If the answers is obvious, I apologize, I’m a beginner. Please help

May 1, 2021

I got ” ORA-02291: integrity constraint (string.string) violated – parent key not found ” error in Oracle database.

ORA-02291: integrity constraint (string.string) violated – parent key not found

Details of error are as follows.

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.




integrity constraint (string.string) violated – parent key not found

This ORA-02291 errors are related with the foreign key value has no matching primary key value.

To solve this error, you need to drop the foreign key or add a matching primary key.

Or firstly you need to insert the same value into the parent table, then you can insert the value into child table.

For example; I have 2 table which has relation between two table with EMPLOYEE_ID column as follows.

CREATE TABLE EMPLOYEE
( EMPLOYEE_ID numeric(10) not null,
NAME varchar2(50) not null,
LAST_NAME varchar2(50),
CONSTRAINT emp_pk PRIMARY KEY (EMPLOYEE_ID)
);

CREATE TABLE MANAGER
( ID numeric(10) not null,
EMPLOYEE_ID numeric(10) not null,
CONSTRAINT fk_EMPLOYEE
FOREIGN KEY (EMPLOYEE_ID)
REFERENCES EMPLOYEE (EMPLOYEE_ID)
);

I have inserted the following record.

INSERT INTO MANAGER (ID, EMPLOYEE_ID) VALUES (10, 63);

But I got this error, because 63 employee_id doesn’t exist in the employee table. So You need to insert this record to parent table, then you can insert the child table as follows.

INSERT INTO EMPLOYEE (EMPLOYEE_ID, NAME, LAST_NAME) VALUES (63, 'Mehmet ', 'Deveci ');

Then you can insert into the MANAGER table:

INSERT INTO MANAGER (ID, EMPLOYEE_ID) VALUES (10, 63);

Or you need to drop the emp_pk constraint.

Do you want to learn Oracle Database for Beginners, then read the following articles.

Oracle Tutorial | Oracle Database Tutorials for Beginners ( Junior Oracle DBA )

About Mehmet Salih Deveci

I am Founder of SysDBASoft IT and IT Tutorial and Certified Expert about Oracle & SQL Server database, Goldengate, Exadata Machine, Oracle Database Appliance administrator with 10+years experience.I have OCA, OCP, OCE RAC Expert Certificates I have worked 100+ Banking, Insurance, Finance, Telco and etc. clients as a Consultant, Insource or Outsource.I have done 200+ Operations in this clients such as Exadata Installation & PoC & Migration & Upgrade, Oracle & SQL Server Database Upgrade, Oracle RAC Installation, SQL Server AlwaysOn Installation, Database Migration, Disaster Recovery, Backup Restore, Performance Tuning, Periodic Healthchecks.I have done 2000+ Table replication with Goldengate or SQL Server Replication tool for DWH Databases in many clients.If you need Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS Consultancy and Training you can send my email adress [email protected].-                                                                                                                                                                                                                                                 -Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS ve linux Danışmanlık ve Eğitim için  [email protected] a mail atabilirsiniz.

Понравилась статья? Поделить с друзьями:
  • Ошибка kerbal space program
  • Ошибка oracle 1555
  • Ошибка err 036
  • Ошибка ldap 81 сервер отключен ошибка win32 58
  • Ошибка kennzeichenl prufen