Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005
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 1:16am
ntitish1.5K
Comments
CShubham at 22-Feb-13 6:19am
   
if you have some code put here!!!!!!
ntitish at 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 at 22-Feb-13 6:24am
   
insert data in parent table first and then in child table
Vardhan Desai at 22-Feb-13 6:25am
   
if you have some code put here else what is the error message?
Ankur\m/ at 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 at 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 at 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 at 22-Feb-13 7:30am
   
yes nitish you are right !!!! if id(pk)=1 so id(fk) must be 1!!!!!
ntitish at 25-Feb-13 0:09am
   
thanks to every one.....

1 solution

Rate this: bad
good
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
  Permalink  
Comments
ntitish at 25-Feb-13 0:09am
   
thanks sir....
S. M. Ahasan Habib at 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)

  Print Answers RSS
0 OriginalGriff 499
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2014
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