【Data Dictionary】Oracle10g 数据字典资料整理


原PPT资料可在这里下载
http://space.itpub.net/20335819/viewspace-694012
 
Oracle10g 数据字典(Data Dictionary)

■数据字典概述
在oracle 系统中,几乎所有的系统信息和对象信息都可由具有相关权限的用户在数据字典中查到。
■数据字典的概念
..数据字典是Oracle数据库系统的信息核心,它是一组提供有关数据库信息的表和视图的集合。这些表和视图是只读的。
..数据字典是随数据库的建立而建立的。
..数据字典中内容是随数据库执行特定动作时自动更新的。
..数据库依赖于数据字典来记录、校验和管理正在进行的操作。
■数据字典的拥有者
..在oracle 中,只有SYS用户才是DD的拥有者。
..DD保存在所属数据库的系统表空间system内。
..任何用户都无权更改sys模式下的模式对象或DD中的行。
..DD只能查询不能更改。
■数据字典的用途
..有三个主要用途:
 –Oracle 通过存取数据字典以便获得有关用户模式对象以及存储结构等信息。
 –当系统执行了DDL语句后,Oracle便及时修改数据字典。
 –任何用户能够以只读的形式使用数据字典来获取数据库信息。
■DD存储的信息
..数据库用户名称;
..为用户授予的权限和角色;
..模式对象的名称,如tables,views,indexes,procedures,functions,packages,triggers等。
..完整性约束的具体信息;
..每个字段的默认值;
..数据库空间的使用情况;
..审计功能,在Oracle_Home\productdb_1\rdbms\admin目录下的文件CATAUDIT.SQL就是用于为审计创建数据字典视图的脚本。
..对象与用户的严格管理(适用于高度机密管理);
..其他一般数据库信息。
■怎样使用数据字典?
..数据字典视图为所有用户充当着参考书作用。
..用户使用SQL语句存取数据字典。
..有些数据字典视图对所有Oracle用户是可存取的。
  而其他的数据字典视图仅供数据库管理员使用。
  在数据库打开期间,数据字典是可用的,它驻留在SYSTEM表空间上,并总是处于在线状态。
■三种前缀的数据字典视图
【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、查询用户
 SQL>select username from dba_users;     --仅由DBA使用
 SQL> select username from all_users;    --当前或任何用户均可使用
 
 修改口令
 SQL>alter userusername_countidentified bynew_password;
 查看当前用户的缺省表空间
 SQL>select username,default_tablespace from user_users;
 查看当前用户的角色
 SQL>select * from user_role_privs;
 查看当前用户的系统权限和表级权限
 SQL>select * from user_sys_privs;
 SQL>select * from user_tab_privs;
 
2、查询表空间:
 SQL>select * from dba_data_files;
 SQL>select * from dba_tablespaces;//表空间
 SQL>select tablespace_name,sum(bytes), sum(blocks) from dba_free_spacegroup by tablespace_name;//空闲表空间
 SQL>select * from dba_data_files where tablespace_name='RBS';//表空间对应的数据文件
 SQL>select * from dba_segments where tablespace_name='INDEXS';
 查询用户模式对象所使用过的或正在使用空间大小
 SQL>select name,type,source_size,code_size from user_object_size;
3、查询数据库对象
 select * from dba_objects;
 
 可根据拥有者查询下列对象类型:
 CLUSTER、DATABASE LINK、FUNCTION、INDEX、LIBRARY、PACKAGE、PACKAGE BODY、PROCEDURE、
 SEQUENCE、SYNONYM、TABLE、TRIGGER、TYPE、UNDEFINED、VIEW。
 如:
 select * from dba_objects where OBJECT_TYPE='TABLE' and WNER='SCOTT';
4、查询表
 SQL>select * from dba_tables;
 SQL>select extent_id,bytes from dba_extents
  where segment_name='CUSTOMERS' and segment_type='TABLE'
  order by extent_id;
 //表使用的extent的信息。segment_type='ROLLBACK‘
 查看回滚段的空间分配信息列信息:
 SQL>select distinct table_name from user_tab_columns where column_name='SO_TYPE_ID';
 查看用户下所有的表
 SQL>select * from user_tables;
 查看名称包含log字符的表
 SQL>select object_name,object_id from user_objects where instr(object_name,'LOG')>0;
 查看某表的创建时间
 SQL>select object_name,created from user_objects where object_name=upper('&table_name');
 
 查看某表的大小
 SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&table_name');
 查看放在ORACLE的内存区里的表
 SQL>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_columnsc
 where i.index_name=c.index_name and i.table_name ='ACC_NBR';//联接使用
 
 查看索引个数和类别
 SQL>select index_name,index_type,table_name from user_indexes order by table_name;
 查看索引被索引的字段
 SQL>select * from user_ind_columns where index_name=upper('&index_name');
 查看索引的大小
 SQL>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是当前值
 SQL>select * from user_sequences;
7、查询视图
 select * from dba_views;
 select * from all_views;
 可用命令descall_views;来查看表结构
 其中,Text列可用于查询视图生成的脚本
 查看视图的名称
 SQL>select view_name from user_views;
 查看创建视图的select语句
 SQL>set view_name,text_length from user_views;
 SQL>set long 2000;
 说明:可以根据视图的text_length值设定set long 的大小
 SQL>select text from user_views where view_name=upper('&view_name');
8、查询聚簇
 SQL>select * from dba_clusters;
9、查询快照:
 SQL>select * from dba_snapshots;
 快照、分区应存在相应的表空间。
10、查询同义词
 SQL>select * from dba_synonyms where table_owner='SCOTT';
 SQL>select * from ALL_synonyms where table_owner='SYSTEM';
 if owner is PUBLIC,then the synonyms is a public synonym.
 if owner is one of users,then the synonyms is a private synonym.
11、查询数据库链:
 SQL>select * from dba_db_links;
 
 在spbase下建数据库链
 SQL>create database link dbl_spnew connect to spnewidentified by spnewusing 'jhhx';
  insert into acc_nbr@dbl_spnew
 SQL>select * from acc_nbrwhere nxx_nbr='237' and line_nbr='8888';
12、查询触发器:
 SQL>select * from dba_trigers;
 存储过程,函数从dba_objects查找。
 其文本:
 SQL>select text from user_source where name='BOOK_SP_EXAMPLE';
 
 oracle总是将存储过程,函数等放在SYSTEM表空间。

 查看触发器
 SQL>set long 50000;
 SQL>set heading off;
 SQL>set pagesize2000;
 SQL>select'create or replace trigger "' || trigger_name || '"' || chr(10)||
  decode( substr( trigger_type, 1, 1 ),
          'A', 'AFTER',
          'B', 'BEFORE', 'I', 'INSTEAD OF' )
  || chr(10) || triggering_event || chr(10) || 'ON "' || table_owner || '"."'
  || table_name || '"' || chr(10) ||
  decode( instr( trigger_type, 'EACH ROW' ),
          0, null,
    'FOR EACH ROW' )
  || chr(10) , trigger_body
 from user_triggers;
13、查看函数和过程的状态
 SQL>select object_name,status from user_objects where object_type='FUNCTION';
 SQL>select object_name,status from user_objects where object_type='PROCEDURE';
 查看函数和过程的源代码
 SQL>select text from all_source where wner=user and name=upper('&plsql_name');
14、查询约束:
(1)约束是和表关联的,可在create table或alter table table_name add/drop/modify来建立、修改、删除约束。
 可以临时禁止约束,如:
 SQL>alter table book_example disable constraint book_example_1;
 SQL>alter table book_example enable constraint book_example_1;
(2)主键和外键被称为表约束,而not null和unique之类的约束被称为列约束。通常将主键和外键作为单独的命名约
 束放在字段列表下面,而列约束可放在列定义的同一行,这样更具有可读性。
(3)列约束可从表定义看出,即describe;表约束即主键和外键,可从dba_constraints和dba_cons_columns查。
 select * from user_constraints where table_name='BOOK_EXAMPLE';
 SQL>select owner,CONSTRAINT_NAME,TABLE_NAME from user_constraints where constraint_type='R' order by table_name;
(4)定义约束可以无名(系统自动生成约束名)和自己定义约束名(特别是主键、外键)
 如:create table book_example (identifier number not null);
  create table book_example (identifier number constranitbook_example_1 not null);
 查看某表的约束条件
 SQL>select constraint_name, constraint_type,search_condition, r_constraint_name from user_constraints where table_name = upper('&table_name');
 SQL>select constraint_name,c.constraint_type,cc.column_name from user_constraints c,user_cons_columns cc where c.owner = upper('&table_owner')
  and c.table_name = upper('&table_name') and c.owner = cc.owner and c.constraint_name = cc.constraint_name order by cc.position;
 数据完整性约束
 SQL>select constraint_name, constraint_type,table_name from dba_constraints;
15、查询回滚段:
 在所有的修改结果存入磁盘前,回滚段中保持恢复该事务所需的全部信息,必须以数据库发生的事务来相应确定其大小
 (注意:DML语句才可回滚,create,drop,truncate等DDL不能回滚)。
 回滚段数量=并发事务/4,但不能超过50;使每个回滚段大小足够处理一个完整的事务;
 SQL>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.submit(:v_num,‘a;’,sysdate,‘sysdate+ (10/(24*60*60))’)加入作业。间隔10秒钟
 使用包exec dbms_job.submit(:v_num,‘a;’,sysdate,‘sysdate+ (11/(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,high_value FROM dba_tab_columns;
 数据库用户的登录名称:
 Select schemaname,osuser,machine,usernamefrom v$session; -- Sys用户
 查询每个用户所授予的权限和角色:
 select table_name,privilege,grantor from dba_tab_privs;
 审核用户信息:
 select username,obj_name,action_namefrom dba_audit_object;
■数据字典中两个特殊的表
 如果知道自己要查的对象类型,但却不知道数据字典的具体名称时,可以查询下面两个特殊的数据字典表,来查找相应的数据字典。
 dictionary    全部数据字典表的名称和解释,它有一个同义词dict
 dict_columns  全部数据字典表里字段名称和解释
 如果想查询跟索引有关的数据字典时,可以用下面这条SQL语句:
 select * from dictionary where instr(comments,'index')>0;
 如果想查询跟表有关的数据字典时,可以用下面这条SQL语句:
 select * from dictionary where instr(comments,‘table')>0;
 查询其他类型的数据字典时,将对象名称修改即可。
 方法:将‘table’替换成相应的对象类型即可。依次类推
■数据字典(视图)中各字段含义查询
 如果知道相关数据字典表(视图)的名称,却不知道该表(视图)每个字段的含义,可以查询dict_columns表得到。
 使用下列格式:
 select column_name,comments from dict_columns where table_name=upper(‘datadictionary_tablename');
 如果我们想知道user_indexes表各字段名称的详细含义,
 可以用下面这条SQL语句:
 select column_name,comments from dict_columnswhere table_name='USER_INDEXES';
■数据库升级后,需要重新建立数据字典
 数据库升级后,必须通过手工的方式重新建立数据字典。以便及时更新数据字典中数据库的基础元数据。
 建立数据字典的方法是:
 .启动SQL*PLUS或SQL*PLUS WORKSHEET,并以用户SYS的DBA身份登录数据库,
 .执行路径\oracle\ora92\rdbms\admin下的catalog.sql和catadt.sql
■回收站查询
 回收站recyclebin是Oracle10g新增的一项功能,可用于恢复被删除的且在回收站中的对象。它与操作系统中的回收站类似。
 ..与回收站相关的数据字典 
  recyclebin,user_recyclebin,dba_recyclebin
 ..查看回收站对象 
  descrecyclebin;
  select * from recyclebin;
  show recyclebin;
 ..根据表名删除回收站对象
  格式:purge table
  例句:purge table bin$v3zj9aowrimot89akyyvsg==$0;
 ..依照表空间删除回收站对象
  格式:purge tablespace< tablespace_name>
  例句:purge tablespacedata_sml;
 ..依照表空间和用户删除回收站对象
  格式:purge tablespace user ;
  例句:purge tablespacesale_xml user recbin;
 ..清空回收站
  格式:purge recyclebin;
 ..清空所有回收站中的所有对象
  格式:purge dba_recyclebin;
 ..从删除的表中查询数据
  --首先查询出被删除对象的名字和在回收站中对应的名字
   select object_name, original_name from User_recyclebin;
  ---查询的表名为被删除表在回收站中的名
   select * from "BIN$w90vuPxqTAu/IyFfa+L/VQ==$0";
请使用浏览器的分享功能分享到微信等