How to list the user created table name in sql? - sql

I created like 20 tables in sql 11g and lost the record of them. Is there any way I can list the table names I created.
SELECT table_name FROM user_tables is not the solution.

First, log on as sys before you can view the tables in the entire database.
Second, run this query using the sys
SELECT owner, table_name FROM dba_tables WHERE owner='HR';
This should display all tables owned by the HR schema. Remember to use uppercase for the owner (HR) else you will receive error.
Hope this helps.

The Best approach to solve this issue is considering the attribute of ALL_ALL_TABLES named
LAST_ANALYZED
It gives Date on which the table was most recently analyzed.
So you could easily query the database with the help of DATE Functions.

The Oracle Database Reference helps you.
SELECT TABLE_NAME FROM USER_ALL_TABLES

Well, if user_all_tables is not your desired result, you will have to rely on dba_tables via:
SELECT owner, table_name
FROM dba_tables
But for this you need more privs than for user_tables obviously

Related

Get the schema name for specific table in SQL/Oracle

I am trying to get the schema names for below table on Oracle. Could you please help me to understand whether below query is checking for all the schemas or not.
select * from all_tables
where table_name like '%ELEC_SURROGATE_KEY';
Hoping for some help here.
Thanks!
all_tables will show you all tables granted (directly or via role) to the user executing that query.
So, it is possible that there are tables matching like '%ELEC_SURROGATE_KEY', that does not show up in your query.
In SQL:
select TABLE_SCHEMA
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE'%YourTableName%'

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';

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).

How does one cheaply validate the existance of a column in a table in another schema with Oracle?

The environment is Oracle 9 & 10. I do not have DBA level access.
The problem is to verify that a specific column exists in a specific table, in another schema.
There are two cases to deal with.
Another schema in the same instance
A schema in a different instance, using a db_link
Given my schema FRED and another schema BARNEY, I tried something like this
SELECT 1
FROM BARNEY.USER_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE'
AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
Which yielded [1]: (Error): ORA-00942: table or view does not exist
After vegging on this awhile, I realized that USER_TAB_COLS, is not really a table. It is a view. I have been selecting from tables all along, but not from a view.
I tried the same thing with my db_link, and was surprised to see data come back. A db_link has an embedded schema_name/password in it, so it seems reasonable to me that it worked, as it effectively logs in to the other schema, which should make the views reachable.
Having Googled around, and worn out my eyeballs on on the mountain of Oracle doc,
I am looking for someone to point me in the correct direction, or at least point out what I am missing.
What techniques are available for getting user table related metadata from a schema in the same instance in order to validate that a specific column exists?
Thanks in advance.
Evil.
+1 for good answers.
Thank you.
You can use the following query:
SELECT 1
FROM ALL_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE'
AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
AND OWNER = 'BARNEY';
(User_Tables and User_Tab_Cols are just views on all_tables and all_tab_coumns with a where owner = <Current User> attached to it)
If you're allowed to see the Barney's some_table (i.e. you have been GRANTed at least SELECT privileges on it), then you'll know if the column is there. If you have no rights on the table, you won't be able to get meta information on it.
As with the other replies, normally I use ALL_TAB_COLUMNS for a query like this. But that will only show columns in tables where you have SELECT. And it's select on that column -- in the unlikely event that they've implemented column-level privileges for that table, you may be able to see the table, but not see the specific column of interest. For most of us, that's extremely rare.
DBA_TAB_COLUMNS will show all columns, but you'll need select on it granted to your schema by your DBA. (Actually, you'll need a grant on ALL_TAB_COLUMNS to use it, but that's common in most shops). The DBMS_METADATA PL/SQL Built-in package can also be used, with similar limitations, but I think you'll find it more complicated.
Of course, you can also just try to select a record from barney.some_table.some_column#my_dblink (or whatever pieces of that you're interested in). And then handle the exception. Ugly, I wouldn't recommend it in most situations.
You would use all_tab_columns for that.
But beware that you'll only see what you are allowed to see.
Same instance, different schema:
Select Count(*)
From all_tab_cols
Where owner = 'BARNEY' and
table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';
The count(*) has the advantage of always returning a single row with a value of either 1 or 0, so you do not have to deal with NO_DATA_FOUND errors in PL/SQL.
Across a DB Link, same schema as the one you connect as:
Select Count(*)
From user_tab_cols#MY_DB_LINK
Where table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';
Across a DB Link, different schema than the one you connect as:
Select Count(*)
From all_tab_cols#MY_DB_LINK
Where owner = 'BARNEY' and
table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';