How to specify tablespace_name in SQLPlus Oracle select - oracle

My setup looks like this
SQL> SELECT tablespace_name, table_name
FROM all_tables
WHERE tablespace_name = 'MYSPACE';
TABLESPACE_NAME TABLE_NAME
-------------------------- ------------------------------
MYSPACE MYTABLENAME
MYSPACE MYOTHERTABLENAME
Now I'd like to SELECT * FROM MYSPACE.MYTABLENAME; but that's apparently not how you do it.
ERROR at line 1:
ORA-00942: table or view does not exist
My expected result would be to get all records from that table. Like I would if it was MySQL.
Thanks

You are selecting from tablespace, which is not the same as your Owner/Schema name. Thats why. For example the tablespace SYSTEM has owner SYS. You do select from Sys.xxx;
Ok.
SELECT owner, tablespace_name, table_name
FROM all_tables
WHERE tablespace_name = 'MYSPACE';
And then
select * from [ owner ].[ table_name ];
(worth to mention: select .. from dba_tables / user_tables)

Tables are not owned by a tablespace. A tablespace is a logical storage storage structure. When you create a table (or index) you specify which tablespace its contents should be stored in. Unless you're doing DBA tasks, you don't really need to know which tablespace(s) your data is stored in.
You may just be confusing terms. Tables are owned by schemas. If you query the owner column instead of tablespace_name you might see something like:
SQL> SELECT owner, table_name
FROM all_tables
WHERE tablespace_name = 'MYSPACE';
OWNER TABLE_NAME
-------------------------- ------------------------------
MYUSER MYTABLENAME
MYUSER MYOTHERTABLENAME
And you can then query from that with
SELECT * FROM MYOWNER.MYTABLENAME;
Of course, if the owner is actually you anyway, then you don't need to prefix the table name with the schema; you can just select FROM MYTABLENAME. (You may also have synonyms or session settings that make the schema prefix unnecessary, but that's getting a a bit off-topic). And if you own the table you'll see it in USER_TABLES as well.

Related

Unable to view tablespace name for a partitioned table

I am unable to get the tablespace information for a partitioned table.
I have tried querying tablespace_name from dba_tables but the value was null. Let me know the reason to this and also from where can I view the tablespace_name for a partitioned table.
select tablespace_name
from dba_tables
where table_name = 'XXXXX';
The result was null.
Please let me know how can I get the tablespace information coz I am trying to move the partitions to a new tablespace.Also let me know the impact of it.
The reason is that you could have each partition in a different tablespace, so the information at a table level does not exist and you need to check the partitions.
Oracle doc for this
You may need DBA_TAB_PARTITIONS
select tablespace_name
from dba_tab_partitions
where table_name='...'
and table_owner = '...'

ora-00942 even i create table in my schema

I have created new user and granted (database administration) privilege to him.
Then I connected to this new user.
When I create a new table in this user then try to select from this table it gives me
oracle-00942: table/view does not exist
When I try to find it through toad from schema browser I cannot find it. I searched for it and found this table in (sys) user.
When I create table with schema name I found it in schema browser but also I cannot select from it.
So what is the wrong with this?
It would help if you would show the sequence of steps you used to create the table. My guess is somehow you're creating the table under the the wrong schema (owner). Do this to find out where it is:
select owner, table_name from all_tables where table_name = 'MYTABLE'
If connected as SYS, you can use dba_tables instead of all_tables.
It would be better if you actually showed us what you did and how Oracle responded (i.e. copy/pasted the whole SQL*Plus session).
As you can create users, you probably can connect as SYS. Do so, and then run such a statement:
SQL> select owner, object_type
2 from dba_objects
3 where object_name = 'EMP';
OWNER OBJECT_TYPE
------------------------------ -------------------
SCOTT TABLE
SQL>
It will show who that table really belongs to.
I'm going to simulate what you did (actually, what I understood you did). You'll see that - if you do it right - everything is OK.
Connect as SYS and create new user:
SQL> show user
USER is "SYS"
SQL> create user utest identified by utest
2 default tablespace users
3 temporary tablespace temp
4 quota unlimited on users;
User created.
SQL> grant dba to utest;
Grant succeeded.
Connect as newly created user, create a table:
SQL> connect utest/utest
Connected.
SQL> create table test (id number);
Table created.
SQL> select * from test;
no rows selected
OK; the table is empty, but - no ORA-00942 error.
Back to SYS, to check who owns the TEMP table:
SQL> connect sys as sysdba
Enter password:
Connected.
SQL> select owner, object_type
2 from dba_objects
3 where object_name = 'TEST'
4 order by owner;
OWNER OBJECT_TYPE
------------------------------ -------------------
SCOTT TABLE
UTEST TABLE
SQL>
Now, it is your turn.
I searched for it and found this table in (sys) user.
It sounds like you have connected to the database using SYSDBA privilege, something like this:
connect your_user/password as sysdba
When we do this Oracle ignores the passed username and connects us as SYS. Consequently, any actions we take are executed as SYS. Which means any tables we create are created in the SYS schema, unless we prefix them with a specific schema name.

How to identify all global temporary tables

I need to identify which tables in my schema are Global Temporary Tables.
Following script returns names of all my tables, but I am not able to identify which of these are GTTs and which are not.
SELECT OBJECT_NAME
FROM ALL_OBJECTS
WHERE OBJECT_TYPE IN ('TABLE')
AND OWNER='owner_name';
Thank you!
You can use ALL_TABLES
select table_name
from all_tables
where TEMPORARY = 'Y'
AND OWNER='owner_name';
Temporary column indicates whether the table is temporary (Y) or not (N)

Oracle - Move all tables and reclaim free space

I have this query:
select file_id, block_id first_block, block_id+blocks-1 last_block,
segment_name
from dba_extents
where tablespace_name = 'USERS'
union all
select file_id, block_id, block_id+blocks-1, 'free'
from dba_free_space
where tablespace_name = 'USERS'
order by file_id, first_block DESC;
It shows lot of "free" segments in between. There are lot of tables which are coming in between.
I move the tables using:
Alter table table_name move;
I have 2000 such tables. Is there a way on how I can move it altogether so I can reclaim all the free space from the tablespace?
To achieve your goal you have to move all objects in the tablespace, not just tables.
At least you have to move tables and then rebuild all their indexes because when you move a table all indexes built on this table are invalidated.
You can not move all tables all together but you can obtain all commands in the following way:
select 'alter table ' ||table_name ||' move;'
from dba_tables where tablespace_name = 'YOURTABLESPACENAME';
To rebuild indexes:
select 'alter index ' ||index_name ||' rebuild;'
from dba_indexes where tablespace_name = 'YOURTABLESPACENAME' and status <>'VALID';
Be careful: this procedure is not complete, you can have indexes of different kind.
Note: to obtain best results you should move objects in a different tablespace.
A simpler approach can be the one explained here: this article describe how to use the shrink command against Oracle database tables to reclaim wated space.
If you want a detailed procedure give me the result of the following query:
select distinct segment_type from dba_segments where tablespace_name='YOURTABLESPACENAME';

How do I view the owner of a Vertica table?

I have a table in Vertica that I can't drop, because I'm not the owner. How do I see who the owner of the table is?
If you're not the owner of the table, or do not have have privileges to see the table, then you most likely won't be able to see who the owner is:
SELECT table_schema, table_name, owner_name FROM v_catalog.tables;
Documentation

Resources