ORACLE 查询某表中的某个字段的类型,是否为空,是否有默认值等

时间:2022-05-30 13:32:31
最近写的功能中有这样一个小功能,根据数据库查询此库中是否有某表,如果有,查询某表下面的某个字段的详细信息


其中一种是...
select ATC.OWNER,
atC.TABLE_NAME,
ATC.COLUMN_NAME,
ATC.DATA_TYPE,
ATC.DATA_LENGTH,
ATC.NULLABLE,
ucc.comments
from (select ATC.OWNER,
atC.TABLE_NAME,
ATC.COLUMN_NAME,
ATC.DATA_TYPE,
ATC.DATA_LENGTH,
ATC.NULLABLE
from all_tab_columns ATC
where ATC.TABLE_NAME = '要查询的字段名'
and ATC.COLUMN_NAME
in ('要查询的字段名','要查询的字段名','要查询的字段名')) atc
left outer join user_col_comments ucc on atc.table_name
= ucc.table_name
and atc.column_name
=
ucc.column_name
left outer join user_tab_comments utc on atc.table_name
= utc.table_name
order by atc.table_name, atc.column_name;

还有一种是

 

 1 SELECT USER_TAB_COLS.TABLE_NAME   as 表名,
2 USER_TAB_COLS.COLUMN_NAME as 列名,
3 USER_TAB_COLS.DATA_TYPE as 数据类型,
4 USER_TAB_COLS.DATA_LENGTH as 长度,
5 USER_TAB_COLS.NULLABLE as 是否为空,
6 USER_TAB_COLS.COLUMN_ID as 列序号,
7 user_col_comments.comments as 备注
8 FROM USER_TAB_COLS
9 inner join user_col_comments on user_col_comments.TABLE_NAME =
10 USER_TAB_COLS.TABLE_NAME
11 and user_col_comments.COLUMN_NAME =
12 USER_TAB_COLS.COLUMN_NAME where user_col_comments.TABLE_NAME='数据库名' AND USER_TAB_COLS.COLUMN_NAME in ('要查询的字段名','要查询的字段名','要查询的字段名')
13

  

查询某库下面的某表全部字段

 select 
ut.COLUMN_NAME,--字段名称
uc.comments,--字段注释
ut.DATA_TYPE,--字典类型
ut.DATA_LENGTH,--字典长度
ut.NULLABLE--是否为空
from user_tab_columns ut
inner JOIN user_col_comments uc
on ut.TABLE_NAME = uc.table_name and ut.COLUMN_NAME = uc.column_name
where ut.Table_Name='某表名'
order by ut.column_name

  

以上SQL都已经运用到程序中,可以直接贴到sql中用

 

欢迎大家提出问题