SQL ORA-02291: integrity constraint violated - parent key not found - sql

I have encountered some problem with SQL foreign key.
Here are my table and insert SQL.
create table passenger_card2
(
phone char(20) primary key,
name char(20)
);
create table card
(
card_num char(20) primary key,
balance number(10,2),
cvn char(20)
);
create table passenger_card1
(
sin integer primary key,
user_id char(20) not null unique,
phone char(20),
card_num char(20) unique,
foreign key(phone) references passenger_card2,
foreign key (card_num) references card
);
And here are my INSERT statements:
INSERT INTO PASSENGER_CARD2 VALUES ( '111222333' , 'Ace');
INSERT INTO CARD VALUES ( '1000' , '100.1' , '110');
INSERT INTO PASSENGER_CARD1 VALUES ('100', 'aaaa', '111222333', '1000');
However, I get an error when I tried to insert PASSENGER_CARD1 data:
SQL ORA-02291: integrity constraint violated - parent key not found
I am not sure why my foreign key is wrong?

I am not sure if this is going to be right but you should make the table 2 first before you create the first table. The database is confused because it wouldnt make sense telling them that theres gonna be a foreign key in the second table but the table isnt created. Run the code for the second table first and then run the code for the first table.

Related

Integrity Constraint violated error when trying to insert into tables

I need a new set of eyes to help me understand what I'm doing wrong here!
I created these tables:
CREATE TABLE bankkund(
PNR VARCHAR2(11) PRIMARY KEY,
FNAMN VARCHAR2(25) NOT NULL,
ENAMN VARCHAR2(25) NOT NULL,
PASSWD VARCHAR2(16) NOT NULL,
UNIQUE(PASSWD));
CREATE TABLE konto(
KNR NUMBER(8)PRIMARY KEY,
KTNR NUMBER(6)NOT NULL,
REGDATUM DATE NOT NULL,
SALDO NUMBER(10,2),
FOREIGN KEY(ktnr) REFERENCES kontotyp(ktnr));
CREATE TABLE kontoägare(
RADNR NUMBER(9)PRIMARY KEY,
PNR VARCHAR2(11)NOT NULL,
KNR NUMBER(8)NOT NULL,
FOREIGN KEY(pnr) REFERENCES bankkund(pnr),
FOREIGN KEY(knr) REFERENCES konto(knr));
then this sequence:
create sequence radnr_seq
start with 1
increment by 1;
and then whenever I want to insert the following values I get an integrity constraint error:
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'540126-1111',123);
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'691124-4478',123);
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'540126-1111',5899);
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'691124-4478',8896);
COMMIT;
What am I missing??
The error you're seeing is expected. Table "kontoägare" has 2 foreign keys: FOREIGN KEY(pnr) REFERENCES bankkund(pnr)and FOREIGN KEY(knr) REFERENCES konto(knr)). This means that the values for "pnr" need to exist in table "bankkund" and the value for "knr" needs to exist in "konto". If either of those values do not exist when you do the insert, an integrity constraint error will be raised.

Error report - ORA-02291: integrity constraint violated - parent key not found

so I'm writing some code in Oracle and have established the following tables:
CREATE TABLE users
(
user_id NUMBER NOT NULL,
email_address VARCHAR2(50) NOT NULL UNIQUE,
first_name VARCHAR2(10) NOT NULL,
last_name VARCHAR2(10) NOT NULL,
CONSTRAINT users_pk PRIMARY KEY (user_id)
)
CREATE TABLE product
(
product_id NUMBER,
product_name VARCHAR2(50) NOT NULL,
CONSTRAINT product_pk PRIMARY KEY (product_id)
)
CREATE TABLE downloads
(
download_id NUMBER,
user_id NUMBER NOT NULL,
product_id NUMBER NOT NULL,
download_date DATE NOT NULL,
filename VARCHAR2(50) NOT NULL,
CONSTRAINT downloads_pk PRIMARY KEY (download_id),
CONSTRAINT downloads_fk
FOREIGN KEY(user_id) REFERENCES users (user_id),
CONSTRAINT downloads_fk2
FOREIGN KEY(product_id) REFERENCES product(product_id)
)
CREATE SEQUENCE user_id_seq
CREATE SEQUENCE download_id_seq
CREATE SEQUENCE product_id_seq
The downloads table connects the users and product table, containing the foreign keys user_id and product_id. I am trying to insert data into the downloads table via the following code:
INSERT INTO downloads (download_id,user_id,product_id,download_date,filename)
VALUES(download_id_seq.NEXTVAL,1,2, SYSDATE, 'one_horse_town.mp3')
INSERT INTO downloads (download_id,user_id,product_id,download_date,filename)
VALUES(download_id_seq.NXTVAL, 2, 1, SYSDATE, 'pedals_are_falling.mp3')
INSERT INTO downloads (download_id,user_id,product_id,download_date,filename)
VALUES(download_id_seq.NEXTVAL, 2, 2, SYSDATE, 'random_song.mp3')
Oracle then gives me the following error:
Error report - ORA-02291: integrity constraint (BC29369.DOWNLOADS_FK2) violated - parent key not found.
I am not sure why this is coming up, as it seems to me that I have clearly labeled the primary and foreign key relationships as I should. Does anyone know how to fix this? Thank you in advance!
The constraints
CONSTRAINT downloads_fk FOREIGN KEY(user_id) REFERENCES users (user_id)
and
CONSTRAINT downloads_fk2 FOREIGN KEY(product_id) REFERENCES product(product_id)
means that the user_id values 1 and 2, the product_id values 1 and 2 which you are trying to insert into downloads table must already exist in users table and product table.
If users and product tables are not populated with these values (1 and 2) before you insert those values into downloads table, you will get this error as the integrity constraints will be violated.
You are creating an orphan record with no parent record. This is precisely what you were ensuring not to happen when you created those two constraints.

ORA-01748: only simple column names allowed here in Oracle

What I am trying to do ?
I am trying to create two tables and at the same time i am trying to link them together using foreign and primary keys. However I successfully create my parent table ( Student with primary key ) but failed to create child table ( Attendence with foreign key ).
What is the problem ?
I get the following error while creating Attendence table:
ERROR at line 5: ORA-01748: only simple column names allowed here
My code:
Student table:
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Attendence table:
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk Attendence.ST_ROLLNO foreign key references Student(ST_ROLLNO)
);
Your foreign key constraint syntax is wrong; it should be:
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
You are preceding the FK column name with the table name, which is wrong in itself, but also have it in the wrong place.
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Table STUDENT created.
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
);
Table ATTENDENCE created.
According to oracle documentation,
ORA ERR
ORA-01748 only simple column names allowed here
The following is the cause of this error:
This SQL statement does not allow a qualified column name, such as
username.table.column or table.column.
Action you can take to resolve this issue: Remove the qualifications
from the column and retry the operation.
In your case, you are trying to refer to the table name while defining a constraint -
Attendence.ST_ROLLNO - WRONG.
It must contain a simple name without the table name or schema name.

Reference a foreign key to a different type

Is it possible to do this in PL/SQL:
CREATE TABLE ROOM_TYPE
(
ROOM_TYPE_ID NUMBER NOT NULL PRIMARY KEY,
REF_SERVICE_PROVIDER NUMBER NOT NULL,
ROOM_TYPE_NAME1 VARCHAR2(100) NOT NULL,
ROOM_TYPE_NAME2 VARCHAR2(100),
REF_SERVICE_ID NUMBER(15,3),
ROOM_AC NUMBER(1),
ROOM_BATH_ROOM NUMBER(1),
ROOM_CHOICE_OF_MEAL NUMBER(1),
CONSTRAINT fk_ref_service_provider
FOREIGN KEY (REF_SERVICE_PROVIDER)
REFERENCES PROVIDER(PROVIDER_ID),
CONSTRAINT fk_ref_service_id
FOREIGN KEY (REF_SERVICE_ID)
REFERENCES MEDICAL_SERVICE(SERVICE_ID)
);
While the REF_SERVICE_ID of type number(15,3) is a foreign key in this table which refers to a primary key SERVICE_ID of type number.
The question is, can I refer to a primary key of type number while the foreign key is number(15,3) type !?
Yes you can, but there is a big chance getting following error:
ORA-01438: value larger than specified precision allowed for this column
For eg:
CREATE TABLE A ( ID NUMBER NOT NULL PRIMARY KEY);
CREATE TABLE b
(
id NUMBER(15,3),
val CHAR(1),
CONSTRAINT b_pk FOREIGN KEY (ID) REFERENCES a(id)
);
INSERT INTO A VALUES(456);
INSERT INTO B VALUES( 456, 'X'); --inserts successfully
INSERT INTO A VALUES(12345678901234567890);
INSERT INTO B VALUES( 12345678901234567890, 'X'); --errors

ORA-02291: integrity constraint (NAVY10.SYS_C00317513) violated - parent key not found

I have to create tables and insert the data using INSERT SQL statements and SQL LOADER utility. I am using Oracle 10g.
the following are the create table statements:
CREATE TABLE Employee_C (
EID char(3),
Name varchar2(20),
Salary number(7,2),
MID char(3),
PRIMARY KEY (EID),
FOREIGN KEY (MID) REFERENCES Employee_C (EID)
);
CREATE TABLE Conference_C (
ConfID char(6),
Title varchar2(20),
Location varchar2(20),
Sdate date,
PRIMARY KEY (ConfID)
);
CREATE TABLE Topic_C (
Tnum char(3),
Title varchar2(20),
PRIMARY KEY (Tnum)
);
CREATE TABLE Includes_C (
Tnum char(3),
ConfID char(6),
PRIMARY KEY (Tnum,ConfID),
FOREIGN KEY (Tnum) REFERENCES Topic_C,
FOREIGN KEY (ConfID) REFERENCES Conference_C
);
CREATE TABLE Deals_C (
EID char(3),
ConfID char(6),
PRIMARY KEY (EID,ConfID),
FOREIGN KEY (EID) REFERENCES Employee_C,
FOREIGN KEY (ConfID) REFERENCES Conference_C
);
I used INSERT SQL statements to insert data into Topic_C table, and used sql loader utility to insert data into other 4 tables.
So my problem is when I run sql loader, I get the following error:
Record 1: Rejected - Error on table INCLUDES_C.
ORA-02291: integrity constraint (NAVY10.SYS_C00317513) violated - parent key not found
The other tables are working perfectly fine!
I don't know where I am going wrong, please clarify
Thank You
As #OMGPonies mentioned above, when you're loading records into Includes_C one or both of the following is happening:
Includes_C.Tnum isn't in Topic_C
Includes_C.ConfID isn't in Conference_C
The best approach would be to load the parent tables first, then work down through the children. To do that, just load the tables in the order you've defined them. If that doesn't work it means you truly do have a Tnum and/or ConfID value that violates the foreign key.