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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM