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

905 views Asked by At

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?

1

There are 1 answers

5
NAJ22 On

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.