日期:2014-05-16  浏览次数:20439 次

oracle 快速查询数据库各种信息、及转换对应java代码

1 查询表中数据量

select 'select ' || '''' || t.TABLE_NAME || '''' ||
       ' as table_name, count(*) from ' || t.TABLE_NAME || ' union'
  from user_tables t;




2 查询数据库中锁表和解锁

详细见: http://blog.csdn.net/pioayang/article/details/10108671

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

--kill session语句
alter system kill session'50,492';
--以下几个为相关表
SELECT * FROM v$lock;
SELECT * FROM v$sqlarea;
SELECT * FROM v$session;
SELECT * FROM v$process ;
SELECT * FROM v$locked_object;
SELECT * FROM all_objects;
SELECT * FROM v$session_wait;

--1.查出锁定object的session的信息以及被锁定的object名

SELECT l.session_id sid, s.serial#, l.locked_mode,l.oracle_username,
l.os_user_name,s.machine, s.terminal, o.object_name, s.logon_time
FROM v$locked_object l, all_objects o, v$session s
WHERE l.object_id = o.object_id
AND l.session_id = s.sid
ORDER BY sid, s.serial# ;
--2.查出锁定表的session的sid, serial#,os_user_name, machine name, terminal和执行的语句
--比上面那段多出sql_text和action
SELECT l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, s.user#,
l.os_user_name,s.machine, s.terminal,a.sql_text, a.action
FROM v$sqlarea a,v$session s, v$locked_object l
WHERE l.session_id = s.sid
AND s.prev_sql_addr = a.address
ORDER BY sid, s.serial#;
--3.查出锁定表的sid, serial#,os_user_name, machine_name, terminal,锁的type,mode
SELECT s.sid, s.serial#, s.username, s.schemaname, s.osuser, s.process, s.machine,
s.terminal, s.logon_time, l.type
FROM v$session s, v$lock l
WHERE s.sid = l.sid
AND s.username IS NOT NULL
ORDER BY sid;

3 查询表中注释及列的注释

详细见 http://blog.csdn.net/pioayang/article/details/11248235

select t3.table_name,t3.comments,t2.comments,t1.COLUMN_NAME
       ,t1.DATA_TYPE||'('||decode(t1.DATA_TYPE, '', t1.DATA_PRECISION+t1.DATA_SCALE, t1.data_length)||')'
from user_tab_cols t1, user_col_comments t2, user_tab_comments t3
where t1.TABLE_NAME=t2.table_name(+)
and t1.COLUMN_NAME=t2.column_name(+)
and t1.TABLE_NAME=t3.table_name(+)
and lower(t1.TABLE_NAME)='user';