sql 语句系列(删库跑路系列)[八百章之第七章]

时间:2020-03-25 10:03:27   收藏:0   阅读:53

前言

最开心的章节,没有之一。

删除违反参照完整性的记录

EMP 是员工表,DEPT 是部门表 DEPTNO是部门编号

delete from EMP where not exists (
select null from  DEPT where EMP.DEPTNO=DEPT.DEPTNO 
)
delete from EMP where DEPTNO not in(
select DEPTNO from DEPT
)

删除重复数据

删除名字相同的员工:

delete from EMP where

EMPNO not in(select MIN(EMPNO) from EMP group by ENAME)

原理很简单,保留ENAME 相同的一项即可。

删除被其他表参照的记录

这里面的意思,不是说因为一个表是另外一张表的外键,而无法删除。
而是说,根据一张表来删除另外一张表。
dept_accidents 是一张部门发生事故的表

select * from dept_accidents

技术分享图片

现在有一个需求,就是删除出现3次事故的部门的员工删除。

delete from EMP
where DEPTNO in(select deptno from dept_accidents group by deptno having count(*)>=3)

原文:https://www.cnblogs.com/aoximin/p/12559928.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!