如何从包含自引用外键的表中删除所有数据 [英] How to Delete all data from a table which contain self referencing foreign key

查看:143
本文介绍了如何从包含自引用外键的表中删除所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在其内部定义了员工关系的表。
ie

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

等等...



其中,高级ID是其主键表相同的外键with refrence column EmpId
我要清除此表中的所有行,而不删除任何约束。我如何做到这一点?



删除需要执行如下
4,3,2,1


$ b b

我如何做这



Jhonny的回答

解决方案

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

  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

这篇关于如何从包含自引用外键的表中删除所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆