从今天开始接触Oracle9i&10g了,记录一下所学,不断更新
用到了一个IDE工具 PL/SQL DEVELOPER 6.0
1、选取数据的前 10 行
SELECT * FROM BM_FH_GZ where rownum<11
2、不同用户拷贝表/表结构
create table BM_FH_GZLB as (select * from tepcor_qhd.BM_RS_GZLB )
如只拷贝表结构不要数据 加 where 1=2
create table BM_FH_GZLB as (select * from tepcor_qhd.BM_RS_GZLB where 1=2 )
3、删除一条重复的记录
在oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会相同。SQL语句如下:
delete from tbl where rowid in (select a.rowid from tbl a,
tbl b where a.rowid>b.rowid and a.col1=b.col1 and a.col2 = b.col2)
如果已经知道每条记录只有一条重复的,这个sql语句适用。但是如果每条记录的重复记录有N条,这个N是未知的,就要考虑适用下面这种方法了。
利用max或min函数
这里也要使用rowid,与上面不同的是结合max或min函数来实现。SQL语句如下
delete from tbl awhere rowid not in (select max(b.rowid)
from tbl b where a.col1=b.col1 and a.col2 = b.col2);
//这里max使用min也可以
或者用下面的语句
delete from tbl awhere rowid<(select max(b.rowid)
from tbl b where a.col1=b.col1 and a.col2 = b.col2);
//这里如果把max换成min的话,前面的where子句中需要把"<"改为">"
跟上面的方法思路基本是一样的,不过使用了group by,减少了显性的比较条件,提高效率。SQL语句如下:
deletefrom tbl where rowid not in (select max(rowid)
from tbl tgroup by t.col1, t.col2);delete from tbl where (col1, col2)
in (select col1,col2 from tblgroup bycol1,col2havingcount(*)
>1)and rowidnotin(selectnin(rowid)fromtblgroup bycol1,
col2havingcount(*) >1)