Managing Tablespaces and Data files
A small database
might need only the SYSTEM tablespace; however, Oracle recommends that you
create additional tablespaces to store user data, user indexes, undo segments,
and temporary segments separate from data dictionary. This gives you more
flexibility in various database administration operations and reduces
contention among dictionary objects and schema objects for the same data files.
The DBA can
create new tablespaces, resize data files, add data files to tablespaces, set
and alter default segment storage settings for segments created in a
tablespace, make a tablespace read-only or read-write, make a tablespace
temporary or permanent, and drop tablespaces.
You can create
Locally Managed or Dictionary Managed Tablespaces. In prior versions of Oracle
only Dictionary managed Tablespaces were available but from Oracle ver. 8i you
can also create Locally managed tablespaces. The
advantages of locally managed tablespaces are
Locally managed tablespaces track all extent information in
the tablespace itself by using bitmaps, resulting in the following benefits:
- Concurrency and speed of space operations is improved, because space allocations and deallocations modify locally managed resources (bitmaps stored in header files) rather than requiring centrally managed resources such as enqueues
- Performance is improved, because recursive operations that are sometimes required during dictionary-managed space allocation are eliminated
To
create a locally managed
tablespace give the following command
SQL>
CREATE TABLESPACE usertbs
DATAFILE '/u02/oracle/ica/ica01.dbf' SIZE 50M
EXTENT MANAGEMENT LOCAL AUTOALLOCATE;
AUTOALLOCATE
causes the tablespace to be system managed with a minimum extent size of 64K.
SQL>
CREATE TABLESPACE usertbs DATAFILE
'/u02/oracle/ica/ica01.dbf' SIZE 50M EXTENT
MANAGEMENT LOCAL UNIFORM SIZE 256K;
To
Create Dictionary Managed
Tablespace
SQL>
CREATE TABLESPACE usertbs DATAFILE
'/u02/oracle/ica/ica01.dbf' SIZE 50M EXTENT MANAGEMENT DICTIONARY;
Bigfile Tablespaces (Introduced in Oracle Ver. 10g)
A
bigfile tablespace is a tablespace with a single, but very large (up to
4G blocks) datafile. Traditional smallfile tablespaces, in contrast, can
contain multiple datafiles, but the files cannot be as large. Bigfile
tablespaces can reduce the number of datafiles needed for a database.
To
create a bigfile tablespace give the following command
SQL>
CREATE BIGFILE TABLESPACE ica_bigtbs
DATAFILE '/u02/oracle/ica/bigtbs01.dbf' SIZE 50G;
To Extend the Size of a tablespace
Option 1
You can extend the size of a tablespace by increasing the size of an existing datafile by typing the following command
SQL> alter database usertbs datafile ‘/u01/oracle/data/icatbs01.dbf’ resize 100M;
This will increase the size from 50M to 100M
Option 2
You can also extend the size of a tablespace by adding a new datafile to a tablespace. This is useful if the size of existing datafile is reached o/s file size limit or the drive where the file is existing does not have free space. To add a new datafile to an existing tablespace give the following command.
SQL> alter tablespace add datafile ‘/u02/oracle/ica/icatbs02.dbf’ size 50M;
Option 3
You can also use auto extend feature of
datafile. In this, Oracle will automatically increase the size of a datafile
whenever space is required. You can specify by how much size the file should
increase and Maximum size to which it should extend.
To make a existing datafile auto extendable
give the following command
SQL>
alter database datafile ‘/u01/oracle/ica/icatbs01.dbf’ auto extend ON next 5M maxsize 500M;
To decrease the size of a tablespace
You can decrease the size of tablespace by
decreasing the datafile associated with it. You decrease a datafile only up to
size of empty space in it. To decrease the size of a datafile give the
following command
SQL>
alter database datafile ‘/u01/oracle/ica/icatbs01.dbf’ resize 30M;
Taking tablespaces Offline or Online
You can take an online tablespace offline so that it is
temporarily unavailable for general use. The rest of the database remains open
and available for users to access data. Conversely, you can bring an offline
tablespace online to make the schema objects within the tablespace available to
database users. The database must be open to alter the availability of a
tablespace.
To alter the availability of a tablespace, use the ALTER
TABLESPACE statement. You must have the ALTER TABLESPACE or MANAGE TABLESPACE
system privilege.
To Take a Tablespace Offline give the
following command
SQL>alter
tablespace usertbs offline;
To again bring it back online give
the following command.
SQL>alter tablespace usertbs
online;
To take individual datafile offline
type the following command
SQL>alter database datafile
‘/u01/oracle/ica/ica_tbs01.dbf’ offline;
Again to bring it back online give
the following command
SQL> alter database datafile
‘/u01/oracle/ica/ica_tbs01.dbf’ online;
Making a Tablespace Read only
Making a tablespace read-only prevents write operations
on the datafiles in the tablespace. The primary purpose of read-only
tablespaces is to eliminate the need to perform backup and recovery of large,
static portions of a database. Read-only tablespaces also provide a way to
protecting historical data so that users cannot modify it. Making a tablespace
read-only prevents updates on all tables in the tablespace, regardless of a
user's update privilege level.
To make a tablespace read only
SQL>alter tablespace ica read
only
Again to make it read write
SQL>alter tablespace ica read
write;
Renaming Tablespaces
Using the RENAME TO clause of the ALTER TABLESPACE, you
can rename a permanent or temporary tablespace. For example, the following
statement renames the users’ tablespace:
Sql>ALTER TABLESPACE users RENAME TO
usersts;
Dropping Tablespaces
You can drop
a tablespace and its contents (the segments contained in the tablespace) from
the database if the tablespace and its contents are no longer required. You
must have the DROP
TABLESPACE
system privilege to drop a tablespace.
Caution: Once a tablespace has been
dropped, the data in the tablespace is not recoverable. Therefore, make sure
that all data contained in a tablespace to be dropped will not be required in
the future. Also immediately before and after dropping a tablespace from a
database, back up the database completely.
To drop a tablespace give the following command.
SQL> drop
tablespace usertbs;
This will drop the tablespace only if it is
empty. If it is not empty and if you want to drop it anyhow then add the following
keyword
SQL>drop
tablespace usertbs including contents;
This will drop the tablespace even if it is not
empty. But the datafiles will not be deleted you have to use operating system
command to delete the files.
But If you include datafiles keyword then,
the associated datafiles will also be deleted from the disk.
SQL>drop tablespace usertbs
including contents and datafiles;Temporary Tablespace
Temporary tablespace is used for sorting large tables. Every database should have one temporary tablespace. To create temporary tablespace give the following command.
SQL>create temporary tablespace temp tempfile ‘/u01/oracle/data/ica_temp.dbf’ size 100M extent management local uniform size 5M;Increasing or Decreasing the size of a Temporary Tablespace
You can use the resize clause to increase or decrease the size of a temporary tablespace. The following statement resizes a temporary file:
SQL>ALTER DATABASE TEMPFILE '/u02/oracle/data/lmtemp02.dbf' RESIZE 18M;
The following
statement drops a temporary file and deletes the operating system file:
SQL> ALTER DATABASE TEMPFILE
'/u02/oracle/data/lmtemp02.dbf' DROP INCLUDING DATAFILES;
Default Temporary
Tablespace
After database
creation, a default temporary tablespace can be set by creating a temporary
tablespace and
then altering the database.
Sql>ALTER
DATABASE DEFAULT TEMPORARY TABLESPACE temp;
Viewing Information about Tablespaces and Data files
Oracle
has provided many Data dictionaries to view information about tablespaces and
datafiles. Some of them are:
To view
information about Tablespaces in a database give the following query
SQL>select * from dba_tablespaces SQL>select * from v$tablespace;
To
view information about Datafiles
SQL>select * from dba_data_files;SQL>select * from v$datafile;
To
view information about Tempfiles
SQL>select * from dba_temp_files;SQL>select * from v$tempfile;
To
view information about free space in datafiles
SQL>select * from dba_free_space;
To
view information about free space in tempfiles
SQL>select * from V$TEMP_SPACE_HEADER;Relocating or Renaming Datafiles
You
can rename datafiles to either change their names or relocate them.
Renaming or Relocating Datafiles belonging to a Single Tablespace
To
rename or relocate datafiles belonging to a Single Tablespace do the following.
1. Take the tablespace offline
2. Rename or Relocate the datafiles using operating system command
3. Give the ALTER TABLESPACE with RENAME DATAFILE option to
change the filenames within the Database.
4. Bring the tablespace Online
For Example suppose you have a
tablespace users with the following datafiles/u01/oracle/ica/usr01.dbf’
/u01/oracle/ica/usr02.dbf’
Now you want to relocate /u01/oracle/ica/usr01.dbf’ to ‘/u02/oracle/ica/usr01.dbf’ and want to rename ‘/u01/oracle/ica/usr02.dbf’ to ‘/u01/oracle/ica/users02.dbf’ then follow the given the steps
1. Bring the tablespace offline
SQL>
alter tablespace users offline;
2. Copy the file to new location using
o/s command.
$cp
/u01/oracle/ica/usr01.dbf /u02/oracle/ica/usr01.dbf’
Rename
the file ‘/u01/oracle/ica/usr02.dbf’ to ‘/u01/oracle/ica/users02.dbf’ using o/s
command.
$mv
/u01/oracle/ica/usr02.dbf /u01/oracle/ica/users02.dbf
3. Now start SQLPLUS and type the
following command to rename and relocate these files
SQL> alter
tablespace users rename file
‘/u01/oracle/ica/usr01.dbf’, ‘/u01/oracle/ica/usr02.dbf’ to
‘/u02/oracle/ica/usr01.dbf’,’/u01/oracle/ica/users02.dbf’;
‘/u01/oracle/ica/usr01.dbf’, ‘/u01/oracle/ica/usr02.dbf’ to
‘/u02/oracle/ica/usr01.dbf’,’/u01/oracle/ica/users02.dbf’;
4. Now bring the tablespace
Online
SQL>
alter tablespace users online;
Procedure for Renaming and Relocating Datafiles in Multiple Tablespaces
You
can rename and relocate datafiles in one or more tablespaces using the
ALTER DATABASE RENAME
FILE
statement. This
method is the only choice if you want to rename or relocate datafiles of
several tablespaces in one operation. You must have the ALTER DATABASE
system privilege
To
rename datafiles in multiple tablespaces, follow these steps.
1. Ensure that the database is
mounted but closed.
2. Copy the datafiles to be
renamed to their new locations and new names, using the operating system..
3. Use
ALTER DATABASE
to rename the file pointers in the
database control file.
For example, the following statement renames the datafiles
/u02/oracle/rbdb1/sort01.dbf
and /u02/oracle/rbdb1/user3.dbf
to /u02/oracle/rbdb1/temp01.dbf
and /u02/oracle/rbdb1/users03.dbf
, respectively:ALTER DATABASE
RENAME FILE '/u02/oracle/rbdb1/sort01.dbf',
'/u02/oracle/rbdb1/user3.dbf'
TO '/u02/oracle/rbdb1/temp01.dbf',
'/u02/oracle/rbdb1/users03.dbf;
Always provide complete filenames (including their paths) to properly identify the old and new datafiles. In particular, specify the old datafile names exactly as they appear in the DBA_DATA_FILES
view.
4. Back up the database. After making any structural changes to a database, always perform an immediate and complete backup.
5. Start the
Database
No comments:
Post a Comment