且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从包含自引用外键的表中删除所有数据

更新时间:2023-01-20 12:00:12

我不知道我是否缺少某些东西,但是也许你可以试试这个。

  UPDATE employee SET SeniorID = NULL 
DELETE FROM employee


I have a table which has employee relationship defined within itself. i.e.

EmpID   Name  SeniorId
-----------------------
1         A      NULL
2         B       1
3         C       1
4         D       3

and so on...

Where Senior ID is a foreign key whose primary key table is same with refrence column EmpId I want to clear all rows from this table without removing any constraint. How can i do this?

Deletion need to be performed like this 4, 3 , 2 , 1

How can I do this

EDIT:

Jhonny's Answer is working for me but which of the answers are more efficient.

I don't know if I am missing something, but maybe you can try this.

UPDATE employee SET SeniorID = NULL
DELETE FROM employee