-1

Delete From Department_Master Where Active = 0

this is my Query When I try to delete some records where Active is 0 its getting me error The DELETE statement conflicted with the REFERENCE constraint "FK__Employee___Depar__29572725". The conflict occurred in database "abc", table "dbo.Employee_Details", column 'Department_Id'.The statement has been terminated. How to resolve

How to Solve this error

Jan
  • 3,825
  • 3
  • 31
  • 51
Om C
  • 1
  • 1
  • 2
    First you need to delete records from the table dbo.Employee_Details for these departments (Department_Master Where Active = 0) – Sergey Mar 20 '23 at 07:44
  • At a guess, you have employees linked to the department you are trying to delete – Matt Evans Mar 20 '23 at 07:44
  • you have to delete related Employee_Details data first, before delete the Department_Master data. As there is data from Employee_Details (FK) linked to Department_Master data (PK) and removing PK data will caused error. – Harlo Mar 20 '23 at 07:46

1 Answers1

0

You can't just delete a parent record and leave orphan child records behind. Either you want the database to automatically delete related records or you don't. If you don't, you need to manually delete the related records first. If you do, you need to configure that relation and specify to cascade on delete. The alternative would be to update all the child records and change their parent first, then delete ethe old parent.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46