39

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

4

4 回答 4

56

您正在查找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的顶层或查看文档来找到所有此类视图。

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

于 2012-03-24T20:45:07.277 回答
20

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

select dbms_metadata.get_ddl('TABLE','<my table name>','<table owner>') from dual;
于 2012-03-24T23:03:41.767 回答
1

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

于 2012-03-24T20:28:39.917 回答
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 中得到的输出:当我运行时发布 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+

在此处输入图像描述

于 2018-05-26T18:23:36.327 回答