Click here to Skip to main content
13,050,617 members (93,739 online)
Rate this:
Please Sign up or sign in to vote.
i am having parent table and child table with primary key and foreign key relationship

if i am inserting the record in child table means it is showing some error..
Posted 22-Feb-13 0:16am
CShubham 22-Feb-13 6:19am
if you have some code put here!!!!!!
ntitish 22-Feb-13 6:51am
sir i am struggling with logic is that possible are i dont know....i will explain my problem clearly
i am having two tables class details and student details
table1- class table
table-2 student_details

actually what was my problem i am saving class details from front end through dropdown i hard coded it
but when i am inserting student_details it is showing an error due to foreign key relation ship the query is terminated
Om Prakash Pant 22-Feb-13 6:24am
insert data in parent table first and then in child table
Vardhan Desai 22-Feb-13 6:25am
if you have some code put here else what is the error message?
Ankur\m/ 22-Feb-13 6:44am
When you have a foreign key constraint, you need to make sure that while inserting a new record in the child table, the foreign key column must be a value that's in the parent table. Otherwise it will throw an error.
You need to give us the error details, or first Google that to get more identify and solve the problem.
CShubham 22-Feb-13 6:58am
yes nitish !!! because of you use pk and fk that's why it will show error!!! because when data is not available on first table and you try to add data in second table it show error
make sure first table id(pk ) and second table id(fk) will be same!!! if not you get error!!!!
ntitish 22-Feb-13 7:14am
sir that means first table id(pk) = second table id(fk) = 1 is that write sir if the value is like that means it will insert ha sir.
CShubham 22-Feb-13 7:30am
yes nitish you are right !!!! if id(pk)=1 so id(fk) must be 1!!!!!
ntitish 25-Feb-13 0:09am
thanks to every one.....

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The table which contain primary key can called Parent table and the table which contain foreign key can called child table. What is a foreign key? It is a key which refer to another table primary key. So when this scenario comes then you must insert primary key first. Without primary key foreign key value is invalid, only you can store foreign key value that is present to primary/parent table or if it is nullable then can store null. So first insert data to primary/parent table then next insert foreign/child table
ntitish 25-Feb-13 0:09am
thanks sir....
S. M. Ahasan Habib 25-Feb-13 0:11am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100