如何使用desc操作显示列的注释

Gui*_*ito 20 oracle oracle-sqldeveloper

我想要desc表; 操作以显示列的注释.我已经看到有些人实现了这个目标,但我无法理解.也许它取决于SQL Developer版本,我的是2.1.0.63.数据库是Oracle 11g.

这是我在做desc表时得到的; :

Desc table;
    Name                Nullable Type
    ------------------- -------- -----
    ID                  NOT NULL NUMBER(38)
    ITEM_ID                      NUMBER(38)
Run Code Online (Sandbox Code Playgroud)

我想得到这样的东西:

Desc table;
    Name                Nullable Type        Comment
    ------------------- -------- ----------  ---------------------------------
    ID                  NOT NULL NUMBER(38)  Table's id
    ITEM_ID                      NUMBER(38)  Reference to an item
Run Code Online (Sandbox Code Playgroud)

win*_*ace 25

对于不同的工具,desc命令的解释方式不同.它的作用是选择一些标准的Oracle视图.

以下是对将提供所需列数据的视图的查询,但我建议您执行select*以查看所有可用的列数据.

您有3种类型的视图,dba_ ,all_和user_*视图.我使用user_*因为它可用于每个模式/用户,但它仅列出该模式/用户拥有的对象.dba_视图通常仅用于dba,并且all_视图可能会或可能不会为您提供,具体取决于您的dba对您的信任程度.^ _ ^

select tc.column_name
,      tc.nullable
,      tc.data_type || case when tc.data_type = 'NUMBER' and tc.data_precision is not null then '(' || tc.data_precision || ',' || tc.data_scale || ')'
                            when tc.data_type like '%CHAR%' then '(' || tc.data_length || ')'
                            else null
                       end type
,      cc.comments
from   user_col_comments cc
join   user_tab_columns  tc on  cc.column_name = tc.column_name
                            and cc.table_name  = tc.table_name
where  cc.table_name = upper(:tablename)
Run Code Online (Sandbox Code Playgroud)


Pav*_*nar 5

这是来自Oracle SQL Developer的定义(如表列视图所示):

SELECT "COLUMN_NAME", "DATA_TYPE", "NULLABLE", "DATA_DEFAULT", "COLUMN_ID", "COMMENTS" FROM(
select c.column_name,  case when data_type = 'CHAR'     then      data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'    
                            when data_type = 'VARCHAR'  then      data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'    
                            when data_type = 'VARCHAR2' then      data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'    
                            when data_type = 'NCHAR'    then      data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'    
                            when data_type = 'NUMBER' then      
                                    case when c.data_precision is null and c.data_scale is null then          'NUMBER' 
                                    when c.data_precision is null and c.data_scale is not null then          'NUMBER(38,'||c.data_scale||')' 
                                    else           data_type||'('||c.data_precision||','||c.data_SCALE||')'      end    
                            when data_type = 'NVARCHAR' then      data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'    
                            when data_type = 'NVARCHAR2' then     data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'    
                            else      data_type    end data_type,
  decode(nullable,'Y','Yes','No') nullable,  
c.DATA_DEFAULT,column_id,   com.comments         
  from sys.Dba_tab_Columns c, 
       sys.Dba_col_comments com
  where c.owner      = :OBJECT_OWNER  
  and  c.table_name =  :OBJECT_NAME   
  and c.table_name = com.table_name
  and c.owner = com.owner
  and c.column_name = com.column_name                 
  order by column_id
)
Run Code Online (Sandbox Code Playgroud)