oracle metadata to obtain the key information of the table under the user

To understand the data situation of a business system, it is necessary to understand the table structure information and ER relationship diagram of the business system. This article mainly talks about obtaining the key information of all tables under the user through oracle metadata, such as: table name, table description, field name, field description, field type, field length, whether the primary key is not empty, default value, etc.

Get all table names and table descriptions under this user

The user_tab_comments table (view), which stores all tables and descriptions under the user

select table_name, comments from user_tab_comments where table_type = 'TABLE';

There are three fields in total:

Field name Explanation
table_name Table Name
table_type Table type (table, view)
comments Table description

The user_tables table (view), which stores information about all tables under the user, including the number of rows (num_rows), the number of blocks (blocks), and the average field size (avg_row_len) of the table.

The all_tables table (view), this table stores all table-related information under all users, and the table structure is the same as the user_table table.

The dba_tables table (view), which stores all table-related information in the system, including system tables, has the same structure as the user_table table.

Obtain and view all table field information under the user (except field description)

The user_tab_columns table (view), the view under the sys user, is derived from the user_tab_cols table and stores the field information of all tables under the user, excluding field descriptions and constraints.

select table_name  --表名
      ,column_name  --字段名
      ,data_type  --字段类型
      ,data_length  --字段长度
      ,data_precision  --字段精度(理解为整数位数)
      ,data_scale  --字段小数位位数
      ,nullable  --是否可为null
      ,data_default  --默认值
  from user_tab_columns
;

The main key fields of the table are as follows:

Field name Explanation
table_name Table Name
column_name Field name
data_type Field type (number, varchar2, etc.)
data_length Field length
data_precision Field precision (such as integer digits of type number)
data_scale Field decimal range
nullable Whether it is empty (N: not empty, Y: can be empty)
column_id Field sequence number
default_length Default length
data_default Defaults
num_distinct Field deduplication number (count(distinct col))
low_value Field minimum
high_value Field maximum
density density
num_nulls Number of nulls
num_buckets Number of barrels
last_analyzed Final analysis time (unknown specific meaning, hope to explain)
sample_size Sample size (unknown specific meaning)
character_set_nane Unknown specific meaning
char_col_decl_length Unknown specific meaning
global_stats Overall status (unknown specific meaning)
user_stats User status (unknown specific meaning)
avg_col_len Average field length (the actual storage size can be calculated)
char_length Character length
char_used Character unit (B)
v80_fmt_image Unknown specific meaning
data_upgraded Unknown specific meaning
histogram Unknown specific meaning

Get field description

The user_col_comments table (view) gets the description of all the table fields under the user

select table_name, columns_nam, comments from user_col_comments;

Note: Table_name is similar to the following figure. These are the deleted tables and
Insert picture description here
the recycle bin. The table has a total of three fields:

Field name description
table_name Table Name
column_name Field name
comments Field description

Get the primary key information of the table

Get primary key field information

select ucc.table_name, ucc.column_name
  from user_cons_columns ucc, user_constraints uc
 where uc.constraint_name = ucc.constraint_name
   and uc.constraint_type = 'P'
;

The user_constraints view, which stores information about table constraints.
Key fields:

Field name description
owner owner
constraint_name Constraint name
constraint_type Constraint type (P: primary key, U: unique, F: foreign key, etc.)
table_name Table Name
r_owner Previous owner
r_constraint_name Last constraint name
index_owner Index owner
index_name Index name

The user_cons_columns view, which stores constrained field information.
Key fields:

Field name description
owner owner
constraint_name Constraint name
table_name Table Name
column_name Field Name
position position

Get the key information of the user in the table below

Obtain key information from users through the integration of the above table

select t1.table_name --表名称
      ,t1.comments as table_comment  --表描述
      ,t2.column_name  --字段名称
      ,t3.comments as column_comment  --字段描述
      --,t2.data_type  --字段类型
      --,t2.data_length  --字段长度
      --,t2.data_precision  --字段精度
      --,t2.data_scale  --字段小数范围
      ,t2.data_type_new  --组合的字段类型
      ,case when t4.table_name is not null then 'Y'
       else null end is_pk  --是否为主键
      ,t2.is_not_null  --是否为空
      ,t2.data_default  --默认值
  from 
  ( --该用户下表名和表描述
  	select table_name, comments
  	  from user_tab_comments 
  	 where table_type = 'TABLE'
  ) t1
  left join
  ( --该用户下表名、字段信息
  	select table_name  --表名
          ,column_name  --字段名
          ,data_type  --字段类型
          ,data_length  --字段长度
          ,data_precision  --字段精度(理解为整数位数)
          ,data_scale  --字段小数位位数
          ,case when nullable = 'N' then 'Y'
           else null end as is_not_null  --是否非空
          ,data_default  --默认值
          ,case when data_precision is not null and data_scale is not null then data_type||'('||data_precision||','||data_scale||')'
                when data_precision is not null and data_scale is null then data_type||'('||data_precision||')'
                when data_precision is null and data_scale is null and data_length is not null then data_type||'('||data_length||')'
           else data_type end as data_type_new
				,column_id	 
      from user_tab_columns
  ) t2 on t1.table_name = t2.table_name
  left join
  (
	--该用户下表名和字段描述
	select table_name
	      ,column_name
	      ,comments
	  from user_col_comments
  ) t3 on t2.table_name = t3.table_name and t2.column_name = t3.column_name
  left join
  ( --该用户下的主键信息
  	select ucc.table_name, ucc.column_name
  	  from user_cons_columns ucc, user_constraints uc
  	 where uc.constraint_name = ucc.constraint_name
       and uc.constraint_type = 'P'
  ) t4 on t2.table_name = t4.table_name and t2.column_name = t4.column_name
 order by t1.table_name, t2.column_id --保证字段顺序和原表字段顺序一致
;

Guess you like

Origin blog.csdn.net/lz6363/article/details/111681631