22

我想要desc 表;操作以显示列的注释。我已经看到有些人做到了这一点,但我不知道如何。也许这取决于 SQL Developer 版本,我的是 2.1.0.63。数据库是Oracle 11g。

这是我在做desc table 时得到的;

Desc table;
    Name                Nullable Type
    ------------------- -------- -----
    ID                  NOT NULL NUMBER(38)
    ITEM_ID                      NUMBER(38)

我想得到这样的东西:

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

3 回答 3

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)
于 2012-06-06T11:03:38.030 回答
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
)
于 2016-04-06T10:32:03.953 回答
1

Oracle SQLcl中,一个用于 Oracle 数据库的新的现代 CLI,我们有 DESC。但我们还构建了一个名为 INFO[RMATION] 的新命令。

它默认显示列注释。

I am HR on orcl > info locations
TABLE: LOCATIONS
         LAST ANALYZED:2017-03-02 17:00:31.0
         ROWS         :23
         SAMPLE SIZE  :23
         INMEMORY     :DISABLED
         COMMENTS     :Locations table that contains specific address of a specific office,
                       warehouse, and/or production site of a company. Does not store addresses /
                       locations of customers. Contains 23 rows; references with the
                       departments and countries tables.

Columns
NAME             DATA TYPE           NULL  DEFAULT    COMMENTS
*LOCATION_ID     NUMBER(4,0)         No               Primary key of locations table
 STREET_ADDRESS  VARCHAR2(40 BYTE)   Yes              Street address of an office, warehouse, or
                                                      production site of a company.Contains building
                                                      number and street name
 POSTAL_CODE     VARCHAR2(12 BYTE)   Yes              Postal code of the location of an office,
                                                      warehouse, or production siteof a company.
 CITY            VARCHAR2(30 BYTE)   No               A not null column that shows city where an office,
                                                      warehouse, orproduction site of a company is
                                                      located.
 STATE_PROVINCE  VARCHAR2(25 BYTE)   Yes              State or Province where an office, warehouse, or
                                                      production site of acompany is located.
 COUNTRY_ID      CHAR(2 BYTE)        Yes              Country where an office, warehouse, or production
                                                      site of a company islocated. Foreign key to
                                                      country_id column of the countries table.

Indexes
INDEX_NAME                 UNIQUENESS   STATUS   FUNCIDX_STATUS   COLUMNS
HR.LOC_ID_PK               UNIQUE       VALID                     LOCATION_ID
HR.LOC_CITY_IX             NONUNIQUE    VALID                     CITY
HR.LOC_COUNTRY_IX          NONUNIQUE    VALID                     COUNTRY_ID
HR.LOC_STATE_PROVINCE_IX   NONUNIQUE    VALID                     STATE_PROVINCE


References
TABLE_NAME    CONSTRAINT_NAME   DELETE_RULE   STATUS    DEFERRABLE       VALIDATED   GENERATED
DEPARTMENTS   DEPT_LOC_FK       NO ACTION     ENABLED   NOT DEFERRABLE   VALIDATED   USER NAME

I am HR on orcl >

如果您运行 INFO+,它会将列注释替换为列统计信息。

I am HR on orcl > info+ locations
TABLE: LOCATIONS
         LAST ANALYZED:2017-03-02 17:00:31.0
         ROWS         :23
         SAMPLE SIZE  :23
         INMEMORY     :DISABLED
         COMMENTS     :Locations table that contains specific address of a specific office,
                       warehouse, and/or production site of a company. Does not store addresses /
                       locations of customers. Contains 23 rows; references with the
                       departments and countries tables.

Columns
NAME             DATA TYPE           NULL  DEFAULT    LOW_VALUE               HIGH_VALUE              NUM_DISTINCT   HISTOGRAM
*LOCATION_ID     NUMBER(4,0)         No                   1000                    3200                    23             NONE
 STREET_ADDRESS  VARCHAR2(40 BYTE)   Yes                  12-98 Victoria Street   Schwanthalerstr. 7031   23             NONE
 POSTAL_CODE     VARCHAR2(12 BYTE)   Yes                  00989                   YSW 9T2                 22             NONE
 CITY            VARCHAR2(30 BYTE)   No                   Beijing                 Whitehorse              23             NONE
 STATE_PROVINCE  VARCHAR2(25 BYTE)   Yes                  BE                      Yukon                   17             NONE
 COUNTRY_ID      CHAR(2 BYTE)        Yes                                                                  14             FREQUENCY

Indexes
INDEX_NAME                 UNIQUENESS   STATUS   FUNCIDX_STATUS   COLUMNS
HR.LOC_ID_PK               UNIQUE       VALID                     LOCATION_ID
HR.LOC_CITY_IX             NONUNIQUE    VALID                     CITY
HR.LOC_COUNTRY_IX          NONUNIQUE    VALID                     COUNTRY_ID
HR.LOC_STATE_PROVINCE_IX   NONUNIQUE    VALID                     STATE_PROVINCE


References
TABLE_NAME    CONSTRAINT_NAME   DELETE_RULE   STATUS    DEFERRABLE       VALIDATED   GENERATED
DEPARTMENTS   DEPT_LOC_FK       NO ACTION     ENABLED   NOT DEFERRABLE   VALIDATED   USER NAME

I am HR on orcl >
于 2018-03-27T19:41:05.313 回答