Oracle DBA常用查詢

Ruthless發表於2013-06-24

–1. 查詢系統所有物件
select owner, object_name, object_type, created, last_ddl_time, timestamp, status
from dba_objects
where owner=upper('scott')

–2. 檢視系統所有表
select owner, table_name, tablespace_name from dba_tables

–3. 檢視所有使用者的表
select owner, table_name, tablespace_name from all_tables

–4. 檢視當前使用者表
select table_name, tablespace_name from user_tables

–5. 檢視使用者表索引
select t.*,i.index_type from user_ind_columns t, user_indexes i where
t.index_name = i.index_name and t.table_name = i.table_name
and t.table_name = 要查詢的表

–6. 檢視主鍵
select cu.* from user_cons_columns cu, user_constraints au
where cu.constraint_name = au.constraint_name
and au.constraint_type = upper('p') and au.table_name = 要查詢的表

–7. 檢視唯一性約束
select column_name from user_cons_columns cu, user_constraints au
where cu.constraint_name = au.constraint_name and au.constraint_type =  upper('u')
and au.table_name = 要查詢的表

–8. 檢視外來鍵
select * from user_constraints c where c.constraint_type = 'r' and c.table_name = 要查詢的表
select * from user_cons_columns cl where cl.constraint_name = 外來鍵名稱
select * from user_cons_columns cl where cl.constraint_name = 外來鍵引用表的鍵名

–9. 檢視錶的列屬性
select t.*,c.comments
from user_tab_columns t, user_col_comments c
where t.table_name = c.table_name and t.column_name = c.column_name and t.table_name = 要查詢的表

–10. 檢視所有表空間
select tablespace_name from dba_data_files group by tablespace_name

############################################
–1. 檢視oracle最大連線數
sql>show parameter processes    #最大連線數

–2. 修改最大連線數
sql>alter system set processes=value scope=spfile
–重啟資料庫
sql>shutdown force
sql>start force

–3. 檢視當前連線數
sql>select * from v$session where username is not null

–4. 檢視不同使用者的連線數
sql>select username,count(username) from v$session where username is not null group by username #檢視指定使用者的連線數

–5. 檢視活動的連線數
sql>select count(*) from v$session where status='active' #檢視併發連線數

–6. 檢視指定程式的連線數
sql>select count(*) from v$session where program='jdbc thin client' #檢視jdbc連線oracle的數目

–7. 檢視資料庫安裝例項(dba許可權)
sql>select * from v$instance

–8. 檢視執行例項名
sql>show parameter instance_name

–9. 檢視資料庫名
sql>show parameter db_name

–10. 檢視資料庫域名
sql>show parameter db_domain

–11. 檢視資料庫服務名
sql>show parameter service_names

–12. 檢視全域性資料庫名
sql>show parameter global

–13. 檢視錶空間使用率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- (1)
select dbf.tablespace_name,
       dbf.totalspace "總量(m)",
       dbf.totalblocks as "總塊數",
       dfs.freespace "剩餘總量(m)",
       dfs.freeblocks "剩餘塊數",
       (dfs.freespace / dbf.totalspace) * 100 as "空閒比例"
  from (select t.tablespace_name,
               sum(t.bytes) / 1024 / 1024 totalspace,
               sum(t.blocks) totalblocks
          from dba_data_files t
         group by t.tablespace_name) dbf,
       (select tt.tablespace_name,
               sum(tt.bytes) / 1024 / 1024 freespace,
               sum(tt.blocks) freeblocks
          from dba_free_space tt
         group by tt.tablespace_name) dfs
 where trim(dbf.tablespace_name) = trim(dfs.tablespace_name)
-- (2)
select t.name "tablespace name",
       free_space,
       (total_space - free_space) used_space,
       total_space
  from (select tablespace_name, sum(bytes / 1024 / 1024) free_space
          from sys.dba_free_space
         group by tablespace_name) free,
       (select b.name, sum(bytes / 1024 / 1024) total_space
          from sys.v_$datafile a, sys.v_$tablespace b
         where a.ts# = b.ts#
         group by b.name) t
 where free.tablespace_name = t.name

相關文章