【问题标题】:How can I describe a table in Oracle without using the DESCRIBE command?如何在不使用 DESCRIBE 命令的情况下在 Oracle 中描述表?
【发布时间】:2012-04-08 22:59:34
【问题描述】:

我正在上的一门课很难。我们需要编写一个类似于 DESCRIBE 命令的 Oracle 脚本。我们正在使用的这本书描述了如何非常糟糕地使用数据字典。不是在寻找答案,而是在寻找正确的方向。

【问题讨论】:

    标签: oracle oracle11g sql-scripts data-dictionary


    【解决方案1】:

    您正在查找 USER_TAB_COLUMNS - 所有列及其在执行查询的架构中的描述 - 或 ALL_TAB_COLUMNS - 除了用户有权查看的所有表之外,其他相同。

    一个典型的查询可能是:

    select *
      from user_tab_columns
     where table_name = 'MY_TABLE'
     order by column_id
    

    column_id 是表中列的“顺序”。

    您应该确保“MY_TABLE”是大写的,除非您一直在添加带有大小写的表(一个坏主意),在这种情况下您需要使用类似= "MyTable" 的东西。

    具体desc 相当于我从ss64 偷来的以下内容,这是一个很好的Oracle 资源:

    select column_name as "Name"
         , nullable as "Null?"
         , concat(concat(concat(data_type,'('),data_length),')') as "Type"
      from user_tab_columns
     where table_name = 'MY_TABLE';
    

    您可以通过select * from dictionarydata dictionary 的顶层)或查看documentation 找到所有此类视图。

    还有DBA_TAB_COLUMNS,与ALL_TAB_COLUMNS 相同,但针对数据库中的每个表。这假定您具有查看它和表的权限。如果您无权访问此表,则需要让您的 DBA 授予您SELECT ANY DICTIONARY 权限。

    【讨论】:

    • 谢谢!这绝对是我见过的最好的解释。
    【解决方案2】:

    您还可以检索可用于重新创建表的整个命令:

    select dbms_metadata.get_ddl('TABLE','<my table name>','<table owner>') from dual;
    

    【讨论】:

    • 这个答案很好,因为它还提供了表的索引和键。
    • 视觉上更吸引人的是这个轻微的修改:select regexp_replace(dbms_metadata.get_ddl('TABLE','&lt;table name&gt;','&lt;table owner&gt;'), '\t|\s{2}', CHR(13)) as ddl from dual
    【解决方案3】:

    Oracle 有一组表,其中包含有关数据库结构的元数据。有一张桌子。视图表。列表。您可以使用视图查询这些表,例如 USER_TABLES(架构中的表)、ALL_TABLES(您有权查看的表)、DBA_TABLES(所有表,如果您有权限)。更一般地说,许多数据库供应商支持“信息模式”,它提供跨供应商的元数据的一致视图。在此处搜索“ALL_TABLES”并查看所有其他可用信息http://docs.oracle.com/cd/B28359_01/server.111/b28320/toc.htm

    【讨论】:

      【解决方案4】:

      Oracle SQLcl 中新引入的是information 命令或简称为INFO table_name。 它有一个简单的语法,比如DESC[RIBE]:

      SQL> info
      INFORMATION
      --------
      
      This command is like describe but with more details about the objects requested.
      
      INFO[RMATION] {[schema.]object[@connect_identifier]}
      INFO+ will show column statistics
      

      DESCRIBE 相比,它的输出要好得多且具有描述性。它列出了有关表、视图或同义词的列定义或函数或过程规范的更详细信息。

      例如:这是我在运行时在 SQLcl: Release 18.1.1 中得到的输出

      info employees

      SQL> info employees;
      TABLE: EMPLOYEES 
           LAST ANALYZED:2018-05-26 15:07:58.0 
           ROWS         :107 
           SAMPLE SIZE  :107 
           INMEMORY     :DISABLED 
           COMMENTS     :employees table. Contains 107 rows. References with departments, 
                             jobs, job_history tables. Contains a self reference. 
      
      Columns 
      NAME             DATA TYPE           NULL  DEFAULT    COMMENTS
      *EMPLOYEE_ID     NUMBER(6,0)         No               Primary key of employees table.
       FIRST_NAME      VARCHAR2(20 BYTE)   Yes              First name of the employee. A not null column.
       LAST_NAME       VARCHAR2(25 BYTE)   No               Last name of the employee. A not null column.
       EMAIL           VARCHAR2(25 BYTE)   No               Email id of the employee
       PHONE_NUMBER    VARCHAR2(20 BYTE)   Yes              Phone number of the employee; includes country
                                                            code and area code
       HIRE_DATE       DATE                No               Date when the employee started on this job. A not
                                                            null column.
       JOB_ID          VARCHAR2(10 BYTE)   No               Current job of the employee; foreign key to job_id
                                                            column of the jobs table. A not null column.
       SALARY          NUMBER(8,2)         Yes              Monthly salary of the employee. Must be greater
                                                            than zero (enforced by constraint emp_salary_min)
       COMMISSION_PCT  NUMBER(2,2)         Yes              Commission percentage of the employee; Only
                                                            employees in sales department elgible for
                                                            commission percentage
       MANAGER_ID      NUMBER(6,0)         Yes              Manager id of the employee; has same domain as
                                                            manager_id in departments table. Foreign key to
                                                            employee_id column of employees table.(useful for
                                                            reflexive joins and CONNECT BY query)
       DEPARTMENT_ID   NUMBER(4,0)         Yes              Department id where employee works; foreign key to
                                                            department_id column of the departments table
      
      Indexes
      INDEX_NAME             UNIQUENESS   STATUS   FUNCIDX_STATUS   COLUMNS                 
      HR.EMP_JOB_IX          NONUNIQUE    VALID                     JOB_ID                  
      HR.EMP_NAME_IX         NONUNIQUE    VALID                     LAST_NAME, FIRST_NAME   
      HR.EMP_EMAIL_UK        UNIQUE       VALID                     EMAIL                   
      HR.EMP_EMP_ID_PK       UNIQUE       VALID                     EMPLOYEE_ID             
      HR.EMP_MANAGER_IX      NONUNIQUE    VALID                     MANAGER_ID              
      HR.EMP_DEPARTMENT_IX   NONUNIQUE    VALID                     DEPARTMENT_ID           
      
      
      References
      TABLE_NAME    CONSTRAINT_NAME   DELETE_RULE   STATUS    DEFERRABLE       VALIDATED   GENERATED   
      DEPARTMENTS   DEPT_MGR_FK       NO ACTION     ENABLED   NOT DEFERRABLE   VALIDATED   USER NAME   
      EMPLOYEES     EMP_MANAGER_FK    NO ACTION     ENABLED   NOT DEFERRABLE   VALIDATED   USER NAME   
      JOB_HISTORY   JHIST_EMP_FK      NO ACTION     ENABLED   NOT DEFERRABLE   VALIDATED   USER NAME   
      

      这是info+的屏幕截图:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        • 2011-03-10
        • 2016-10-20
        相关资源
        最近更新 更多