1.限制返回记录的行数(4-10行)
select rownum,c1 from t144 where rownum<10
minus
select rownum,c1 from t144 where rownum<5
2.批量插入
insert into tablename(select * from othertable)
或
insert into (column1,column2,,,,,,,)tablename(select column1,column2,,,,,,,from othertable)
3,分组函数(结果中出现的列要在group by 中出现)
select count(age), username, , ,... from table group by age,usename,,,,...
4.查看数据库中的存在的表
select table_name from dba_tables (include all users)
select * from tab(include view)
5.高效update,一次修改多个列
update smsmonitor sms
set (sms.smsmonitor_sendflag, sms.smsmonitor_maxnum) = (select s.smsmonitor_sendflag,
s.smsmonitor_maxnum
from smsmonitor s
where s.smsmonitor_id =
'i74668')
where sms.smsmonitor_id = 'i74669'
6,高效删除表中重复记录
DELETE FROM EMP E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM EMP X
WHERE X.EMP_NO = E.EMP_NO);
7.如何定位重要(消耗资源多)的SQL
select sql_text
from v$sql
where disk_reads > 1000 or (executions > 0 and buffer_gets/executions > 30000);
8.如何跟踪某个session的SQL
exec dbms_system.set_sql_trace_in_session(sid,serial#,&sql_trace);
select sid,serial# from v$session where sid = (select sid from v$mystat where rownum = 1);
exec dbms_system.set_ev(&sid,&serial#,&event_10046,&level_12,'');
8,mysql中字段操作
增加字段
alter table name add code char(200) default 0
删除字段
ALTER TABLE table_NAME DROP COLUMN column_NAME
修改字段类型
ALTER TABLE table_name ALTER COLUMN column_name new_data_type
添加主键字段
alter table tb add 列名 int primary key default 0
查询被锁住的sql语句
SELECT /*+ rule */ s.username,
decode(l.type,'TM','TABLE LOCK',
'TX','ROW LOCK',
NULL) LOCK_LEVEL,
o.owner,o.object_name,o.object_type,
s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser
FROM v$session s,v$lock l,dba_objects o
WHERE l.sid = s.sid
AND l.id1 = o.object_id(+)
AND s.username is NOT Null
解锁表
--alter system kill session 'sid,serial#';--把锁给KILL掉
alter system kill session '146,21177';
解锁用户sql
ALTER USER username ACCOUNT UNLOCK;
根据索引名称查询该索引所在的表信息
SELECT table_name FROM all_indexes WHERE index_name='DCHNBACCFEEDET_PK'
查询该索引的信息
SELECT * FROM all_objects WHERE object_name LIKE 'DCHNBACCFEEDET_PK'