1. 程式人生 > >Oracle資料字典使用

Oracle資料字典使用

一、資料字典
  資料字典是oracle存放有關資料庫資訊的地方,幾乎所有的系統資訊和物件資訊都可在資料字典中進行查詢。資料字典是oracle資料庫系統的資訊核心,它是一組提供有關資料庫資訊的表和檢視的集合,這些表和檢視是隻讀的。它是隨著資料庫的建立而建立的,當資料庫執行特定動作時資料字典也會自動更新。資料一覽與資料字典來記錄、校驗和管理正在進行的操作。
oracle中、sys使用者是資料字典的擁有者,資料字典保證在所有資料庫的系統表空間system內,任何使用者都無權更改sys模式下的模式物件或資料字典中的行。也就是說資料字典只能查詢,不能手動進行修改。

  資料字典用途
  oracle通過存取資料字典從而比較方便地獲取有關使用者某事物件和儲存結構等資訊。當系統執行了DDL語句後,oracle會及時修改資料字典。任何使用者只能以讀的形式使用資料字典獲取資料庫資訊。

資料字典儲存的資訊

  • 資料使用者的名稱
  • 為使用者授予的許可權和角色
  • 模式物件的名稱,如 tables,views,indexex,procedures,functions,packages,triggers等。
  • 完整性約束的具體資訊;
  • 每個欄位的預設值;
  • 資料庫空間的使用情況;
  • 審計功能,在Oracle_Home\productdb_l\rdbms\admin目錄下的檔案cataudit.sql就是用於為審計建立資料字典檢視的腳步。
  • 物件與使用者的嚴格管理(適用於高度機密管理);
  • 其他一般資料庫資訊。

  三種字首的資料字典檢視 
  user_ :任何使用者都可以讀取的檢視,每個使用者讀取的都不一樣,它只提供當前使用者某事下的物件資訊。如查詢當前模式下的所有物件select object_name, object_type from user_objects;

  all_ :所有使用者都可讀取的使用者檢視,它提供與使用者有關的物件資訊。如查詢當前使用者可訪問的所有物件
select owner, object_name, object_type from all_objects;
  dba_:提供了只有資料庫管理員才可讀取的檢視,包括所有使用者檢視中的物件資訊。如select owner, object_name, object_type from sys.dba_objects;

二、資料字典相關查詢

  1、 查詢使用者

select username from dba_users; -- 只有管理員許可權的使用者才能查詢

select username from all_users; -- 當前或任何使用者都可使用

-- 檢視當前使用者的預設表空間
select username, default_tablespace from user_users;

--當前使用者角色
select * from user_role_privs;

-- 當前使用者的系統許可權和表級許可權
select * from user_sys_privs;
select * from user_tab_privs;

   2、查詢 表空間 (擁有DBA許可權的使用者才能查詢)

select * from dba_data_files;
select * from dba_tablespaces; --表空間

select tablespace_name, sum(bytes), sum(blocks) from dba_free_space group by tablespace_name; --空閒表空間

select * from dba_data_files where tablespace_name='USERS'; -- 表空間對於的資料檔案

select * from dba_segments where tablespace_name='USERS';
--查詢使用者模式物件所使用過的正在使用空間大小
select name, type, source_size, code_size from user_object_size;

  3、查詢資料庫物件(擁有DBA許可權的使用者才能查詢)

select * from dba_objects
select * from dba_objects where object_type = upper('package body');

select * from dba_objects where OBJECT_TYPE='TABLE' and OWNER='SCOTT'

  可根據擁有者查詢下列物件型別(object_type)

  cluster    databaselink
  function   index
  library   package
  package body   procedure
  sequence   synonym
  table   trigger
  type   undefined
  view
  4、查詢表

--表使用的extent的資訊。segment_type='ROLLBACK'

select * from dba_tables;
select extent_id, bytes from dba_extents where segment_name='CUSTOMERS' and segment_type='TABLE' order by extent_id;

--    查看回滾段的空間分配資訊列資訊 SELECT * FROM user_tab_columns;
select distinct table_name from user_tab_columns where column_name='ID';

-- 檢視當前使用者下所有的表
select * from user_tables;
--檢視名稱包含log字元的表
select object_name, object_id from user_objects where instr(object_name, 'LOG') > 0;
-- 檢視某表的建立時間
select object_name, created from user_objects where object_name = upper('&table_name');
-- 檢視某表的大小
select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name = upper('&table_name');
-- 檢視放在oracle的記憶體的表
select table_name, cache from user_tables where instr(cache, 'Y') > 0;

  5、查詢索引

select * from dba_indexes; -- 索引,包括主鍵索引

select * from all_indexes;
select * from dba_ind_columns; -- 索引列

select i.index_name, i.uniqueness, c.column_name from user_indexes i, user_ind_columns c where i.index_name=c.index_name and i.table_name = 'PERSON'; --連線使用

-- 檢視索引個數和類別
select index_name, index_type, table_name from user_indexes order by table_name;
-- 檢視索引被索引的欄位
select * from user_ind_columns where index_name=upper('&index_name');
-- 檢視索引的大小
select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name = upper('&index_name');

  6、查詢序列

select * from dba_sequences;
select * from all_sequences;

檢視序列號,last_number是當前值
select * from user_sequences;

  7、查詢檢視

select * from dba_views;
select * from all_views;

可用目錄desc all_views;來檢視檢視結構
其中,Text列可用於查詢檢視生產的指令碼。

--查詢當前使用者檢視的名稱
select view_name from user_views;

--查詢建立檢視的select語句
select view_name, text_length from user_views;
set long 2000;

--可以根據檢視的text_length 值設定set long的大小
select text from user_views where view_name = upper('&view_name');

  8、查詢聚簇

select * from dba_clusters;

  9、查詢快照

select * from dba_snapshots;

  快照、分割槽應存在相對應的表空間

  10、查詢同義詞

select * from dba_synonyms where table_owner='SCOTT';
select * from ALL_synonyms where table_owner='SYSTEM';

  如果使用者表可以被訪問,那麼同義詞也可以被訪問,使用者表不能被訪問,則同義詞也不能被訪問。

  11、查詢資料庫鏈

select * from dba_db_links;

   12、查詢觸發器(12)

select * from dba_triggers;

儲存過程,函式從dba_objects查詢

查詢文字
select text from user_source where name = 'PRO_PERSON_FINDBYID';

oracle總是將儲存過程,函式放在system表空間。

  13、檢視函式和過程的狀態

select object_name, status from user_objects where object_type='FUNCTION';
select object_name, status from user_objects where object_type='PROCEDURE';

--檢視原始碼
select * from all_source where owner='WUXX' and name=upper('&plsql_name');

  14、查詢約束

約束是和表關聯的,可以在create table或alter table table_name add/drop/modify 來建立、修改、刪除約束。
可以臨時禁止約束,如:
alter table book_example
disable constraint book_example_l;
資料完整性約束

select constraint_name, constraint_type, table_name from dba_constraints;

  15、查詢回滾段
在所有的修改結果存入磁碟前,回滾段中保持恢復該事務所需的全部資訊,必須以資料庫傳送的事務來相應確定其大小。(DML語句才可回滾,create, drop, truncate等DDL不能回滾)
回滾段數量=併發事務/4,但不能超過50個;是每個回滾段大小足夠處理一個完整的事物;

create rollback segment r05 tablespace rbs;
create rollback segment rbs_cvt tablespace rbs storage(initial 1M next 500k);

  16、查詢作業

select job, broken, next_date, interval, what from user_jobs;
select job, broken, next_date, interval, what from dba_jobs;

--正在執行的作業
select * from dba_jobs_running;

使用包exec dbms_job.sumit(:v_num, 'a;', sysdate, 'sysdate +(10/(24*60*60))');加入作業。間隔10秒

使用包exec dbms_job.sumit(:v_num, 'a;', sysdate, 'sysdate +(10/(24*60))');加入作業。間隔11分
使用表exec dbms_job.remove(21)刪除21號作業。

其他資訊查詢
查詢優化模式物件使用過的或正在使用的空間大小

select name, type, source_size, code_size from user_object_size;

查詢欄位的預設值

select table_name, column_name, data_default, low_value, hight_value from dba_tab_columns;