如何删除表中已经不存在的主键(急!) 有一个表,两个字段:id,id_foreign.其中id_foreign作为外键引用id的值(也就是自相关)。现在有一个问题,id_foreign引用的主键不存在,请问如何查询出这些不存在的主键并把这条记录删除?在线等求助,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 delete tbwhere not exists( select 1 from tb t where t.id_foreign=tb.id) 你别着急,现在说的有点看不懂,你自己懂了,说得在明白点haoma? 比如:1,null2,13,14,2第四条记录id_foreign的值为2,但是整个表没有id为2的记录,就是如何查出第四条记录注:id_foreign为null不需要查出 你的意思是:1,null3,14,35,46,2把6,2这条删掉是吗?我要下班了,回家给你看看。 delete form table tbwhere tb.id_foreign not in{ select tb1.id from table tb1} delete tbwhere id_foreign not in(select id_foreign from tb where id=id_foreign) 连接服务器中的访问接口字符串意思? 求一存储过程 外连接,求SQL语句。 有人向有一百万条记录的数据表里面插入过数据吗? 即时更新事务复制求救 高度难动态BOM成本计算 触发器修改与删除的操作. ___数据备份问题!请进!___ 触发器中能不能调用外部可以执行程序? SQL Server 2008r2 Express安装问题 300万数据下执行速度问题 多长时间是正常的? 请高手给我分析这条sql语句的具体意思,O(∩_∩)O谢谢
where not exists(
select 1
from tb t
where t.id_foreign=tb.id)
1,null
2,1
3,1
4,2
第四条记录id_foreign的值为2,但是整个表没有id为2的记录,就是如何查出第四条记录
注:id_foreign为null不需要查出
1,null
3,1
4,3
5,4
6,2
把6,2这条删掉是吗?我要下班了,回家给你看看。
where tb.id_foreign not in{
select tb1.id from table tb1
}
delete tb
where id_foreign not in(select id_foreign from tb where id=id_foreign)