Oracle查询优化-05元数据查询

前端之家收集整理的这篇文章主要介绍了Oracle查询优化-05元数据查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

5.1列出已创建的表的清单

  1. select * from all_tables ;
  2. select * from dba_tables ;
  3. select * from user_tables ;

5.2 列出表的列

  1. select * from all_tab_columns a ;
  2. select * from dba_tab_columns a ;
  3. select * from user_tab_columns a ;

5.3列出表的索引列

  1. select a.*from all_ind_columns a ;
  2. select a.* from dba_ind_columns a ;
  3. select a.* from user_ind_columns a

5.4 列出表约束

查询 sys.all_constraints 和 sys.all_cons_columns

  1. select a.TABLE_NAME,a.CONSTRAINT_NAME,b.COLUMN_NAME,a.CONSTRAINT_TYPE
  2. from all_constraints a,all_cons_columns b
  3. where a.TABLE_NAME = 'EMP'
  4. and a.OWNER = b.OWNER
  5. and a.TABLE_NAME = b.TABLE_NAME
  6. and a.CONSTRAINT_NAME = b.CONSTRAINT_NAME;
  7.  
  8.  
  9. TABLE_NAME CONSTRAINT_NAME COLUMN_NAME CONSTRAINT_TYPE
  10. ------------------------------ ------------------------------ -------------------------------------------------------------------------------- ---------------
  11. EMP FK_DEPTNO DEPTNO R
  12. EMP PK_EMP EMPNO P

5.5 列出没有相应索引的外键

列出还有没有被索引的外键的表,例如 判断EMP表中的外键是否被索引。

  1. select
  2. a.TABLE_NAME,a.COLUMN_NAME,c.INDEX_NAME
  3. from all_cons_columns a,all_constraints b,all_ind_columns c
  4. where a.TABLE_NAME = 'EMP'
  5. and a.OWNER = 'CRM'
  6. and b.CONSTRAINT_TYPE = 'R'
  7. and a.OWNER = b.OWNER
  8. and a.TABLE_NAME = b.TABLE_NAME
  9. and a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
  10. and a.OWNER = c.TABLE_OWNER(+)
  11. and a.TABLE_NAME = c.TABLE_NAME(+)
  12. and a.COLUMN_NAME = c.COLUMN_NAME(+)
  13. and c.INDEX_NAME is null;
  14.  
  15.  
  16. TABLE_NAME CONSTRAINT_NAME COLUMN_NAME INDEX_NAME
  17. ------------------------------ ------------------------------ -------------------------------------------------------------------------------- ------------------------------
  18. EMP FK_DEPTNO DEPTNO

5.6 使用sql生成sql

举例:生成sql统计所有表中的行数

  1. select 'select count(1) from ' || table_name || ';' from user_tables ;

5.7 在oracle中描述数据字典视图

列出数据字典视图和他们的用途

  1. select * from dictionary a order by a.TABLE_NAME ;

查询数据字典中的列

  1. select * from dict_columns a where a.TABLE_NAME = 'V$sql';

猜你在找的Oracle相关文章