Is there a need to create a tablespace in oracle - oracle

Is it mandatory to create a tablespace in oracle? I do know that it will take a users tablespace by default. What difference does it make by specifying a tablespace and without one? Now i have created a schema without tablespace and be defualt it is in users tablespace , what are the steps to take to move my objects and datas from default to one custom tablespace.Please suggest

You can simply rename USERS tablespace if you dislike its name.
alter tablespace USERS rename to COOLNAME;
If you want also to rename files then you need to offline tablespace and rename them.
To know the names you can select from dba_data_files or v$datafile
alter tablespace COOLNAME OFFLINE;
move file at OS level..
host mv /oracle/users01.dbf /oracle/coolname01.dbf
alter database rename file '/oracle/users01.dbf' to '/oracle/coolname01.dbf';
alter tablespace COOLNAME online;
If you need to move objects to another tablespace you should use move and rebuild DDL. Here is some useful queries to generate DDLs.
select 'alter table ' || OWNER || '.' || SEGMENT_NAME || ' move tablespace COOLNAME;' from dba_segments where OWNER='YOURUSER' and SEGMENT_TYPE='TABLE' and TABLESPACE_NAME='USERS';
select 'alter index ' || OWNER || '.' || SEGMENT_NAME || ' rebuild tablespace COOLNAME;' from dba_segments where OWNER='YOURUSER' and SEGMENT_TYPE='INDEX' and TABLESPACE_NAME='USERS';

Related

How to change the default tablespace of the schema in Oracle

There is a schema called Docker, in which there are tables named TABLE2, TABLE3.
Example
SELECT * FROM all_all_tables WHERE TABLE_name= 'TABLE3';
Also, the Docker schema belongs to the DEFAULT TABLESPACE SYSTEM tablespace.
select
username
, default_tablespace from dba_users WHERE USERNAME = 'DOCKER';
The following syntax is used to change the default tablespace of the schema. (TS3 tablespace already exists)
ALTER USER docker DEFAULT tablespace TS3;
Then, when I searched again, I found that the DEFAULT TABLESPACE was changed.
select
username
, default_tablespace from dba_users WHERE USERNAME = 'DOCKER';
And, of course, I thought that the tablespace in which TABLE2 and TABLE3 were designated would also have been changed to TS3, and the following statement was executed.
However, the tablespace of the table was SYSTEM, not TS3. I am curious about why it hasn't changed, and I want to know how.
SELECT * FROM all_all_tables WHERE TABLE_name= 'TABLE3';
The default tablespace is just that-- a default for when you create a segment (normally a table or an index but a segment could be a partition of a table, a materialized view, or anything else that requires space) and don't specify the tablespace. Once you create a segment and it is assigned to a particular tablespace, it will remain in that tablespace unless you move it.
Assuming you are on 12.2 or later so that online moves are an option (in other versions you'd need to remove the online keyword)
alter table table3
move online tablespace ts3;
You'd need to do that for each table. If there are also indexes in the system tablespace, you'd want to move those as well
alter index index_name
rebuild online tablespace ts3;
Depending on the number of tables and indexes involved, you may want to write a bit of dynamic SQL to generate the various alter table and alter index statements for you.

How to find TEMP tablespace usage?

we are using v$sort_segment to calculate temp file usage. but one of my temp tablespace not available in v$sort_segment table. IS there any other way to get the correct usage which returns the same value as in v$sort_segment?
V$SORT_SEGMENT only gets populated as a temporary segment is required. For example, if I create a second temp tablespace, then it does not appear because it is not in use
SQL> create temporary tablespace temp2 tempfile 'X:\ORACLE\ORADATA\DB19\PDB1\TEMP02.DBF' size 20m;
Tablespace created.
SQL> select tablespace_name from v$sort_segment;
TABLESPACE_NAME
------------------------
TEMP
If you want a view of all of your temporary tablespaces, you can outer join either DBA_TABLESPACES (type=TEMPORARY) or DBA_TEMP_FILES to V$SORT_SEGMENT.

Run generated scripts against tablespaces_Oracle

The homework asks to re-org a fragmented tablespace. Instead of manually copy and drop each table between two tablespaces, the homework requires to write a script that will generate a script file which automatically runs against the tablespace and reconstruct it.
How to generate a script that runs automatically against thousands of tables?
you can use following query :
select 'alter table '||table_name ||' move tablespace NewTableSpace ' from dba_tables where TABLESPACE_NAME='current tablespace name';
if your table's owner is special user then use where clouse :
where owner = 'the owner name'
if you
remember that :first you need to create new tablespace with name :NewTableSpace
be carefull about the space of new tablespace(your new tablespace have to had enough space.

how to cleanup the temp tablespace in oracle 10g server , Please provide the steps for linux plateform

I want to clean up the Temp table space which have datafiles
temp01.dbf and temp02.dbf, so please suggest me should I drop
temp01.dbf file or drop the temp tablespace. Datafiles of Temp tablespaces is given below
33G temp01.dbf
1.5G temp02.dbf
Create Temporary Tablespace Temp
CREATE TEMPORARY TABLESPACE TEMP2 TEMPFILE ‘/u01/app/oradata/temp01.dbf′ SIZE 2000M ;
Move Default Database temp tablespace
ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp2;
Make sure No sessions are using your Old Temp tablespace
a. Find Session Number from V$SORT_USAGE:
SELECT USERNAME, SESSION_NUM, SESSION_ADDR FROM V$SORT_USAGE;
b. Find Session ID from V$SESSION:
If the resultset contains any rows then your next step will be to find the SID from the V$SESSION view. You can find session id by using SESSION_NUM or SESSION_ADDR from previous resultset.
SELECT SID, SERIAL#, STATUS FROM V$SESSION WHERE SERIAL#=SESSION_NUM;
OR
SELECT SID, SERIAL#, STATUS FROM V$SESSION WHERE SADDR=SESSION_ADDR;
c. Kill Session:
Now kill the session with IMMEDIATE.
ALTER SYSTEM KILL 'SID,SERIAL#' IMMEDIATE;
Drop temp tablespace
DROP TABLESPACE temp INCLUDING CONTENTS AND DATAFILES;
Recreate Tablespace Temp
CREATE TEMPORARY TABLESPACE TEMP TEMPFILE '/u01/app/temp/temp01.dbf′ SIZE 2000M;
6 Move Tablespace Temp, back to new temp tablespace
ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp;
Drop temporary for tablespace temp
DROP TABLESPACE temp2 INCLUDING CONTENTS AND DATAFILES;
Thanks #anudeepks! Just wanna point something, in step 5 the sentence should be as follows:
CREATE TEMPORARY TABLESPACE TEMP TEMPFILE '/u01/app/temp/temp.dbf′ SIZE 2000M;
(Note that the filename has changed from temp01.dbf to temp.dbf). Otherwise we get an error because the file already exists, and also such file will be deleted on step 7.

How to move table from one tablespace to another in oracle 11g

I run oracle 11g and need to move table (tbl1) from one tablespace (tblspc1) to another (tblspc2). What is the easiest way to do that?
Try this:-
ALTER TABLE <TABLE NAME to be moved> MOVE TABLESPACE <destination TABLESPACE NAME>
Very nice suggestion from IVAN in comments so thought to add in my answer
Note: this will invalidate all table's indexes. So this command is usually followed by
alter index <owner>."<index_name>" rebuild;
Use sql from sql:
spool output of this to a file:
select 'alter index '||owner||'.'||index_name||' rebuild tablespace TO_TABLESPACE_NAME;' from all_indexes where owner='OWNERNAME';
spoolfile will have something like this:
alter index OWNER.PK_INDEX rebuild tablespace CORRECT_TS_NAME;
Moving tables:
First run:
SELECT 'ALTER TABLE <schema_name>.' || OBJECT_NAME ||' MOVE TABLESPACE '||' <tablespace_name>; '
FROM ALL_OBJECTS
WHERE OWNER = '<schema_name>'
AND OBJECT_TYPE = 'TABLE' <> '<TABLESPACE_NAME>';
-- Or suggested in the comments (did not test it myself)
SELECT 'ALTER TABLE <SCHEMA>.' || TABLE_NAME ||' MOVE TABLESPACE '||' TABLESPACE_NAME>; '
FROM dba_tables
WHERE OWNER = '<SCHEMA>'
AND TABLESPACE_NAME <> '<TABLESPACE_NAME>
Where <schema_name> is the name of the user.
And <tablespace_name> is the destination tablespace.
As a result you get lines like:
ALTER TABLE SCOT.PARTS MOVE TABLESPACE USERS;
Paste the results in a script or in a oracle sql developer like application and run it.
Moving indexes:
First run:
SELECT 'ALTER INDEX <schema_name>.'||INDEX_NAME||' REBUILD TABLESPACE <tablespace_name>;'
FROM ALL_INDEXES
WHERE OWNER = '<schema_name>'
AND TABLESPACE_NAME NOT LIKE '<tablespace_name>';
The last line in this code could save you a lot of time because it filters out the indexes which are already in the correct tablespace.
As a result you should get something like:
ALTER INDEX SCOT.PARTS_NO_PK REBUILD TABLESPACE USERS;
Paste the results in a script or in a oracle sql developer like application and run it.
Last but not least, moving LOBs:
First run:
SELECT 'ALTER TABLE <schema_name>.'||LOWER(TABLE_NAME)||' MOVE LOB('||LOWER(COLUMN_NAME)||') STORE AS (TABLESPACE <table_space>);'
FROM DBA_TAB_COLS
WHERE OWNER = '<schema_name>' AND DATA_TYPE like '%LOB%';
This moves the LOB objects to the other tablespace.
As a result you should get something like:
ALTER TABLE SCOT.bin$6t926o3phqjgqkjabaetqg==$0 MOVE LOB(calendar) STORE AS (TABLESPACE USERS);
Paste the results in a script or in a oracle sql developer like application and run it.
O and there is one more thing:
For some reason I wasn't able to move 'DOMAIN' type indexes. As a work around I dropped the index. changed the default tablespace of the user into de desired tablespace. and then recreate the index again.
There is propably a better way but it worked for me.
Try this to move your table (tbl1) to tablespace (tblspc2).
alter table tb11 move tablespace tblspc2;
I tried many scripts but they didn't work for all objects. You can't move clustered objects from one tablespace to another. For that you will have to use expdp, so I will suggest expdp is the best option to move all objects to a different tablespace.
Below is the command:
nohup expdp \"/ as sysdba\" DIRECTORY=test_dir DUMPFILE=users.dmp LOGFILE=users.log TABLESPACES=USERS &
You can check this link for details.

Resources