• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
26 views

Problem:

I have following table and i get message 'the insert statement conflicted with the foreign key same table constraint'

EMPID

EMPNAME

DESIGNATION

MANAGERID

101

James

Manager

1

102

Rabin

Group Lead

2

103

Suresh Jana

Group Lead

3

 INSERT INTO [dbo].[tbl_EMPLOYEEMASTER]
       (EMPID, EMPNAME, DESIGNATION, MANAGERID)
VALUES (103, 'Sukamal Jana', 'Group Lead', 4);     

GO

Msg 547, Level 16, State 0, Line 1

The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_MANAGERID_tbl_EMPLOYEEMASTER". The conflict occurred in database "PRACTICE_DB", table "dbo.tbl_EMPLOYEEMASTER", column 'EMPID'.

The statement has been terminated.

by (330 points)  

1 Answer

0 votes

Solution:

This looks like  you are inserting a value in the child table whereas the  parent table value does not exist.

When you insert a value in foreign key column , it must be null or point to existing reference to a row in the other table.

 Foreign key always refers to the another column of same table.

  Correct Inserts

   INSERT INTO [dbo].[tbl_EMPLOYEEMASTER]

       (EMPID, EMPNAME, DESIGNATION, MANAGERID)

      VALUES (101, 'Sudip Das', 'Manager', 1);

        GO

  CONCLUSION:

Manager ID 4 was not present in the table was the cause of error.

by (3.3k points)  
...