如何查询Oracle数据库以显示Oracle数据库中所有表的名称?
当前回答
尝试从user_tables中选择,其中列出了当前用户拥有的表。
其他回答
SQLcl(Oracle数据库的免费命令行接口)中的一个新特性是
表别名。
下面是几个示例,展示了该特性的用法和其他方面。首先,连接到sql命令行会话(windows中的sql.exe)。建议在运行任何其他显示数据的命令或查询之前输入此sqlcl特定命令。
SQL> set sqlformat ansiconsole -- resizes the columns to the width of the
-- data to save space
SQL >表
TABLES
-----------
REGIONS
LOCATIONS
DEPARTMENTS
JOBS
EMPLOYEES
JOB_HISTORY
..
要知道表的别名指的是什么,可以简单地使用alias list <alias>
SQL> alias list tables
tables - tables <schema> - show tables from schema
--------------------------------------------------
select table_name "TABLES" from user_tables
您不必定义这个别名,因为它在SQLcl下默认存在。如果希望从特定模式列出表,使用新的用户定义别名并将模式名作为绑定参数传递,只显示一组列,则可以使用
SQL> alias tables_schema = select owner, table_name, last_analyzed from all_tables where owner =:ownr
此后,您可以简单地将模式名作为参数传递
SQL> tables_schema HR . SQL
OWNER TABLE_NAME LAST_ANALYZED
HR DUMMY1 18-10-18
HR YOURTAB2 16-11-18
HR YOURTABLE 01-12-18
HR ID_TABLE 05-12-18
HR REGIONS 26-05-18
HR LOCATIONS 26-05-18
HR DEPARTMENTS 26-05-18
HR JOBS 26-05-18
HR EMPLOYEES 12-10-18
..
..
一个更复杂的预定义别名称为Tables2,它显示其他几个列。
SQL> tables2
Tables
======
TABLE_NAME NUM_ROWS BLOCKS UNFORMATTED_SIZE COMPRESSION INDEX_COUNT CONSTRAINT_COUNT PART_COUNT LAST_ANALYZED
AN_IP_TABLE 0 0 0 Disabled 0 0 0 > Month
PARTTABLE 0 0 0 1 0 1 > Month
TST2 0 0 0 Disabled 0 0 0 > Month
TST3 0 0 0 Disabled 0 0 0 > Month
MANAGE_EMPLYEE 0 0 0 Disabled 0 0 0 > Month
PRODUCT 0 0 0 Disabled 0 0 0 > Month
ALL_TAB_X78EHRYFK 0 0 0 Disabled 0 0 0 > Month
TBW 0 0 0 Disabled 0 0 0 > Month
DEPT 0 0 0 Disabled 0 0 0 > Month
要知道它在后台运行什么查询,请输入
alias list tables2
这将显示稍微复杂一些的查询以及SQL*Plus中常用的预定义列定义。
Jeff Smith在这里解释了更多关于别名的知识
有了这些,你可以选择:
SELECT DISTINCT OWNER, OBJECT_NAME
FROM DBA_OBJECTS
WHERE OBJECT_TYPE = 'TABLE' AND OWNER='SOME_SCHEMA_NAME';
SELECT DISTINCT OWNER, OBJECT_NAME
FROM ALL_OBJECTS
WHERE OBJECT_TYPE = 'TABLE' AND OWNER='SOME_SCHEMA_NAME';
当前用户登录模式中的表
select * from tabs;
SELECT owner, table_name
FROM dba_tables
This is assuming that you have access to the DBA_TABLES data dictionary view. If you do not have those privileges but need them, you can request that the DBA explicitly grants you privileges on that table, or, that the DBA grants you the SELECT ANY DICTIONARY privilege or the SELECT_CATALOG_ROLE role (either of which would allow you to query any data dictionary table). Of course, you may want to exclude certain schemas like SYS and SYSTEM which have large numbers of Oracle tables that you probably don't care about.
或者,如果您没有访问DBA_TABLES,您可以通过ALL_TABLES视图查看您的帐户访问的所有表:
SELECT owner, table_name
FROM all_tables
不过,这可能是数据库中可用表的一个子集(ALL_TABLES显示用户已被授予访问权限的所有表的信息)。
如果你只关心你拥有的表,而不是那些你可以访问的表,你可以使用USER_TABLES:
SELECT table_name
FROM user_tables
因为USER_TABLES只包含关于您拥有的表的信息,所以它没有OWNER列——根据定义,所有者就是您自己。
Oracle also has a number of legacy data dictionary views-- TAB, DICT, TABS, and CAT for example-- that could be used. In general, I would not suggest using these legacy views unless you absolutely need to backport your scripts to Oracle 6. Oracle has not changed these views in a long time so they often have problems with newer types of objects. For example, the TAB and CAT views both show information about tables that are in the user's recycle bin while the [DBA|ALL|USER]_TABLES views all filter those out. CAT also shows information about materialized view logs with a TABLE_TYPE of "TABLE" which is unlikely to be what you really want. DICT combines tables and synonyms and doesn't tell you who owns the object.
我找不到一个有用的答案
DBA_ALL_TABLES (ALL_ALL_TABLES/USER_ALL_TABLES)
所以决定加上我的版本。 这个视图实际上返回比DBA_TABLES更多的数据,因为它也返回对象表(http://docs.oracle.com/cd/E11882_01/server.112/e40402/statviews_1003.htm)。
推荐文章
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- 查询JSON类型内的数组元素
- 确定记录是否存在的最快方法
- 获得PostgreSQL数据库中当前连接数的正确查询
- 在SQL选择语句Order By 1的目的是什么?
- 我如何循环通过一组记录在SQL Server?
- 如何从命令行通过mysql运行一个查询?
- 外键约束可能导致循环或多条级联路径?
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- ORA-12505, TNS:监听器当前不知道连接描述符中给定的SID
- 货币应该使用哪种数据类型?
- 如何选择每一行的列值不是独特的
- 如何改变字符集(和排序)在整个数据库?
- mySQL:: insert到表,数据从另一个表?