Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
create table foreign_2
( id int,
age int,
ph_no int
foreign key(id) references foreign_1(id)
on delete cascade on update set null
);

insert into foreign_2 values(1,18,8988477);
insert into foreign_2 values(2,15,8988477);
insert into foreign_2 values(3,12,1098777);
insert into foreign_2 values(4,10,225555477);
insert into foreign_2 values(5,17,87525483);


this query is not completed why?
an error occured that...
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__foreign_2__ph_no__3D491139". The conflict occurred in database "master", table "dbo.foreign_1", column 'id'.
The statement has been terminated.

What I have tried:

i tried to insert the data in the foreign_2 table
Posted
Updated 15-Apr-16 21:24pm
v3
Comments
Karthik_Mahalingam 16-Apr-16 3:28am    
make sure that the ids 1,2,3,4,5 is present in foreign_1 table

1 solution

The error is pretty explicit:
The INSERT statement conflicted with the FOREIGN KEY constraint
What that mean sis that you tried to insert a value into your table (specifically into the id column) which doesn't exist in the matching column in the other table (specifically, the ID column of the foreign_1 table).
You can't do that: a foreign key means "this table is related to that table" - and you can't create a row which doesn't have matching value in the column of the other table.
 
Share this answer
 
Comments
Prachi balodhi 16-Apr-16 3:38am    
ok thnx

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