ORA-02291:违反了完整性约束(INA.member#mem_id) - 未找到父键

时间:2018-06-03 23:14:59

标签: database oracle insert key parent

错误报告 - ORA-02291:违反了完整性约束(INA.member#mem_id) - 找不到父密钥。

                            Thanks very much, and i apologize for my bad english

尝试运行获取错误消息时 ORA-02291:违反了完整性约束(INA.member#mem_id) - 未找到父键。 真的很感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

假设您还有两个表,即socialClubsclubMembership,以及必要的约束(primary foreign),定义如下:

SQL> create table socialClubs(   -- parent(look-up) table for "clubMembership" table.
  2                           id        int primary key,
  3                           name      varchar2(500)
  4                           ); 

Table created
SQL> create table clubMembership( -- parent table for "member" table.
  2                               id      int primary key,
  3                               club_id int,
  4                               constraint fk_cmmb_sc_id
  5                                  foreign key(club_id)
  6                               references socialClubs(id)
  7                              ); 

Table created
SQL> create table member(
  2                      mem_id         int primary key,
  3                      mem_address    varchar2(500),
  4                      address_type   varchar2(10),
  5                      effective_date date,
  6                      end_date       date,
  7                      adress         varchar2(500),
  8                      city           varchar2(50),
  9                      zip_code       varchar2(10),
 10                      phone_number   number(15),
 11                      last_name      varchar2(50),
 12                      first_name     varchar2(50),
 13                      constraint fk_cmmb_mem_id
 14                         foreign key(mem_id)
 15                      references clubMembership(id)
 16                      );

Table created
SQL> insert into socialClubs values(1,'Mathematics');

1 row inserted
SQL> insert into member
  2    (mem_id,
  3     mem_address,
  4     address_type,
  5     effective_date,
  6     end_date,
  7     adress,
  8     city,
  9     zip_code,
 10     phone_number,
 11     last_name,
 12     first_name)
 13  values
 14    (19889218,
 15     191166765,
 16     'Z2',
 17     date'2013-08-01',
 18     date'2016-05-07',
 19     '45 NEWYORK',
 20     'ATLANTIC NY',
 21     011101,
 22     2012922341,
 23     'BOB',
 24     'GUY');


ORA-02291: integrity constraint (ASKIMEMUR.FK_CMMB_MEM_ID) violated - parent key not found

引发 ORA-02291错误

因为 clubMembership 表中 member_id 19889218的成员表中找不到(父)记录,该表具有内联定义constraint fk_cmmb_mem_id foreign key(mem_id) references clubMembership(id)

因此,将必要的member_id插入 clubMembership 表并继续进行,不会有任何问题:

SQL> insert into clubMembership values(19889218,1);

1 row inserted
SQL> insert into member
  2    (mem_id,
  3     mem_address,
  4     address_type,
  5     effective_date,
  6     end_date,
  7     adress,
  8     city,
  9     zip_code,
 10     phone_number,
 11     last_name,
 12     first_name)
 13  values
 14    (19889218,
 15     191166765,
 16     'Z2',
 17     date'2013-08-01',
 18     date'2016-05-07',
 19     '45 NEWYORK',
 20     'ATLANTIC NY',
 21     011101,
 22     2012922341,
 23     'BOB',
 24     'GUY');

1 row inserted

SQL> commit;

Commit complete