Oracle数据库查询表的主键列名、序列名和外键,以及查询表结构

1.查询表中的主键和序列

select  uc.table_name  表名, ucc.column_name  主键列名, uic.column_name 序列列名

  from  user_constraints uc, user_cons_columns  ucc, user_indexes  ui,user_ind_columns  uic

where

-----关联关系

uc.table_name  = ucc.table_name

and  uc.table_name  =  ui.table_name

and  ui.table_name  =  uic.table_name

-----表名

and  uc.table_name  = 'XXX(表名大写)'

-----主键名

and  uc.constraint_type  =  'P'

and  uc.constraint_name  =  ucc.constraint_name

-----序列名

and  ui.index_name  =  uic.index_name

2.查询外键

select uc.table_name 表名, ucc.column_name 外键列名,uc.constraint_type
 
  from  user_constraints uc, user_cons_columns ucc
 
where
 
-----关联关系
 
uc.table_name  =  ucc.table_name
 
------表名
 
and  uc.table_name = 'XXX(一定要注意表名大写)'
 
------外键名
 
and  uc.constraint_type =  'R'

and  uc.constraint_name = ucc.constraint_name

3.查询表结构字段

select uc.table_name   表名,
       utc.comments    表说明,
       uc.column_name  字段名,
       ucc.comments    字段说明,
       uc.data_type    数据类型,
       uc.data_length  长度,
       uc.nullable     是否为空,
       uc.data_default 默认值
  from cols uc, user_col_comments ucc, user_tab_comments utc

 where uc.table_name = ucc.table_name
      
   and uc.column_name = ucc.column_name
      
   and uc.table_name = utc.table_name
      
   and uc.table_name='XXX(一定要注意表名大写)'
   
 order by uc.table_name, uc.column_id

 

你可能感兴趣的:(数据库,查询主键,查询序列,查询表结构,数据字典整理)