Choosing table from the right owner in Oracle SQL - sql

I am quite new to SQL, and my first "job" is to get something out of an Oracle SQL database.
Just to see what's actually found in my connection I use the following:
SELECT owner, table_name FROM dba_tables
This gives me a bunch of tuples with an owner name and table_name. However, some table names are the same for different owners.
So when I run a command like:
SELECT * FROM MyTableName
How do I ensure that this table is coming from owner1 and not owner2, where both of them actually have a table called MyTableName ?

You can do:
SELECT * FROM <owner>.MyTableName

You can specify exactly which table with
select * from some_owner.my_table;
If you do this:
select * from my_table;
You will be selecting from the version of MY_TABLE that belongs to the owner/user with which you you are connected to the database. So, if you connected as user SCOTT, then
select * from my_table;
effectively becomes
select * from scott.my_table;
Note that this behavior can be overridden by the use of synonyms. Suppose user SCOTT has a synonym like this:
create synonym scott.my_table for fred.my_table;
Then SCOTT issues
select * from my_table;
Then oracle will check first for a synonym, and finding it will effectively transform the query to
select * from fred.my_table;
So in the end, the precedence is this. When you reference a table without qualifying it with the owner, oracle will look for it in this sequence:
is there a user synonym by that name? if so, use what it references if the user has been granted necessary privileges.
is there a global synonym by that name? if so, use what it references if the user has been granted necessary privileges.
does the user itself own a table by that name? if so, use that table.
return error "ORA-00942: table or view does not exist"

Related

Error says table doesn't exist in database, while it exists

I have extracted list of names of tables in database using this sql query:
"SELECT table_name from information_schema.tables"
I got this list:
table_name
1 main_table
2 kp_table
3 ids_table
4 main_logs
Then i want to extract table ids_table:
"SELECT * from ids_table"
So desired result is to get that table, but i get this error:
Error: Failed to prepare query: ERROR: relation "ids_table" does not exist
LINE 1: SELECT * from ids_table
Why it happens? Why i get from first query its name, but then it tells me that it doesn't exist?
In this case, I would say that the most common issue is that you are looking at two different databases. That is probably not the cause in this case.
Another common possibility is that the schema is something unexpected, and you should be referencing the schema. For that, include the schema in the query:
select table_schema, table_name
from information_schema.tables
Another possibility are hidden characters, such as spaces. You can see if this is the problem by adding delimiters so check the names:
select '|' || table_name || '|'
from information_schema.tables
It looks like you are trying to select data from different schema, which by default owns by different user, but you have access to it. As this table is not created by user, which you are using for select * from ... , you must indicate schemas name before table name. Without schemas name it's trying to select date from users schema, but actually table are owned by completely different user.

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

How to find the name of a table based upon a column name and then access said table

I have a column name "CustomerIDClass" and I need to find the table it's associated with within an entire Oracle database.
I've run this to determine the owner and name of the table where this column name appears:
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CustomerIDClass%';
and I'm getting this response:
I don't have enough reputation to post the image, so here's the link: http://i.imgur.com/a7rcKoA.png
I have no idea how to access this (BIN$Csew==) table. When I try to use it as a table name I get errors or messages saying that no rows were returned.
My main goal here is to write a simple statement that lets me search the database for the "CustomerIDClass" and view the table that contains this column name.
This table is in the recycle bin. You have to issue FLASHBACK TABLE "Customer1"."BIN$Csew==$0" TO BEFORE DROP command, given you have the appropriate privileges.
Doc: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9012.htm
Do note that in oracle the column names are stored in capital but you are using mixed case in your like statement therefore the select clause will not return any result
Try the below
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CUSTOMERIDCLASS%';

How to find schema name in Oracle ? when you are connected in sql session using read only user

I am connected to a oracle database with a read only user and i used service name while Setting up connection in sql developer hence i dont know SID ( schema ).
How can i find out schema name which i am connected to ?
I am looking for this because i want to generate ER diagram and in that process at one step it asks to select schema. When i tried to select my user name , i dint get any tables as i guess all tables are mapped with schema user.
Edit: I got my answer partially by the below sql Frank provided in comment , it gave me owner name which is schema in my case. But I am not sure if it is generic solution applicable for all cases.
select owner, table_name from all_tables.
Edit: I think above sql is correct solution in all cases because schema is owner of all db objects. So either i get schema or owner both are same. Earlier my understanding about schema was not correct and i gone through another question and found schema is also a user.
Frank/a_horse_with_no_name Put this in answer so that i can accept it.
Call SYS_CONTEXT to get the current schema. From Ask Tom "How to get current schema:
select sys_context( 'userenv', 'current_schema' ) from dual;
To create a read-only user, you have to setup a different user than the one owning the tables you want to access.
If you just create the user and grant SELECT permission to the read-only user, you'll need to prepend the schema name to each table name. To avoid this, you have basically two options:
Set the current schema in your session:
ALTER SESSION SET CURRENT_SCHEMA=XYZ
Create synonyms for all tables:
CREATE SYNONYM READER_USER.TABLE1 FOR XYZ.TABLE1
So if you haven't been told the name of the owner schema, you basically have three options. The last one should always work:
Query the current schema setting:
SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') FROM DUAL
List your synonyms:
SELECT * FROM ALL_SYNONYMS WHERE OWNER = USER
Investigate all tables (with the exception of the some well-known standard schemas):
SELECT * FROM ALL_TABLES WHERE OWNER NOT IN ('SYS', 'SYSTEM', 'CTXSYS', 'MDSYS');
How about the following 3 statements?
-- change to your schema
ALTER SESSION SET CURRENT_SCHEMA=yourSchemaName;
-- check current schema
SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') FROM DUAL;
-- generate drop table statements
SELECT 'drop table ', table_name, 'cascade constraints;' FROM ALL_TABLES WHERE OWNER = 'yourSchemaName';
COPY the RESULT and PASTE and RUN.