sql - how to delete data from table which has self referencing foreign key column

Опубликовано: 02 Ноябрь 2024
на канале: Tableau Tip
17,585
62

UPDATE employee SET manager_id=NULL

DELETE FROM employee

How to delete data from Table which has self referecing
foreign key

In this table Manager_Id is referring customer_id

Now we try to delete data from this table

Error Code: 1451
Cannot delete or update a parent row: a foreign key constraint fails (`my_retail_data`.`employee`, CONSTRAINT `employee_fk0` FOREIGN KEY (`manager_id`) REFERENCES `employee` (`employee_id`))

to overcome, first we need set data in mangaer_id column
as nulls

Try, deleting data now from the Employee table

Done!