-1

我已经尝试了所有方法,但插入数据时仍然出错。

  create table Address(
  AddressID integer primary key,
  StreetnameID integer,
  CountyID integer,
  CityID integer,
  PostcodeID integer,
  DoornumID integer,
  foreign key (StreetnameID) references Streetname,
  foreign key (CountyID) references County,
  foreign key (CityID) references City,
  foreign key (PostcodeID) references Postcode,
  foreign key (DoornumID) references Doornum
);

这是我的地址创建表。下面是我失败的插入语句。

INSERT INTO Address(AddressID, StreetnameID, CountyID, CityID, PostcodeID, DoornumID)
values(seq_AddressID.nextval, 1, 1, 1, 1, 1)

我不知道是什么导致这个问题产生这个错误:

ORA-02291: integrity constraint (ROOT.SYS_C007211) violated - parent key not found
4

2 回答 2

2

外键引用之一不存在。它们都必须存在。如果您命名约束,这将更容易弄清楚:

create table Address (
  AddressID integer primary key,
  StreetnameID integer,
  CountyID integer,
  CityID integer,
  PostcodeID integer,
  DoornumID integer,
  constraint fk_address_StreetnameID foreign key (StreetnameID) references Streetname,
  constraint fk_address_CountyID foreign key (CountyID) references County,
  constraint fk_address_CityID foreign key (CityID) references City,
  constraint fk_address_PostcodeID foreign key (PostcodeID) references Postcode,
  constraint fk_address_DoornumID foreign key (DoornumID) references Doornum
);

该约束要求引用表中存在相应的行,然后才能将这些行插入address.

于 2019-04-05T18:24:36.710 回答
0

ORA-02291: 违反完整性约束 (ROOT.SYS_C007211) - 未找到父键

对于插入语句,当您尝试插入没有匹配父项的子项(由外键约束定义)时,此 ORA-02291 错误很常见。在这种情况下,您需要将父行添加到表中,然后重新插入子表行。

因此,请检查您错过的父表,以将尝试在此处插入的数据放入当前子表中

于 2019-04-05T18:29:12.443 回答