如何在不使用DESCRIBE命令的情况下在Oracle中描述表?

时间:2012-03-24 20:15:46

标签: oracle oracle11g sql-scripts data-dictionary

我正在参加我正在上课的课程。我们需要编写一个像DESCRIBE命令一样的Oracle脚本。我们使用的这本书描述了如何使用数据字典非常糟糕。不是寻找答案,而是寻找正确方向的一点。

4 个答案:

答案 0 :(得分:35)

您正在寻找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 dictionary找到所有这类视图,这是data dictionary的顶级或查看documentation

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

答案 1 :(得分:14)

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

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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+的屏幕截图:

enter image description here