100% found this document useful (1 vote)
226 views5 pages

Tablespace Management Oracle

The document discusses various commands and queries for managing Oracle tablespaces and datafiles. This includes commands for creating, altering, dropping, and getting status of tablespaces, as well as adding, resizing, dropping and getting status of datafiles. Queries are also provided to view space usage, files, and tables associated with tablespaces.

Uploaded by

MaheshSai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
226 views5 pages

Tablespace Management Oracle

The document discusses various commands and queries for managing Oracle tablespaces and datafiles. This includes commands for creating, altering, dropping, and getting status of tablespaces, as well as adding, resizing, dropping and getting status of datafiles. Queries are also provided to view space usage, files, and tables associated with tablespaces.

Uploaded by

MaheshSai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

tablespace management

create tablespace ---- create tablespace maheshTS01 datafile


'/data/maheshTS01_01.dbf' size 2G;

change default tablespace to already existing user ---- alter user mahesh
identified by mahesh123 default tablespace mahesh01;

add datafile to tablesapce ---- alter tablespace TABLESPACENAME add datafile


'/path/datafilename.dbf' size 2g;

resize datafile ---- alter database datafile '/path/DATAFILENAME.dbf' resize 2g;

create tablespace with auto extent ---- create tablespace TABLESPACENAME datafile
'/path/DATAFILENAME.dbf' size 2g AUTOEXTEND ON MAXSIZE UNLIMITED;

create bigfile tablespace ---- create bigfile tablespace TABLESPACENAME datafile


'/path/DATAFILENAME.dbf' size 2g

create undo tablespace ---- create undo tablesapce TABLESPACENAME datafile


'/path/DATAFILENAME.dbf' size 2g

tablespace offline ---- alter tablespace TABLESPACENAME offline;

tablespace offline immediate ---- alter tablespace TABLESPACENAME offline immediate

tablespace online ---- alter tablespace TABLESPACENAME online;

tablespace read-only ---- alter tablespace TABLESPACENAME read only;

datafile offline ---- alter database datafile '/path/DATAFILENAME.dbf' offline;

datafile online ---- alter database datafile '/path/DATAFILENAME.dbf' online;

datafile drop ---- alter database datafile '/path/DATAFILENAME.dbf' drop;

datafile offline drop ---- alter database datafile '/path/DATAFILENAME.dbf' offline


drop;

drop tablespace ---- drop tablespace TABLESPACENAME;

drop tablespace including contents and datafiles ---- drop tablespace


TABLESPACENAME including contents and datafile;

drop datafile ---- alter tablespace TABLESPACENAME drop datafile


'/path/DATAFILENAME.dbf';

rename tablespace ---- alter tablespace TABLESPACENAME rename to NEWTABLESPACENAME;

how to check how many users have same tablespace ---- select
USERNAME,ACCOUNT_STATUS,DEFAULT_TABLESPACE from dba_users where
DEFAULT_TABLESPACE='MAHESH01';

find tables in user ---- select * from tab;

insert values in table ---- insert into TABLENAME values (col1,col2......);

permissions to write data in tablesapce ---- alter user USERNAME quota unlimited on
TABLESPACENAME;
view all tables in user ---- select * from tab;

view all columns in table ---- desc TABLENAME;

view content in tables ---- select * from TABLENAME;

view list of datafiles in particular tablespace ---- select file_name,


tablespace_name from dba_data_files;

view list of datafiles in particular tablespace with size ---- SELECT file_id,
file_name, bytes/1024/1024/1024 size_gb FROM dba_data_files WHERE tablespace_name =
'TABLESPACENAME';

view list of tables in tablespace ---- select table_name from dba_tables where
tablespace_name=’TABLESPACENAME’;

rename datafile ---- ALTER DATABASE MOVE DATAFILE ‘/PATH/DATAFILENAME.dbf’ TO


‘/PATH/NEWDATAFILENAME.dbf’;

reloacte datafile ---- ALTER DATABASE MOVE DATAFILE ‘/path/DATAFILENAME.dbf’ TO


‘/newpath/DATAFILENAME.dbf';

copy datafile ---- ALTER DATABASE MOVE DATAFILE ‘/path/DATAFILENAME.dbf’ TO


‘/newpath/DATAFILENAME.dbf' KEEP;

dba_tablespaces;

dba_data_files;

dba_temp_files;

v$tablespace;

v$datafile;

v$log

v$logfile

v$tempfile

v$rollstat

how to grant permissions to access a table to different user ----grant select on


banktab01 to mahesh; (conncet to table owner and run);

how to check datafiles space usage in percentage ---- SET PAUSE ON


SET PAUSE 'Press Return to Continue'
SET PAGESIZE 60
SET LINESIZE 300
COLUMN "Tablespace Name" FORMAT A20
COLUMN "File Name" FORMAT A80

SELECT Substr(df.tablespace_name,1,20) "Tablespace Name",


Substr(df.file_name,1,80) "File Name",
Round(df.bytes/1024/1024,0) "Size (M)",
decode(e.used_bytes,NULL,0,Round(e.used_bytes/1024/1024,0)) "Used (M)",
decode(f.free_bytes,NULL,0,Round(f.free_bytes/1024/1024,0)) "Free (M)",
decode(e.used_bytes,NULL,0,Round((e.used_bytes/df.bytes)*100,0)) "% Used"
FROM DBA_DATA_FILES DF,
(SELECT file_id,
sum(bytes) used_bytes
FROM dba_extents
GROUP by file_id) E,
(SELECT sum(bytes) free_bytes,
file_id
FROM dba_free_space
GROUP BY file_id) f
WHERE e.file_id (+) = df.file_id
AND df.file_id = f.file_id (+)
ORDER BY df.tablespace_name,
df.file_name
/

tablespace size with percentage ----

column "Tablespace" format a13


column "Used MB" format 99,999,999
column "Free MB" format 99,999,999
column "Total MB" format 99,999,999
select
fs.tablespace_name "Tablespace",
(df.totalspace - fs.freespace) "Used MB",
fs.freespace "Free MB",
df.totalspace "Total MB",
round(100 * (fs.freespace / df.totalspace)) "Pct. Free"
from
(select
tablespace_name,
round(sum(bytes) / 1048576) TotalSpace
from
dba_data_files
group by
tablespace_name
) df,
(select
tablespace_name,
round(sum(bytes) / 1048576) FreeSpace
from
dba_free_space
group by
tablespace_name
) fs
where
df.tablespace_name = fs.tablespace_name;

check tablespace size and percentage with no of datafiles ----

SELECT a.tablespace_name,
ROUND (((c.BYTES - NVL (b.BYTES, 0)) / c.BYTES) * 100,2) percentage_used,
c.BYTES / 1024 / 1024 space_allocated,
ROUND (c.BYTES / 1024 / 1024 - NVL (b.BYTES, 0) / 1024 / 1024,2) space_used,
ROUND (NVL (b.BYTES, 0) / 1024 / 1024, 2) space_free,
c.DATAFILES
FROM dba_tablespaces a,
( SELECT tablespace_name,
SUM (BYTES) BYTES
FROM dba_free_space
GROUP BY tablespace_name
) b,
( SELECT COUNT (1) DATAFILES,
SUM (BYTES) BYTES,
tablespace_name
FROM dba_data_files
GROUP BY tablespace_name
) c
WHERE b.tablespace_name(+) = a.tablespace_name
AND c.tablespace_name(+) = a.tablespace_name
ORDER BY NVL (((c.BYTES - NVL (b.BYTES, 0)) / c.BYTES), 0) DESC;

datafiles size and percentage

select df.tablespace_name, df.file_name, round(df.bytes/1024/1024) totalSizeMB,


nvl(round(usedBytes/1024/1024), 0) usedMB, nvl(round(freeBytes/1024/1024), 0)
freeMB,
nvl(round(freeBytes/df.bytes * 100), 0) freePerc, df.autoextensible
from dba_data_files df
left join (
select file_id, sum(bytes) usedBytes
from dba_extents
group by file_id
) ext on df.file_id = ext.file_id
left join (
select file_id, sum(bytes) freeBytes
from dba_free_space
group by file_id
) free on df.file_id = free.file_id
order by df.tablespace_name, df.file_name;

list and paths of datafiles ---- select name from v$datafile;

To get the tablespace for a particular Oracle table ---- select tablespace_name
from all_tables where owner = 'USERNAME' and table_name = 'TABLENAME';

To get the tablespaces for all Oracle tables in a particular library ---- select
table_name, tablespace_name from all_tables where owner = 'USERNAME';

To get the tablespace for a particular Oracle index ---- select tablespace_name
from all_indexes where owner = 'USERNAME' and index_name = 'INDEXNAME';

To get the tablespaces for all Oracle indexes in a particular library ---- select
index_name, tablespace_name from all_indexes where owner = 'USERNAME';

Create a duplicate table ---- CREATE TABLE New_Table_name AS SELECT * FROM


Existing_table_Name;

delete row from table ---- delete from tablename where row='row_name';

You might also like