Sunteți pe pagina 1din 6

Tablespaces and Datafiles

Oracle stores data logically in tablespaces and physically in datafiles.


• Tablespaces:
– Can belong to only one database at a time
– Consist of one or more datafiles
– Are further divided into logical units of storage

• Datafiles:
– Can belong to only one tablespace and one database
– Are a repository for schema object data

Types of Tablespaces

• SYSTEM tablespace
– Created with the database
– Contains the data dictionary
– Contains the SYSTEM undo segment

• Non-SYSTEM tablespace
– Separate segments
– Eases space administration
– Controls amount of space allocated to a user

Creating Tablespaces
Sql>CREATE TABLESPACE userdata
DATAFILE '---path of datafile---- /userdata01.dbf'
SIZE 100M
AUTOEXTEND ON NEXT 5M MAXSIZE 200M;

Space Management in Tablespaces

Locally managed tablespace:

– Free extents managed in the tablespace


– Bitmap is used to record free extents
– Each bit corresponds to a block or group of blocks
– Bit value indicates free or used
Reduced contention on data dictionary tables
• No undo generated when space allocation or deallocation occurs
• No coalescing required
Sql>CREATE TABLESPACE userdata
DATAFILE '---path of datafile----- /userdata01.dbf'
SIZE 500M
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K;

• Dictionary-managed tablespace:

– Free extents are managed by the data dictionary


– Appropriate tables are updated when extents are allocated or deallocated
Extents are managed in the data dictionary
• Each segment stored in the tablespace can have a different storage clause
• Coalescing required

Sql>CREATE TABLESPACE userdata


DATAFILE '---path of datafile----- /userdata01.dbf'
SIZE 500M EXTENT MANAGEMENT DICTIONARY
DEFAULT STORAGE
(initial 1M NEXT 1M PCTINCREASE 0 );

Undo Tablespace

 Used to store undo segments


 Cannot contain any other objects
 Extents are locally managed
 Can only use the DATAFILE and EXTENT MANAGEMENT clauses

Sql>CREATE UNDO TABLESPACE undo1


DATAFILE '---path of datafile---- /undo01.dbf'
SIZE 40M ;

Temporary Tablespaces

Used for sort operations


Cannot contain any permanent objects
Locally managed extents recommended

Sql>CREATE TEMPORARY TABLESPACE temp


TEMPFILE '---path of datafile-----/temp01.dbf' SIZE 500M
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 4M;
Default Temporary Tablespace

Specifies a database-wide default temporary tablespace


• Eliminates using SYSTEM tablespace for storing
temporary data
• Can be created by using:
– CREATE DATABASE
– Locally managed
– ALTER DATABASE

Sql>ALTER DATABASE
DEFAULT TEMPORARY TABLESPACE temp

Read Only Tablespaces

– Causes a checkpoint
– Data available only for read operations
– Objects can be dropped from tablespace

Sql>ALTER TABLESPACE userdata READ ONLY ;

Taking a Tablespace Offline

• Not available for data access


• Tablespaces that cannot be taken offline:
– SYSTEM tablespace
– Tablespaces with active undo segments
– Default temporary tablespace

Sql>ALTER TABLESPACE userdata OFFLINE;


Sql>ALTER TABLESPACE userdata ONLINE ;

Changing Storage Settings


Using ALTER TABLESPACE command to change storage settings

Sql>ALTER TABLESPACE userdata


DEFAULT STORAGE (INITIAL 2M NEXT 2M MAXEXTENTS 999 );
Resizing a Tablespace

A tablespace can be resized by:


• Changing the size of a datafile:
– Automatically using AUTOEXTEND
– Manually using ALTER TABLESPACE
• Adding a datafile using ALTER TABLESPACE

Can be resized automatically with the following commands:


– CREATE DATABASE
– CREATE TABLESPACE
– ALTER TABLESPACE … ADD DATAFILE

Sql>CREATE TABLESPACE user_data


DATAFILE'---path of datafile---- /userdata01.dbf' SIZE 200M
AUTOEXTEND ON NEXT 10M MAXSIZE 500M ;

Manually Resizing a Datafile


Manually increase or decrease a datafile size using ALTER DATABASE
• Resizing a datafile adds more space without adding more datafiles
• Manual resizing of a datafile reclaims unused space in database

Sql>ALTER DATABASE
DATAFILE '---path of datafile-----/userdata02.dbf'
RESIZE 200M;

Adding Datafiles to a Tablespace

• Increases the space allocated to a tablespace by adding additional datafiles


• ADD DATAFILE clause is used to add a datafile

Sql>ALTER TABLESPACE user_data


ADD DATAFILE ''---path of datafile-----/userdata03.dbf'
SIZE 200M;
Dropping Tablespaces

• Cannot drop a tablespace if it:


– Is the SYSTEM tablespace
– Has active segments
• INCLUDING CONTENTS drops the segments
• INCLUDING CONTENTS AND DATAFILES deletes datafiles
• CASCADE CONSTRAINTS drops all referential integrity constraints

DROP TABLESPACE userdata


INCLUDING CONTENTS AND DATAFILES

Obtaining Tablespace Information


Obtaining tablespace and datafile information can be obtained by querying the
following:

• Tablespaces:
– DBA_TABLESPACES
– V$TABLESPACE

• Datafile information:
– DBA_DATA_FILES
– V$DATAFILE

• Temp file information:


– DBA_TEMP_FILES
– V$TEMPFILE

S-ar putea să vă placă și