1.唯一约束取出 select MatlCode from t1 union select MatlCode from t2;2.完全並集: select MatlCode from t1 union all select MatlCode from t2;
union:去除相同的部分 union all:全部
以前这里的帖子,希望对你有用怎样删除重复的记录1..先创建一个空表,create table ... as select * from ... where rownum <= 1; 然后将原来的表的数据插入进新表:insert into ... select a, b, c from ... group by a, b, c; 就用新表代替旧表可以了。 2..http://www.oradb.net/sql/find0.htm 软件环境: 1、Windows NT4.0+ORACLE 8.0.4 2、ORACLE安装路径为:C:\ORANT问题提出: 1、当我们想要为一个表创建唯一索引时,如果该表有重复的记录,则无法创建成功。 方法原理: 1、Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的, rowid确定了每条记录是在ORACLE中的哪一个数据文件、块、行上。2、在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中 那些具有最大rowid的就可以了,其余全部删除。3、以下语句用到了3项技巧:rowid、子查询、别名。实现方法: 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 select * from a;插入4个记录.SQL> commit;完全提交.SQL> select rowid,bm,mc from a;ROWID BM MC ------------------ ---- ------- 000000D5.0000.0002 1111 1111 000000D5.0001.0002 1112 1111 000000D5.0002.0002 1113 1111 000000D5.0003.0002 1114 1111 000000D5.0004.0002 1111 1111 000000D5.0005.0002 1112 1111 000000D5.0006.0002 1113 1111 000000D5.0007.0002 1114 1111查询到8记录. 查出重复记录 SQL> select rowid,bm,mc from a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);ROWID BM MC ------------------ ---- -------------------- 000000D5.0000.0002 1111 1111 000000D5.0001.0002 1112 1111 000000D5.0002.0002 1113 1111 000000D5.0003.0002 1114 1111删除重复记录 SQL> delete from a a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);删除4个记录.SQL> select rowid,bm,mc from a;ROWID BM MC ------------------ ---- -------------------- 000000D5.0004.0002 1111 1111 000000D5.0005.0002 1112 1111 000000D5.0006.0002 1113 1111 000000D5.0007.0002 1114 1111 3..:SQL> create table ori(id number,name char(122)) storage(initial 1m next 1m pctincrease 0 maxextents unlimited); 2:SQL> begin 2 dbms_random.initialize(100); 3 for i in 1..1000000 loop 4 insert into ori values(i, 5 chr(mod(trunc(abs(dbms_random.random)),127))); 6 end loop; 7 dbms_random.terminate; 8 commit; 9 end; 10 / 3:SQL> delete ori where id in //------notice 2 ( select id from ori 3 minus 4 select min(id) from ori 5 group by name 6 ); 已删除999873行。 已用时间: 00: 00: 00.04 这样就可以了,而且步骤3的算法会比下面的方法要好得多 SQL> delete ori_bak where id not in 2 ( select min(id) from ori 3 group by name 4 ); 已删除9873行。 已用时间: 00: 00: 23.08 从时间对比可以看出来!
select a from table_a union select a from table_b
select * from t1 where not exists (select 1 from t2 where t2.MatlCode = t1.MatlCode) union select * from t2 where not exists (select 1 from t1 where t1.MatlCode = t2.MatlCode) union select * from t1 where exists (select 1 from t2 where t2.MatlCode = t1.MatlCode)
select MatlCode from t1
union
select MatlCode from t2;2.完全並集:
select MatlCode from t1
union all
select MatlCode from t2;
union all:全部
然后将原来的表的数据插入进新表:insert into ... select a, b, c from ... group by a, b, c;
就用新表代替旧表可以了。
2..http://www.oradb.net/sql/find0.htm
软件环境:
1、Windows NT4.0+ORACLE 8.0.4
2、ORACLE安装路径为:C:\ORANT问题提出:
1、当我们想要为一个表创建唯一索引时,如果该表有重复的记录,则无法创建成功。
方法原理:
1、Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,
rowid确定了每条记录是在ORACLE中的哪一个数据文件、块、行上。2、在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中
那些具有最大rowid的就可以了,其余全部删除。3、以下语句用到了3项技巧:rowid、子查询、别名。实现方法:
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 select * from a;插入4个记录.SQL> commit;完全提交.SQL> select rowid,bm,mc from a;ROWID BM MC
------------------ ---- -------
000000D5.0000.0002 1111 1111
000000D5.0001.0002 1112 1111
000000D5.0002.0002 1113 1111
000000D5.0003.0002 1114 1111
000000D5.0004.0002 1111 1111
000000D5.0005.0002 1112 1111
000000D5.0006.0002 1113 1111
000000D5.0007.0002 1114 1111查询到8记录.
查出重复记录
SQL> select rowid,bm,mc from a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);ROWID BM MC
------------------ ---- --------------------
000000D5.0000.0002 1111 1111
000000D5.0001.0002 1112 1111
000000D5.0002.0002 1113 1111
000000D5.0003.0002 1114 1111删除重复记录
SQL> delete from a a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);删除4个记录.SQL> select rowid,bm,mc from a;ROWID BM MC
------------------ ---- --------------------
000000D5.0004.0002 1111 1111
000000D5.0005.0002 1112 1111
000000D5.0006.0002 1113 1111
000000D5.0007.0002 1114 1111
3..:SQL> create table ori(id number,name char(122)) storage(initial 1m next 1m pctincrease 0 maxextents
unlimited); 2:SQL> begin
2 dbms_random.initialize(100);
3 for i in 1..1000000 loop
4 insert into ori values(i,
5 chr(mod(trunc(abs(dbms_random.random)),127)));
6 end loop;
7 dbms_random.terminate;
8 commit;
9 end;
10 / 3:SQL> delete ori where id in //------notice
2 ( select id from ori
3 minus
4 select min(id) from ori
5 group by name
6 ); 已删除999873行。 已用时间: 00: 00: 00.04 这样就可以了,而且步骤3的算法会比下面的方法要好得多
SQL> delete ori_bak where id not in
2 ( select min(id) from ori
3 group by name
4 );
已删除9873行。 已用时间: 00: 00: 23.08
从时间对比可以看出来!
union
select a from table_b
union
select * from t2 where not exists (select 1 from t1 where t1.MatlCode = t2.MatlCode)
union
select * from t1 where exists (select 1 from t2 where t2.MatlCode = t1.MatlCode)