Get list of all tables in Oracle? - sql

How do I query an Oracle database to display the names of all tables in it?

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.
Alternatively, if you do not have access to DBA_TABLES, you can see all the tables that your account has access to through the ALL_TABLES view:
SELECT owner, table_name
FROM all_tables
Although, that may be a subset of the tables available in the database (ALL_TABLES shows you the information for all the tables that your user has been granted access to).
If you are only concerned with the tables that you own, not those that you have access to, you could use USER_TABLES:
SELECT table_name
FROM user_tables
Since USER_TABLES only has information about the tables that you own, it does not have an OWNER column – the owner, by definition, is you.
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.

Querying user_tables and dba_tables didn't work.
This one did:
select table_name from all_tables

Going one step further, there is another view called cols (all_tab_columns) which can be used to ascertain which tables contain a given column name.
For example:
SELECT table_name, column_name
FROM cols
WHERE table_name LIKE 'EST%'
AND column_name LIKE '%CALLREF%';
to find all tables having a name beginning with EST and columns containing CALLREF anywhere in their names.
This can help when working out what columns you want to join on, for example, depending on your table and column naming conventions.

For better viewing with sqlplus
If you're using sqlplus you may want to first set up a few parameters for nicer viewing if your columns are getting mangled (these variables should not persist after you exit your sqlplus session ):
set colsep '|'
set linesize 167
set pagesize 30
set pagesize 1000
Show All Tables
You can then use something like this to see all table names:
SELECT table_name, owner, tablespace_name FROM all_tables;
Show Tables You Own
As #Justin Cave mentions, you can use this to show only tables that you own:
SELECT table_name FROM user_tables;
Don't Forget about Views
Keep in mind that some "tables" may actually be "views" so you can also try running something like:
SELECT view_name FROM all_views;
The Results
This should yield something that looks fairly acceptable like:

Simple query to select the tables for the current user:
SELECT table_name FROM user_tables;

select object_name from user_objects where object_type='TABLE';
----------------OR------------------
select * from tab;
----------------OR------------------
select table_name from user_tables;

Try the below data dictionary views.
tabs
dba_tables
all_tables
user_tables

Execute the below commands:
Show all tables in the Oracle Database
sql> SELECT table_name FROM dba_tables;
Show tables owned by the current user
sql> SELECT table_name FROM user_tables;
Show tables that are accessible by the current user
sql> SELECT table_name FROM all_tables ORDER BY table_name;

Oracle database to display the names of all tables using below query
SELECT owner, table_name FROM dba_tables;
SELECT owner, table_name FROM all_tables;
SELECT table_name FROM user_tables;
vist more : http://www.plsqlinformation.com/2016/08/get-list-of-all-tables-in-oracle.html

Try selecting from user_tables which lists the tables owned by the current user.

With any of those, you can select:
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 dba_tables
gives all the tables of all the users only if the user with which you logged in is having the sysdba privileges.

I did not find answer which would point to use
DBA_ALL_TABLES (ALL_ALL_TABLES/USER_ALL_TABLES)
so decided to add my version as well.
This view actually returns more that DBA_TABLES as it returns object tables as well (http://docs.oracle.com/cd/E11882_01/server.112/e40402/statviews_1003.htm).

A new feature available in SQLcl( which is a free command line interface for Oracle Database) is
Tables alias.
Here are few examples showing the usage and additional aspects of the feature. First, connect to a sql command line (sql.exe in windows) session. It is recommended to enter this sqlcl specific command before running any other commands or queries which display data.
SQL> set sqlformat ansiconsole -- resizes the columns to the width of the
-- data to save space
SQL> tables
TABLES
-----------
REGIONS
LOCATIONS
DEPARTMENTS
JOBS
EMPLOYEES
JOB_HISTORY
..
To know what the tables alias is referring to, you may simply use alias list <alias>
SQL> alias list tables
tables - tables <schema> - show tables from schema
--------------------------------------------------
select table_name "TABLES" from user_tables
You don't have to define this alias as it comes by default under SQLcl. If you want to list tables from a specific schema, using a new user-defined alias and passing schema name as a bind argument with only a set of columns being displayed, you may do so using
SQL> alias tables_schema = select owner, table_name, last_analyzed from all_tables where owner = :ownr;
Thereafter you may simply pass schema name as an argument
SQL> tables_schema HR
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
..
..
A more sophisticated pre-defined alias is known as Tables2, which displays several other columns.
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
To know what query it runs in the background, enter
alias list tables2
This will show you a slightly more complex query along with predefined column definitions commonly used in SQL*Plus.
Jeff Smith explains more about aliases here

You can use Oracle Data Dictionary to get information about oracle objects.
You can get list of tables in different ways:
select *
from dba_tables
or for example:
select *
from dba_objects
where object_type = 'TABLE'
Then you can get table columns using table name:
select *
from dba_tab_columns
Then you can get list of dependencies (triggers, views and etc.):
select *
from dba_dependencies
where referenced_type='TABLE' and referenced_name=:t_name
Then you can get text source of this objects:
select * from dba_source
And you can use USER or ALL views instead of DBA if you want.

We can get all tables including column details from below query:
SELECT * FROM user_tab_columns;

Including views:
SELECT owner, table_name as table_view
FROM dba_tables
UNION ALL
SELECT owner, view_name as table_view
FROM DBA_VIEWS

Below is a commented snippet of SQL queries describing how options you can make use of:
-- need to have select catalog role
SELECT * FROM dba_tables;
-- to see tables of your schema
SELECT * FROM user_tables;
-- tables inside your schema and tables of other schema which you possess select grants on
SELECT * FROM all_tables;

The following query only list the required data, whereas the other answers gave me the extra data which only confused me.
select table_name from user_tables;

I was looking to get a list of all columns names belonging to a table of a schema sorted by the order of column id.
Here's the query I am using: -
SELECT COLUMN_NAME
FROM ALL_TAB_COLUMNS
WHERE OWNER = 'schema_owner_username' AND TABLE_NAME='table_name'
ORDER BY COLUMN_ID ASC;

Indeed, it is possible to have the list of tables via SQL queries.it is possible to do that also via tools that allow the generation of data dictionaries, such as ERWIN, Toad Data Modeler or ERBuilder. With these tools, in addition to table names, you will have fields, their types, objects like(triggers, sequences, domain, views...)
Below steps to follow to generate your tables definition:
You have to reverse engineer your database
In Toad data modeler: Menu -> File -> reverse engineer -> reverse engineering wizard
In ERBuilder data modeler: Menu -> File -> reverse engineer
Your database will be displayed in the software as an Entity Relationship diagram.
Generate your data dictionary that will contain your Tables definition
In Toad data modeler: Menu -> Model -> Generate report -> Run
In ERBuilder data modeler: Menu -> Tool -> generate model documentation

To get all the table names, we can use:
Select owner, table_name from all_tables;
if you have dba permission, you can use:
Select owner, table_name from dba_tables;

select * from all_all_tables
this additional 'all' at the beginning gives extra 3 columns which are:
OBJECT_ID_TYPE
TABLE_TYPE_OWNER
TABLE_TYPE

Tables in the current user - logon schema
select * from tabs;

Related

Get DDL command from oracle database objects without using dbms_metadata

I am doing a procedure in the oracle database that has the function of performing a kind of inventory of the objects of the database.
Basically I must get the DDL of objects of type table.
For this, I am using queries from the bank itself as:
select * from user_objects;
select * from user_constraints;
select * from user_source;
My inventory must contain the following information:
Inventory information here.
How do I get the DDL command from objects without using the function:
dbms_metadata.get_ddl();
and no other ready functions from the metadata library.
I have also tried this:
SELECT
(CASE WHEN line = 1 THEN 'create or replace ' || text ELSE text END) texto
FROM user_source
WHERE NAME = '....'
ORDER BY line
but this command does not get the ddl of table objects.
For getting the DDL of views, it's very easy:
SELECT VIEW_NAME, TEXT FROM ALL_VIEWS;
If you want it to return just the text of a particular view, then do:
SELECT TEXT FROM ALL_VIEWS
WHERE VIEW_NAME LIKE '[name_of_view]';
Getting the DDL for tables is more cumbersome, but can be done by querying the data from several system views:
ALL_TABLES ALL_TAB_COLUMNS ALL_COL_COMMENTS
ALL_CONSTRAINTS ALL_CONS_COLUMNS ALL_INDEXES
ALL_IND_COMMENTS
For example, if you wanted to get all column names and their data types for TABLE1, you would do:
SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME LIKE 'TABLE1';
To get a list of all constraints on a table, the query is:
SELECT * FROM ALL_CONSTRAINTS
WHERE TABLE_NAME LIKE 'TABLE1';
To get a full table definition takes a fairly good understanding of how to use these system views. A very helpful page for this can be found here: 6 Useful Oracle Data Dictionary Queries Every DBA Should Have

Oracle: get all table names which i had created tables [duplicate]

I am new to Oracle and want to find all tables created by user 'john' .
I connect to Oracle database via command line by the following command:
sqlplus john/passwd
How do i list all the tables created by a given user e.g. john?
This will get all the tables where the "JOHN" user is the owner:
SELECT * FROM USER_TABLES;
or
SELECT * FROM ALL_TABLES WHERE OWNER = 'JOHN';
([TL;DR] 'JOHN' typically needs to be in upper-case. Assuming that the user john was created using the CREATE USER john ... statement then Oracle's default behaviour is to convert all object names (i.e. tables, columns, users, etc) to upper case. When you query the data-dictionary the table details will be stored in this case (and not the case you used in the original command unless you wrap it in double quotes).)
To list the table you can use
SELECT * FROM ALL_TABLES WHERE OWNER = 'JOHN';
TO see the size of the schema you can use
SELECT sum(bytes)
FROM dba_segments
WHERE owner = 'JOHN'
Since you are logged in as the schema owner, you can also use
SELECT SUM(bytes)
FROM user_segments
You can use also
select * from
USER_TABLES;
anyway you can find all the data dictionary explain here https://docs.oracle.com/cd/B28359_01/server.111/b28310/tables014.htm
select * from
USER_TABLES;
The above code will show all the information of the tables under the user which is currently connected. This might clutter your SQL terminal.
To Specifically see only the table names under a user you should use the following code
select table_name
from USER_TABLES;
Try:
select *
from all_tables
where owner = 'jhon';

List all tables of a given user in Oracle

I am new to Oracle and want to find all tables created by user 'john' .
I connect to Oracle database via command line by the following command:
sqlplus john/passwd
How do i list all the tables created by a given user e.g. john?
This will get all the tables where the "JOHN" user is the owner:
SELECT * FROM USER_TABLES;
or
SELECT * FROM ALL_TABLES WHERE OWNER = 'JOHN';
([TL;DR] 'JOHN' typically needs to be in upper-case. Assuming that the user john was created using the CREATE USER john ... statement then Oracle's default behaviour is to convert all object names (i.e. tables, columns, users, etc) to upper case. When you query the data-dictionary the table details will be stored in this case (and not the case you used in the original command unless you wrap it in double quotes).)
To list the table you can use
SELECT * FROM ALL_TABLES WHERE OWNER = 'JOHN';
TO see the size of the schema you can use
SELECT sum(bytes)
FROM dba_segments
WHERE owner = 'JOHN'
Since you are logged in as the schema owner, you can also use
SELECT SUM(bytes)
FROM user_segments
You can use also
select * from
USER_TABLES;
anyway you can find all the data dictionary explain here https://docs.oracle.com/cd/B28359_01/server.111/b28310/tables014.htm
select * from
USER_TABLES;
The above code will show all the information of the tables under the user which is currently connected. This might clutter your SQL terminal.
To Specifically see only the table names under a user you should use the following code
select table_name
from USER_TABLES;
Try:
select *
from all_tables
where owner = 'jhon';

Unable to query USER_TABLES from a different schema

I have two schemas TEST and DEV. I am working in the TEST schema and I want to get the list of tables starting with SDB in the DEV schema. So I have used the below query:
SELECT TABLE_NAME FROM DEV.USER_TABLES WHERE UPPER(TABLE_NAME) LIKE 'SDB%';
but I am getting an ORA-00942 error. Is this an issue with grants? Or is there any other method by which I can get the list of tables in the DEV schema.
You need this:
SELECT TABLE_NAME FROM all_tables where OWNER = 'DEV' and UPPER(TABLE_NAME) LIKE 'SDB%';
all_tables contains all the table that are on the db that you have access to, so that the table you should select from.
The ora you got is because this isn't a specific user table, you can't select from dev.ORACLE TABLES , when you select from there, don't specify a schema
USER_TABLES is a SYS view which only contains objects you own. It doesn't exist as part of each user's schema (so there is no DEV.USER_TABLES), and you cannot see another user's objects in it. Use ALL_TABLES instead, specifying the owner:
SELECT TABLE_NAME FROM ALL_TABLES
WHERE OWNER = 'DEV'
AND UPPER(TABLE_NAME) LIKE 'SDB%';
You will only be able to see the table in the other schema if you have privileges on it. If you don't then it won't be listed; in that case you can query DBA_TABLES if you have permission to see that.

search the history for the all tables that are created by a user

i'm new in oracle/sql world., and i know how to find a answer by myself searching., and searching again .. but for my new issue i d'ont find a good answer, so i just want to find the history of manipulation the database by filtering the user who created the last tables, what tables are, when he created it etc .. is sql oracle
I'm using oracle XE and the client is toad.
i try it with
select table_name from sys.dba_tables where owner='system'
but is not display the tables that i was created with the system user a few months ago., and now i forget what tables i was created.
String matching is case-sensitive, and most things in the data dictionary are stored in all upper-case as a general rule. So your example query should return some rows if you change the literal to upper case:
select table_name from sys.dba_tables where owner='SYSTEM'
If you want to see recently-created tables, you'll need to join this with dba_objects and use the created column there to filter or sort.
Of course, if you really just want to see tables for the schema you are currently logged into, user_tables is a better view to query.
Per your comment, here's how to get the last-modified time for each table:
select table_name, last_ddl_time
from dba_tables t
join dba_objects o
on o.object_name=t.table_name and o.object_type='TABLE' and o.owner = t.owner
where t.owner='SYSTEM'
and last_ddl_time >= date '2011-01-02'
and last_ddl_time < date '2011-01-10'
(Note that "modified" means a change to the table definition, not to the data it contains.)
Try one of the following:
select * from tab;
or
select * from user_tables;
Both will give the list of tables created by user (you).