Click here to Skip to main content
14,836,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
[no name] 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
id(PK)
class_name
class_id
table-2 student_details
id(FK)
student_name
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
MalwareTrojan 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.
[no name] 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.
[no name] 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

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
   
Comments
ntitish 25-Feb-13 0:09am
   
thanks sir....
S. M. Ahasan Habib 25-Feb-13 0:11am
   
wecome:)

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900