新建了一个表,语句如下:
create table course
(cno char(4) primary key,
 cname char(40),
 cpno char(4),
 ccredit smallint,
 foreign key cpno references course(cno)
);插入时语句为:
insert into course values('1','database','5','4')执行时出现了问题:ORA 02291: 违反完整约束条件 (SCOTT.SYS_C005172) 未找到父项关键字这里cpno相对应的参照列是cno,已经赋值了。
我用的是PLSQL Developer
各位大侠帮帮忙

解决方案 »

  1.   

    这个可能应为在你的 course 表里 所有数据行的字段cno的值中没有'4' 所以你在别的地方 加入'4'位外键值 Oracle会去相对应的主键中找这个值进行关联 如果没找到 就会报错
      

  2.   


    ORA-02291: integrity constraint (string.string) violated - parent key not found 
    Cause: A foreign key value has no matching primary key value.
     
    Action: Delete the foreign key or add a matching primary key.
     create table course 
    (cno char(4) primary key, 
    cname char(40), 
    cpno char(4), 
    ccredit smallint, 
    foreign key cpno references course(cno) 
    ); 楼主的表设计的有问题呀, 怎么外键也是关连自己呀, 这样你怎么插入呀. 外键是用来关联其他表的,要是不需要关联,不建外键就可以了. 
      

  3.   


    可以这样设计,cpno表示先行课,是外码,被参照表是course,也就是本身,被参照列是cno,只有cno有相对应值的时候cpno才允许插入。
      

  4.   

    因为关联的记录‘5’不存在。
    先插入
    insert into course values('5','test','5',0);