dsf12313 2012-11-29 21:26
浏览 104

子表中的外键如何自动获取它在父表中实际拥有的值

Can anybody tell whether a foreign key in a child table, can automatically get the value it actually has in the parent table? I has two tables student and course table.Student SSC is foreign key in course.I am inserting data correctly into student table.But It gives me an error when I insert data into course table?Any suggestions? Values of variables are got via post method of form.

$query1=mysql_query("INSERT INTO course VALUES('','$subject','$total','$attendce','$ssn')")or die(mysql_error());

This is error I am getting....

Cannot add or update a child row: a foreign key constraint fails (`cast_db`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY (`cnic`) REFERENCES `student` (`cnic`) ON DELETE CASCADE ON UPDATE CASCADE)

  • 写回答

1条回答 默认 最新

  • dsf23223 2012-11-29 21:31
    关注

    That's not how foreign keys work. You have to create a parent record first, get that record's ID (whatever the primary key happens to be) value, and then use that value in your child record insertions.

    Remember - a parent table can have MANY records. There is no way for a database to know which of those records is the parent of a new child record. You HAVE to explicitly say that "this new child record has parent X".

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)