查詢表資訊

brj880719發表於2017-10-26

收集表資訊指令碼

大神寫的指令碼分享給大家,用來收集表資訊的

  1. -- | PURPOSE : Prompt the user for a schema and and table name then query all |
  2. -- | metadata about the table. |
  3. -- | NOTE : As with any code, ensure to test this script in a development |
  4. -- | environment before attempting to run it in production. |
  5. -- +----------------------------------------------------------------------------+


  6. SET PAGESIZE 9999
  7. SET VERIFY OFF
  8. SET FEEDBACK OFF
  9. SET LONG 9000

  10. -- +----------------------------------------------------------------------------+
  11. -- | PROMPT USER FOR SCHEMA AND TABLE |
  12. -- +----------------------------------------------------------------------------+

  13. ACCEPT sch prompt 'Enter Schema (i.e. SCOTT) : '
  14. ACCEPT tab prompt 'Enter Table (i.e. EMP) : '


  15. PROMPT
  16. PROMPT +----------------------------------------------------------------------------+
  17. PROMPT | TABLE INFORMATION |
  18. PROMPT +----------------------------------------------------------------------------+

  19. COLUMN owner FORMAT A15 HEADING "Owner"
  20. COLUMN table_name FORMAT A30 HEADING "Table Name"
  21. COLUMN tablespace_name FORMAT A28 HEADING "Tablespace"
  22. COLUMN last_analyzed FORMAT A20 HEADING "Last Analyzed"
  23. COLUMN num_rows FORMAT 999,999,999 HEADING "# of Rows"

  24. SELECT
  25.     owner
  26.   , table_name
  27.   , tablespace_name
  28.   , TO_CHAR(last_analyzed, 'DD-MON-YYYY HH24:MI:SS') last_analyzed
  29.   , num_rows
  30. FROM
  31.     dba_tables
  32. WHERE
  33.       owner = UPPER('&sch')
  34.   AND table_name = UPPER('&tab')
  35. /

  36. PROMPT
  37. PROMPT +----------------------------------------------------------------------------+
  38. PROMPT | OBJECT INFORMATION |
  39. PROMPT +----------------------------------------------------------------------------+

  40. COLUMN object_id HEADING "Object ID"
  41. COLUMN data_object_id HEADING "Data Object ID"
  42. COLUMN created FORMAT A20 HEADING "Created"
  43. COLUMN last_ddl_time FORMAT A20 HEADING "Last DDL"
  44. COLUMN status HEADING "Status"

  45. SELECT
  46.     object_id
  47.   , data_object_id
  48.   , TO_CHAR(created, 'DD-MON-YYYY HH24:MI:SS') created
  49.   , TO_CHAR(last_ddl_time, 'DD-MON-YYYY HH24:MI:SS') last_ddl_time
  50.   , status
  51. FROM
  52.     dba_objects
  53. WHERE
  54.       owner = UPPER('&sch')
  55.   AND object_name = UPPER('&tab')
  56.   AND object_type = 'TABLE'
  57. /

  58. PROMPT
  59. PROMPT +----------------------------------------------------------------------------+
  60. PROMPT | SEGMENT INFORMATION |
  61. PROMPT +----------------------------------------------------------------------------+

  62. COLUMN segment_type HEADING "Segment Type"
  63. COLUMN bytes FORMAT 9,999,999,999,999 HEADING "Bytes"
  64. COLUMN extents FORMAT 999,999,999 HEADING "Extents"
  65. COLUMN initial_extent FORMAT 999,999,999,999 HEADING "Initial|Extent"
  66. COLUMN next_extent FORMAT 999,999,999,999 HEADING "Next|Extent"
  67. COLUMN min_extents FORMAT 999 HEADING "Min|Extents"
  68. COLUMN max_extents FORMAT 9,999,999,999 HEADING "Max|Extents"
  69. COLUMN pct_increase FORMAT 999.00 HEADING "Pct|Increase"
  70. COLUMN freelists HEADING "Free|Lists"
  71. COLUMN freelist_groups HEADING "Free|List Groups"

  72. SELECT
  73.     segment_type segment_type
  74.   , bytes/1024/1024 Mbytes
  75.   , extents extents
  76.   , initial_extent initial_extent
  77.   , next_extent next_extent
  78.   , min_extents min_extents
  79.   , max_extents max_extents
  80.   , pct_increase pct_increase
  81.   , freelists freelists
  82.   , freelist_groups freelist_groups
  83. FROM
  84.     dba_segments
  85. WHERE
  86.       owner = UPPER('&sch')
  87.   AND segment_name = UPPER('&tab')
  88. /


  89. PROMPT
  90. PROMPT +----------------------------------------------------------------------------+
  91. PROMPT | COLUMNS |
  92. PROMPT +----------------------------------------------------------------------------+

  93. COLUMN column_name FORMAT A20 HEADING "Column Name"
  94. COLUMN data_type FORMAT A25 HEADING "Data Type"
  95. COLUMN nullable FORMAT A13 HEADing "Null?"

  96. SELECT
  97.     column_name
  98.   , DECODE(nullable, 'Y', ' ', 'NOT NULL') nullable
  99.   , DECODE(data_type
  100.                , 'RAW', data_type || '(' || data_length || ')'
  101.                , 'CHAR', data_type || '(' || data_length || ')'
  102.                , 'VARCHAR', data_type || '(' || data_length || ')'
  103.                , 'VARCHAR2', data_type || '(' || data_length || ')'
  104.                , 'NUMBER', NVL2( data_precision
  105.                                  , DECODE( data_scale
  106.                                             , 0
  107.                                             , data_type || '(' || data_precision || ')'
  108.                                             , data_type || '(' || data_precision || ',' || data_scale || ')'
  109.                                    )
  110.                                  , data_type)
  111.                , data_type
  112.     ) data_type
  113. FROM
  114.     dba_tab_columns
  115. WHERE
  116.       owner = UPPER('&sch')
  117.   AND table_name = UPPER('&tab')
  118. ORDER BY
  119.     column_id
  120. /


  121. PROMPT
  122. PROMPT +----------------------------------------------------------------------------+
  123. PROMPT | CONSTRAINTS |
  124. PROMPT | UNCOMMENT THIS SECTION FROM THE SCRIPT TO GET THE INFO ABOUT CONST |
  125. PROMPT +----------------------------------------------------------------------------+
  126. PROMPT

  127. COLUMN constraint_name FORMAT A18 HEADING "Constraint Name"
  128. COLUMN constraint_type FORMAT A11 HEADING "Constraint|Type"
  129. COLUMN search_condition FORMAT A15 HEADING "Search Condition"
  130. COLUMN r_constraint_name FORMAT A20 HEADING "R / Constraint Name"
  131. COLUMN delete_rule FORMAT A11 HEADING "Delete Rule"
  132. COLUMN status HEADING "Status"

  133. BREAK ON constraint_name ON constraint_type

  134. SELECT
  135.     a.constraint_name
  136.   , DECODE(a.constraint_type
  137.              , 'P', 'Primary Key'
  138.              , 'C', 'Check'
  139.              , 'R', 'Referential'
  140.              , 'V', 'View Check'
  141.              , 'U', 'Unique'
  142.              , a.constraint_type
  143.     ) constraint_type
  144.   , b.column_name
  145.   , a.search_condition
  146.   , NVL2(a.r_owner, a.r_owner || '.' || a.r_constraint_name, null) r_constraint_name
  147.   , a.delete_rule
  148.   , a.status
  149. FROM
  150.     dba_constraints a
  151.   , dba_cons_columns b
  152. WHERE
  153.       a.owner = UPPER('&sch')
  154.   AND a.table_name = UPPER('&tab')
  155.   AND a.constraint_name = b.constraint_name
  156.   AND b.owner = UPPER('&sch')
  157.   AND b.table_name = UPPER('&tab')
  158. ORDER BY
  159.     a.constraint_name
  160.   , b.position
  161. /


  162. SET PAGESIZE 9999
  163. SET VERIFY OFF
  164. SET FEEDBACK ON

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24851054/viewspace-2146407/,如需轉載,請註明出處,否則將追究法律責任。

相關文章