`

获取ORACLE 表字段,表名,以及主键之类等等的信息。

阅读更多

获取表名:

 Oracle的user_talbes用于记录了用户表信息。

 

select * from user_tables

  获取某个表的字段:

 

USER_TAB_COLS中记录了用户表的列信息。 

 

SELECT USER_TAB_COLS.TABLE_NAME   as 表名,
       USER_TAB_COLS.COLUMN_NAME  as 列名,
       USER_TAB_COLS.DATA_TYPE    as 数据类型,
       USER_TAB_COLS.DATA_LENGTH  as 长度,
       USER_TAB_COLS.NULLABLE     as 是否为空,
       USER_TAB_COLS.COLUMN_ID    as 列序号,
       user_col_comments.comments as 备注
  FROM USER_TAB_COLS
 inner join user_col_comments
    on user_col_comments.TABLE_NAME = USER_TAB_COLS.TABLE_NAME
   and user_col_comments.COLUMN_NAME = USER_TAB_COLS.COLUMN_NAME

 如何从Oracle、中取得表的注释

 

user_tab_comments;表注释

        user_col_comments;表字段注释

        以上两个只能获取自己用户的表的注释信息,如果要访问自己能够访问的其他用户的表,则需要使用:

        all_tab_comments;表注释

        all_col_comments;表字段注释

        当然,如果有DBA权限,则可以使用

        dba_tab_comments;表注释

        dba_col_comments;表字段注释

        dba*和all*最好指定owner条件。user*没有该字段

        user_tab_comments;表注释

        user_col_comments;表字段注释

        以上两个只能获取自己用户的表的注释信息,如果要访问自己能够访问的其他用户的表,则需要使用:

        all_tab_comments;表注释

        all_col_comments;表字段注释

        当然,如果有DBA权限,则可以使用

        dba_tab_comments;表注释

        dba_col_comments;表字段注释

        dba*和all*最好指定owner条件。user*没有该字段

 

关于Oracle与SqlServer中获取所有字段、主键、外键的sql语句 标签: 主键  外键  sql  
最近在做的社会网络分析原型系统需要将多种不同数据库中的表的字段、主外键信息读出,实现这些功能费了不少功夫,记录下来以备用吧

 

Oracle:
查询某个表中的字段名称、类型、精度、长度、是否为空

 

select COLUMN_NAME, DATA_TYPE, DATA_PRECISION, DATA_SCALE, NULLABLE
  from user_tab_columns
 where table_name = 'T_CUST'

 

 查询某个表中的主键字段名

 

select col.column_name 
from user_constraints con,  user_cons_columns col 
where con.constraint_name = col.constraint_name 
and con.constraint_type='P' 
and col.table_name = 'YourTableName'

  查询某个表中的外键字段名称、所引用表名、所应用字段名

 

select distinct (col.column_name), r.table_name, r.column_name
  from user_constraints con,
       user_cons_columns col,
       (select t2.table_name, t2.column_name, t1.r_constraint_name
          from user_constraints t1, user_cons_columns t2
         where t1.r_constraint_name = t2.constraint_name
           and t1.table_name = 'YourTableName') r
 where con.constraint_name = col.constraint_name
   and con.r_constraint_name = r.r_constraint_name
   and con.table_name = 'YourTableName'
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics