Click here to Skip to main content
15,891,783 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.

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
 
Share this answer
 
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