1.
Oracle中运用ROWNUM实现SELECT TOP N
用法,见附件文档!
http://bfc99.blog.51cto.com/265386/803718
http://www.itpub.net/thread-415-1-1.html
2.删除重复记录,保留rowid最小的记录。即如果有10条记录重复,删除9条,保留一条。
not in实现为:
delete from tableName t where t.rowid not in
(select min(a.rowid) from tableName a group by a.field1,a.field2);
not exists实现为:
delete from tableName t where not exists
(select 1 from (select min(a.rowid) rid from tableName a group by a.field1,a.field2) b where b.rid=t.rowid);
或采用创建临时表的方式:
step1:创建临时表
create table tempTableName as
select min(a.rowid) rid from tableName a group by a.field1,a.field2;
step2:删除重复数据
delete from tableName t where t.rowid not in (select b.rid from tempTableName b);
或delete from tableName a where not exists (select 1 from tempTableName b where b.rid=a.rowid);
附:not in优化:http://blog.csdn.net/anerou/article/details/5185573
3.INSERT INTO SELECT语句 与 SELECT INTO语句
INSERT INTO SELECT语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1;
INSERT INTO SELECT也可以直接插入序列值、常量值,例如:
insert into Table2(ID, username, age, field) select table2_seq.nextval, 'gerry', 25, otherField from Table1;
SELECT INTO语句形式为:select * into Table_backup FROM Table;
SELECT INTO语句从一个表中选取数据,然后把数据插入另一个表中,SELECT INTO语句常用于创建表的备份复件或者用于对记录进行存档。需要注意的是:在Oracle中select into from不可以使用-----原因很简单:select into是PL/SQL language 的赋值语句!如果使用则Oracle会抛出0RA-00905:missing keyword的异常!但是可以用create table as select代替该功能:create table new_table as select * from old_table;
http://jingyan.baidu.com/article/d5c4b52b29c326da570dc548.html
http://stackoverflow.com/questions/2250196/select-into-using-oracle
4、序列
创建序列:
create sequence TABLE_SEQ
minvalue 1
maxvalue 999999999999
start with 1
increment by 1
cache 20;
插入记录时主键使用序列值:
insert into tableName(ID, ...) values(TABLE_SEQ.nextval, ...)
5、oracle中count(*) count(1) count(字段)
oracle中:
count(*):返回表中所有行数(包括值为null的行);
count(*)、count(1)、count(2)甚至count(23890), 执行计划一样,查询结果也一样。
count(字段):返回表中除去该列值为null以外的所有行数。
由于count(字段),查询的是该字段值不为空的记录数。所以select count(field) from table;与select count(*) from table t where t.field is not null;查询的结果是一样的。
select distinct filed from table; //剔除该列重复数据
select count(distinct filed) from table; //除去重复数据与值为null的结果
6、使用for update卡住锁表的解决办法
http://www.cnblogs.com/chenwenbiao/archive/2012/06/06/2537496.html
7、alter system flush shared_pool
alter system flush shared_pool的作用:
The FLUSH SHARED POOL clause lets you clear all data from the shared pool in the system global area (SGA). The shared pool stores Cached data dictionary information and Shared SQL and PL/SQL areas for SQL statements, stored procedures, function, packages, and triggers.
This statement does not clear shared SQL and PL/SQL areas for items that are currently being executed. You can use this clause regardless of whether your instance has the database dismounted or mounted, open or closed.
http://wenku.baidu.com/link?url=bIEx6mMvgjW24kg04rg7DvP1SPGdbc0Wt6_TOg-PREkzHIOmKULYj6a9hljPzQtqSN9pSmRnL1TX_Kt6Gg6hlQlYoKO6GNmg30nYsFQ6RcC
8、sql中case when then条件语句
select CASE sex
WHEN 1 THEN '男'
WHEN 2 THEN '女'
ELSE '保密' END
from usertable;
--或
select CASE
WHEN sex = 1 THEN '男'
WHEN sex = 2 THEN '女'
ELSE '保密' END
from usertable;
其他复杂用法去google吧