sql - Constraints interdependent tables -
this question has answer here:
i created 2 tables dependent on each other this.
create table a(no1 number(2) primary key,no2 number(2));
table created.
create table b(no1 number(2) primary key,no2 number(2));
table created.
alter table add constraint aa foreign key(no2) references b(no1);
table altered.
alter table b add constraint bb foreign key(no2) references b(no1);
table altered.
insert values(10,20); insert values(10,20);
error @ line 1: ora-02291: integrity constraint (subk.aa) violated - parent key not found
insert b values(10,20); insert b values(10,20);
error @ line 1: ora-02291: integrity constraint (subk.bb) violated - parent key not found
how insert data in table a
, b
you create invalid constraints:
alter table b add constraint bb foreign key(no2) references b(no1);
foreign key should different table, not column in same table.
if want circular constraint -> b , b ->a can insert first data , add constraints. foreign key should direct primary key, new sql :
create table a(no1 number(2) primary key,no2 number(2)); create table b(no1 number(2) primary key,no2 number(2)); insert values(10,20); insert b values(10,20); alter table add constraint aa foreign key(no1) references b(no1); alter table b add constraint bb foreign key(no1) references a(no1);
Comments
Post a Comment