Академический Документы
Профессиональный Документы
Культура Документы
Daniel W. Fink
Overview
Where is the information
Data Dictionary views & tables
How can I extract it?
SQL, functions
How can I use it?
SQL*Plus to build and execute the script
Method
Build a script that generates the create
tablespace commands for the database
Not all options considered
AUTOEXTEND
MINEXTENT
ONLINE/OFFLINE
*I have to leave something for you to add
Not all tablespaces
Enough to demonstrate likely permutations
Command
Rules
Tablespace can be TEMPORARY
TEMPORARY tablespace can use DATA files
or TEMP files
A single tablespace can have multiple data files
Command structure
CREATE [TEMPORARY] TABLESPACE tablespacename
[TEMPORARY]
{DATAFILE|TEMPFILE} filename filesize
EXTENT MANAGEMENT [DICTIONARY|LOCAL
[{AUTOALLOCATE|UNIFORM} [size {K|M}]]}]
Oracles Data Dictionary
Access via Views
1390 in 8i
1820 in 9i
DBA_ - Everything in database
ALL_ - What you can see
USER_ What you own
Other Miscellaneous views
Where does it come from?
Views of underlying base objects
Provides level of abstraction
Easy to understand
Hides translations and calculations
READ ONLY!
Columns View Text
TABLESPACE_NAME select ts.name,
INITIAL_EXTENT ts.blocksize * ts.dflinit,
NEXT_EXTENT decode(bitand(ts.flags, 3), 1, to_number(NULL),
ts.blocksize*ts.dflincr),
MIN_EXTENTS ts.dflminext,
MAX_EXTENTS decode(ts.contents$, 1, to_number(NULL), ts.dflmaxext),
PCT_INCREASE decode(bitand(ts.flags, 3), 1, to_number(NULL),
ts.dflextpct),
MIN_EXTLEN ts.blocksize * ts.dflminlen,
STATUS decode(ts.online$,1,'ONLINE',2,'OFFLINE',
4,'READ ONLY','UNDEFINED'),
CONTENTS decode(ts.contents$, 0, 'PERMANENT', 1, 'TEMPORARY'),
LOGGING decode(ts.dflogging, 0, 'NOLOGGING', 1, 'LOGGING'),
EXTENT_MANAGEMENT decode(ts.bitmapped, 0, 'DICTIONARY', 'LOCAL'),
ALLOCATION_TYPE decode(bitand(ts.flags,3),0,'USER',1,'SYSTEM',2,'UNIFORM',
'UNDEFINED'),
PLUGGED_IN decode(ts.plugged, 0, 'NO', 'YES')
from sys.ts$ ts
where ts.online$ != 3
Where can I find more
information?
Oracle Documentation Server Reference
Guide
DBA_VIEWS
text contains the actual text of the view
SET LONG 10000 in order to see all the text
catalog.sql
sql.bsq
Notes on Data Dictionary
ID
COLUMN_ID starts at 1
EXTENT_ID starts at 0
Not All DBA_* views have ALL_* and/or USER_*
counterparts
DBA_EXTENTS and USER_EXTENTS have different
information
USER_EXTENTS does not contain file information
DBA_TAB_COLUMNS
Not just table columns, also views, clusters
DBA_IND_COLUMNS
Reverse Key Indexes have function name instead of table column
name for leading column
Quick Tour of the Data
Dictionary
DBA_CONS_COLUMNS DBA_ROLES
DBA_VIEWS DBA_ROLE_PRIVS
DBA_TAB_PRIVS
DBA_SYS_PRIVS
DBA_PROFILES
Data Dictionary views for script
DBA_TABLESPACES
All tablespaces in database
DBA_DATAFILES
All DATAFILEs for all tablespaces
DBA_TEMPFILES
*New in Oracle 8i
Identifies TEMPFILES created by command
create temporary tablespace
Using SQL to Write SQL
Joins
Unions
Functions
Sorting
Joins
A relationship using common data (natural or
derived) must exist
Inner
If a related row exists, return both main and related row
If a related row does not exist, do not return the main
row
Outer
If a related row exists, return both main and related row
If a related row does not exist, return main row and null
values for related row
Join
select t.tablespace_name,
d.file_name
from dba_tablespaces t,
dba_data_files d
where d.tablespace_name = t.tablespace_name;
TABLESPACE_NAME FILE_NAME
--------------- ---------------------------------
SYSTEM C:\ORADATA\ORA817\SYSTEM01.DBF
TOOLS C:\ORADATA\ORA817\TOOLS01.DBF
TOOLS C:\ORADATA\ORA817\TOOLS02.DBF
TEMP2 C:\ORADATA\ORA817\TEMP201.DBF
Union
Set Operation
Used to combine sets of data
Joins & Relationships are not used
Columns must be same number and datatype
A literal expression can be used
Returns one row for each distinct set of values
UNION ALL will return duplicates
select statement1
union
select statement2
File List
File names are listed in
DBA_DATA_FILES (10 rows)
DBA_TEMP_FILES (1 row)
Common Data?
Both contain tablespace_name
File List
select d.file_name, t.file_name
from dba_data_files d,
dba_temp_files t
where d.tablespace_name = t.tablespace_name;
no rows selected
Why?
Tablespaces use either data files or temp
filesnot both
Even an outer join will miss rows
File List
FILE_NAME
select file_name ------------------------------
from dba_data_files C:\ORADATA\ORA817\RBS01.DBF
C:\ORADATA\ORA817\SYSTEM01.DBF
union C:\ORADATA\ORA817\TEMP01.DBF
C:\ORADATA\ORA817\TEMP201.DBF
select file_name C:\ORADATA\ORA817\TOOLS01.DBF
from dba_temp_files; C:\ORADATA\ORA817\TOOLS02.DBF
Tablespace/File List
'C:\ORADATA\ORA817\SYSTEM01.DBF'
'C:\ORADATA\ORA817\TOOLS01.DBF'
'C:\ORADATA\ORA817\SYSTEM01.DBF'
'C:\ORADATA\ORA817\TOOLS01.DBF'
New Line
CHR(10) prints a newline character
Able to output single row of data on multiple lines
Line 1
Line 2
CREATE TABLESPACE
select CREATE TABLESPACE ||
t.tablespace_name||chr(10)||
DATAFILE ||chr(39)||
d.file_name||chr(39)
from dba_tablespaces t,
dba_data_files d
where d.tablespace_name = t.tablespace_name;
CREATE TABLESPACE Error
CREATE TABLESPACE SYSTEM
DATAFILE 'C:\ORADATA\ORA817\SYSTEM01.DBF'
TS_NAME SQL_COMMAND
------- -------------------------------------------------------
SYSTEM CREATE TABLESPACE SYSTEM
SYSTEM DATAFILE 'C:\ORADATA\ORA817\SYSTEM01.DBF' SIZE 280576K
TEMP CREATE TEMPORARY TABLESPACE TEMP
TEMP TEMPFILE 'C:\ORADATA\ORA817\TEMP01.DBF' SIZE 204800K
TEMP2 CREATE TEMPORARY TABLESPACE TEMP2
TEMP2 DATAFILE 'C:\ORADATA\ORA817\TEMP201.DBF' SIZE 102400K
TOOLS CREATE TABLESPACE TOOLS
TOOLS DATAFILE 'C:\ORADATA\ORA817\TOOLS01.DBF' SIZE 12288K
TOOLS DATAFILE 'C:\ORADATA\ORA817\TOOLS02.DBF' SIZE 102400K
COLUMN Formatting
SQL> COLUMN ts_name NOPRINT
SQL> /
SQL_COMMAND
-------------------------------------------------------
CREATE TABLESPACE SYSTEM
DATAFILE 'C:\ORADATA\ORA817\SYSTEM01.DBF' SIZE 280576K
CREATE TEMPORARY TABLESPACE TEMP
TEMPFILE 'C:\ORADATA\ORA817\TEMP01.DBF' SIZE 204800K
CREATE TEMPORARY TABLESPACE TEMP2
DATAFILE 'C:\ORADATA\ORA817\TEMP201.DBF' SIZE 102400K
CREATE TABLESPACE TOOLS
DATAFILE 'C:\ORADATA\ORA817\TOOLS01.DBF' SIZE 12288K
DATAFILE 'C:\ORADATA\ORA817\TOOLS02.DBF' SIZE 102400K
Formatting Output
Supress all non-sql statement strings
SET
PAGESIZE 0 suppress all breaks, headings,
etc.
FEEDBACK OFF suppress 10 Rows
Returned message
SQLPROMPT removes SQL> prompt
Be carefulit may look like the command hangs
Formatting Output
SQL> col ts_name noprint
SQL> set pagesize 0 feedback off sqlprompt ""
/
CREATE TABLESPACE SYSTEM
DATAFILE 'C:\ORADATA\ORA817\SYSTEM01.DBF' SIZE 280576K
CREATE TEMPORARY TABLESPACE TEMP
TEMPFILE 'C:\ORADATA\ORA817\TEMP01.DBF' SIZE 204800K
CREATE TEMPORARY TABLESPACE TEMP2
DATAFILE 'C:\ORADATA\ORA817\TEMP201.DBF' SIZE 102400K
CREATE TABLESPACE TOOLS
DATAFILE 'C:\ORADATA\ORA817\TOOLS01.DBF' SIZE 12288K
DATAFILE 'C:\ORADATA\ORA817\TOOLS02.DBF' SIZE 102400K
OOPS
DBA_TABLESPACES.CONTENTS lists
contents, but not type of file
Each command creates a TEMPORARY
tablespace, but the file specs are not the same!
CREATE TABLESPACE TEMPORARY <name> TEMPFILE <name> <specs>
CREATE TABLESPACE <name> TEMPORARY DATAFILE <name> <specs>