首页 - 数据库 - SQL Server

删除a表中和b表相同的数据

发布时间: 2007-04-19 05:04    作者: 未知    来源: 未知    浏览:    评论


软件环境:
1、Windows NT4.0+ORACLE 8.0.4
2、ORACLE安装路径为:C:ORANT
问题提出:
在做数据转储业务的时候,如果发生操作错误,有可能出现主表和副表中都有同一种数据,这样结算的结果就有可能发生错误。


实现方法:
SQL> create table a (
2 bm char(4), --编码
3 mc varchar2(20) --名称
4 )
5 /
表已建立.
SQL> insert into a values('1111','1111');
SQL> insert into a values('1112','1111');
SQL> insert into a values('1113','1111');
SQL> insert into a values('1114','1111');
SQL> insert into a values('1115','1111');
SQL> create table b as select * from a where 1=2;
表已建立.
SQL> insert into b values('1111','1111');
SQL> insert into b values('1112','1111');
SQL> insert into b values('1113','1111');
SQL> insert into b values('1114','1111');
SQL> commit;
完全提交.
SQL> select * from a;
BM MC
---- --------------------
1111 1111
1112 1111
1113 1111
1114 1111
1115 1111
SQL> select * from b;
BM MC
---- --------------------
1111 1111
1112 1111
1113 1111
1114 1111

方法一:exists子句
SQL> delete from a where exists (select 'X' from b where a.bm=b.bm and a.mc=b.mc);
删除4个记录.
where条件:如果两个表中都拥有相同字段的主键(primary key),则只需比较两个主键就可以了
方法二:in子句
SQL> delete from a where (bm,mc) in (select bm,mc from b);
删除4个记录.
SQL> select * from a;
BM MC
---- --------------------
1115 1111
实际测试结论:
在表不是很大时,用in子句速度还可以忍受,而如果记录量很多时(十万条以上),in子句简直让人难以人忍受,速度奇慢。

TAG

Smile Big Smile Surprise Stick out tongue Wink Sad Tongue Tied Indifferent Crying Embarrassed Cool Angry Angel Devil [8-|] [:#] [:-*] [:^)] [<:o)] [|-)] Yes Beer Left Hug Music Star Time Snail Pizza Automobile Umbrella Computer Storm [mo] [8o|] [^o)] [+o(] [*-)] [8-)] Coffee No Drinks [Z] Right Hug Cake Broken Heart Gift Wilted Flower Movie Dog Idea Sleep Email Travel Paradise
呢称:

加粗 斜体 下划线 链接 图片 代码 邮件地址 引用 列表

最多只能输入100个字符

Tags

SQL 数据库 asp.net C# XML 控件 .NET教程 程序 事件 数据 安全 代码 Server 客户端 验证 数据库专栏 接口 文件 Oracle DataSet 函数 DataGrid 问题 .net return C#语言 JavaScript 服务 IIS 对象 语句 windows 继承 时间 web.config 设计 开发 参数 变量 解决 字符 ADO.net 环境 VB.Net语言 web 异常 工具 服务器 计算 实例 OLEDB Application VB Word WebService insert asp net 安装 记录

精华推荐

更多

精品下载

更多