1. 程式人生 > 其它 >oracle元資料獲取使用者下的表關鍵資訊

oracle元資料獲取使用者下的表關鍵資訊

技術標籤:oralce

目錄

瞭解一個業務系統的資料情況,所有要了解該業務系統的表結構資訊及ER關係圖。本文主要聊聊通過oracle元資料獲取該使用者下所有表的關鍵資訊,比如:表名、表描述、欄位名、欄位描述、欄位型別、欄位長度、是否主鍵、是否非空、預設值等。

獲取該使用者下所有表名和表描述

user_tab_comments表(檢視),該表儲存的是該使用者下所有表和描述

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

總共三個欄位:

欄位名解釋
table_name表名
table_type表型別(table、view)
comments表描述

user_tables表(檢視),該表儲存該使用者下所有表相關資訊,包括表的行數(num_rows)、塊數(blocks)、欄位平均大小(avg_row_len)等資訊。

all_tables表(檢視),該表儲存所有使用者下所有表相關資訊,表結構和user_table表一樣。

dba_tables表(檢視),該表儲存系統內所有表相關資訊,包括系統表,結構和user_table表一樣。

獲取檢視該使用者下所以表字段資訊(除欄位描述)

user_tab_columns表(檢視),在sys使用者下的檢視,來源於user_tab_cols表,儲存該使用者下所有表的欄位資訊,不包括欄位描述和約束等。

select table_name  --表名
      ,column_name  --欄位名
      ,data_type  --欄位型別
      ,data_length  --欄位長度
      ,data_precision  --欄位精度(理解為整數位數)
      ,data_scale  --欄位小數位位數
      ,nullable  --是否可為null
      ,data_default  --預設值
from user_tab_columns ;

該表主要關鍵欄位有如下:

欄位名解釋
table_name表名
column_name欄位名
data_type欄位型別(number、varchar2等)
data_length欄位長度
data_precision欄位精度(比如number型別的整數位)
data_scale欄位小數範圍
nullable是否為空(N:不為空,Y:可為空)
column_id欄位順序序號
default_length預設長度
data_default預設值
num_distinct欄位去重數(count(distinct col))
low_value欄位最小值
high_value欄位最大值
density密度
num_nulls空值數
num_buckets桶數
last_analyzed最後分析時間(未知具體含義,望大佬解釋)
sample_size樣品大小(未知具體含義)
character_set_nane未知具體含義
char_col_decl_length未知具體含義
global_stats整體狀態(未知具體含義)
user_stats使用者狀態(未知具體含義)
avg_col_len欄位平均長度(可以計算實際儲存大小)
char_length字元長度
char_used字元單位(B)
v80_fmt_image未知具體含義
data_upgraded未知具體含義
histogram未知具體含義

獲取欄位描述

user_col_comments表(檢視)獲取該使用者下所有表字段描述

select table_name, columns_nam, comments from user_col_comments;

注: 其中有table_name類似於下圖的,這些是被刪除存在回收站的表
在這裡插入圖片描述
該表總共三個欄位:

欄位名描述
table_name表名
column_name欄位名
comments欄位描述

獲取表的主鍵資訊

獲取主鍵欄位資訊

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'
;

user_constraints檢視,儲存表約束相關資訊。
關鍵欄位:

欄位名描述
owner所有者
constraint_name約束名稱
constraint_type約束型別(P:主鍵,U:唯一,F:外來鍵等)
table_name表名
r_owner上一個所有者
r_constraint_name上一個約束名稱
index_owner索引所有者
index_name索引名稱

user_cons_columns檢視,儲存約束的欄位資訊。
關鍵欄位:

欄位名描述
owner所有者
constraint_name約束名稱
table_name表名
column_name欄位名稱
position位置

獲取使用者下表關鍵資訊

通過以上表整合獲取使用者下關鍵資訊

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 --保證欄位順序和原表字段順序一致
;