oracle - find有关表和列的信息

ulmd4ohb  于 2023-06-22  发布在  Oracle
关注(0)|答案(2)|浏览(128)

我使用SQL Developer在数据库模式中进行各种查询。这些表列在“对象”容器下。在那里,我可以左键单击并选择viewwith,然后在Columns选项卡下显示有关表的信息。评论尤其重要。有没有一种方法可以直接通过Oracle数据字典查询它?describe或desc命令对我不起作用。

m528fe3b

m528fe3b1#

对于表,可以使用USER_TABLESUSER_TAB_COMMENTS

SELECT ut.table_name,
       ut.tablespace_name,
       utc.comments
FROM   user_tables ut
       LEFT OUTER JOIN user_tab_comments utc
       ON ut.table_name = utc.table_name
ORDER BY ut.table_name;

对于列,可以使用USER_TAB_COLUMNSUSER_COL_COMMENTS

SELECT utc.table_name,
       utc.column_name,
       utc.data_type,
       utc.data_length,
       utc.data_precision,
       utc.data_scale,
       utc.data_default,
       ucc.comments
FROM   user_tab_columns utc
       LEFT OUTER JOIN user_col_comments ucc
       ON utc.table_name = ucc.table_name AND utc.column_name = ucc.column_name
ORDER BY utc.table_name, utc.column_name, utc.column_id

如果你有table:

CREATE TABLE table_name (
  id    NUMBER(10,0),
  value VARCHAR2(20)
);
COMMENT ON TABLE table_name IS 'A table for testing comments.';
COMMENT ON COLUMN table_name.id IS 'The identifier.';
COMMENT ON COLUMN table_name.value IS 'The value.';

针对相应查询的输出:
| 表名|TABLESPACE_NAME|评论|
| - -----|- -----|- -----|
| 表名|用户|测试注解表。|
| 表名|列名称|数据类型|数据长度|数据_精密度|DATA_SCALE|数据默认值|评论|
| - -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|
| 表名|ID|数量|二十二|十个|0|联系我们|标识符。|
| 表名|价值|VARCHAR2|二十个|联系我们|联系我们|联系我们|价值。|
fiddle

klr1opcd

klr1opcd2#

查询例如

  • USER_TABLES(用于表列表)
  • USER_TAB_COLUMNS(用于这些表中的列)
  • USER_OBJECTS(用于模式中的任何对象)
  • USER_TAB_COMMENTS(用于与表和视图相关的注解)
  • USER_COL_COMMENTS(用于与这些表和视图中的列相关的注解)

您没有指定使用哪个数据库;在Oracle 11 g上,可以查询dictionary视图,该视图包含所有 * 系统视图 * 的列表和简短说明。在我看来,这是一个很酷的选择:
例如:哪些视图包含“注解”?

SQL> select * from v$version where rownum = 1;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SQL> select table_name, comments
  2  from dictionary
  3  where lower(comments) like '%comment%';

TABLE_NAME                     COMMENTS
------------------------------ ----------------------------------------------------------------------
USER_COL_COMMENTS              Comments on columns of user's tables and views
USER_INDEXTYPE_COMMENTS        Comments for user-defined indextypes
USER_MVIEW_COMMENTS            Comments on materialized views owned by the user
USER_OPERATOR_COMMENTS         Comments for user-defined operators
USER_TAB_COMMENTS              Comments on the tables and views owned by the user
ALL_COL_COMMENTS               Comments on columns of accessible tables and views
ALL_EDITION_COMMENTS           Describes comments on all editions in the database
ALL_INDEXTYPE_COMMENTS         Comments for user-defined indextypes
ALL_MVIEW_COMMENTS             Comments on materialized views accessible to the user
ALL_OPERATOR_COMMENTS          Comments for user-defined operators
ALL_TAB_COMMENTS               Comments on tables and views accessible to the user

11 rows selected.

SQL>

这同样适用于例如。18 c也一样,但它在21 cXE上不起作用,因为dictionary的整个comments列都是空的(不知道为什么):

SQL> select banner from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production

SQL> select count(*) from dictionary where comments is not null;

  COUNT(*)
----------
         0

SQL>

相关问题