每个Oracle数据库中包括一个被称为“元数据”的集合,或者说包含用来描述数据库有关数据结构的数据。包含这些元数据的表和视图称为Oracle数据字典。本文列出了一些常用的Oracle数据字典的查询方法。
下面按类别列出一些Oracle用户常用数据字典的查询使用方法。
一、用户
查看当前用户的缺省表空间
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;
二、表
查看用户下所有的表
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;
三、索引
查看索引个数和类别
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');
四、序列号
查看序列号,last_number是当前值
SQL>SELECT * FROM user_sequences;
五、视图
查看视图的名称
SQL>SELECT view_name FROM user_views;
查看创建视图的select语句
SQL>SELECT 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');
六、同义词
查看同义词的名称
SQL>SELECT * FROM user_synonyms;
七、约束条件
查看某表的约束条件
SQL>SELECT constraint_name, constraint_type,search_condition, r_constraint_name
FROM user_constraints WHERE table_name = UPPER('&table_name');
//注意,表名一定是大写才行
SQL>SELECT c.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 6 ORDER BY cc.position;
八、存储函数和过程
查看函数和过程的状态
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 owner=user AND name=UPPER('&plsql_name');
九、触发器
查看触发器
SET long 50000;
SET heading off;
SET pagesize 2000;
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;
下面按类别列出一些Oracle用户常用数据字典的查询使用方法。
一、用户
查看当前用户的缺省表空间
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;
二、表
查看用户下所有的表
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;
三、索引
查看索引个数和类别
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');
四、序列号
查看序列号,last_number是当前值
SQL>SELECT * FROM user_sequences;
五、视图
查看视图的名称
SQL>SELECT view_name FROM user_views;
查看创建视图的select语句
SQL>SELECT 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');
六、同义词
查看同义词的名称
SQL>SELECT * FROM user_synonyms;
七、约束条件
查看某表的约束条件
SQL>SELECT constraint_name, constraint_type,search_condition, r_constraint_name
FROM user_constraints WHERE table_name = UPPER('&table_name');
//注意,表名一定是大写才行
SQL>SELECT c.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 6 ORDER BY cc.position;
八、存储函数和过程
查看函数和过程的状态
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 owner=user AND name=UPPER('&plsql_name');
九、触发器
查看触发器
SET long 50000;
SET heading off;
SET pagesize 2000;
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;