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

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

I'm having a hard time with a class I am taking. We need to write an Oracle script that will act just like the DESCRIBE command. The book we are using describes how to work with the Data Dictionary very poorly. Not looking for answers, but a point in the correct direction.

推荐答案

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

You're looking for USER_TAB_COLUMNS - all the columns, and their descriptions in the schema the query is executed in - or ALL_TAB_COLUMNS - the same except for all tables that user has permission to view.

一个典型的查询可能是:

A typical query might be:

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

column_id 是表中列的顺序".

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

You should ensure that 'MY_TABLE' is capitalised unless you've been adding tables with casing ( a bad idea ) in which case you need to use something like = "MyTable".

特别是 desc 等价于我从 ss64 中窃取的以下内容,一个优秀的 Oracle资源:

Specifically desc is equivalent to the following which I stole from ss64, a good Oracle resource:

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找到所有此类视图,这是数据字典 或查看文档.

You can find all of this sort of view by select * from dictionary, which is the top level of the data dictionary or by looking at the documentation.

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

There is also the DBA_TAB_COLUMNS, which is the same as ALL_TAB_COLUMNS, but for every table in the database. This assumes that you have the privileges to view both it and the tables. If you do not have access to this table you need to get your DBA to grant you the SELECT ANY DICTIONARY privilege.

相关文章