Problem Statement: I wanted to insert rows into “Company” table when i tried to insert row into “Company” table i was getting below error message.
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint “FK__Company__status__164452B1″. The conflict occurred in database “MyDB”, table “dbo.dept”, column ‘status’.
The statement has been terminated.
The above error message is very common and occurs when we try to insert a row in child table(foreign key table) which has a key that is not present in master table(Primary key table). This article will help you in finding list of tables on which our child table is dependent. So that we can insert data into those tables first and then try to insert data into child table. Below is the query that can be used for our purpose.
SELECT OBJECT_NAME(REFERENCED_OBJECT_ID) AS MASTERTABLE FROM SYS.FOREIGN_KEY_COLUMNS A
JOIN SYS.COLUMNS B ON A.PARENT_COLUMN_ID =B.COLUMN_ID AND A.PARENT_OBJECT_ID=B.OBJECT_ID
JOIN SYS.COLUMNS C ON A.CONSTRAINT_COLUMN_ID=C.COLUMN_ID AND A.REFERENCED_OBJECT_ID=C.OBJECT_ID
In the above screenshot, we can see that there are five tables which are linked with “Company” table and has a foreign key relation with “Company” table. I hope you will find this article useful.
Note: Above query will not be supported in SQL Server 2000 version.
826 total views, 1 views today