Thursday, 28 November 2019

Working on oracle tablespace..


SQL> CREATE TABLESPACE svss DATAFILE '/u02/oracle/svss/svss01.dbf' SIZE 50m EXTENT MANAGEMENT LOCAL AUTOALLOCATE;

AUTOALLOCATE causes the tablespace to be system managed with a minimum extent size of 64K.
The alternative to AUTOALLOCATE is UNIFORM. which specifies that the tablespace is managed with extents of uniform size. You can specify that size in the SIZE clause of UNIFORM. If you omit SIZE, then the default size is 1M. The following example creates a Locally managed tablespace with uniform extent size of 256K

SQL> CREATE TABLESPACE svss DATAFILE '/u02/oracle/svss/svss01.dbf' SIZE 50M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K;

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 svss datafile ‘/u01/oracle/data/svss01.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 svss add datafile  /u02/oracle/svss/svss02.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/svss/svss01.dbf’ auto extend ON next 5M maxsize 500M;
You can also make a datafile auto extendable while creating a new tablespace itself by giving the following command.
SQL> create tablespace svss datafile ‘/u01/oracle/svss/svss01.dbf’ size 50M auto extend ON next 5M maxsize 500M;



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/svss/svss01.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 svss offline;
To again bring it back online give the following command.
SQL>alter tablespace svss online;



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 svss read only
Again to make it read write
SQL>alter tablespace svss read write;


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 DROPTABLESPACE 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 svss;
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 svss 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 svss including contents and datafiles;


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/svss_temp.dbf’ size 100M 
   extent management local  uniform size 5M;

The extent management clause is optional for temporary tablespaces because all temporary tablespaces are created with locally managed extents of a uniform size.  The AUTOALLOCATE clause is not allowed for temporary tablespaces.

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/temp02.dbf' RESIZE 18M;

The following statement drops a temporary file and deletes the operating system file:
SQL> ALTER DATABASE TEMPFILE '/u02/oracle/data/temp02.dbf' DROP  INCLUDING DATAFILES;

No comments:

Post a Comment