Professional Documents
Culture Documents
A collection of 19 FAQs on creating and managing tablespaces and data files. Clear
answers are provided with tutorial exercises on creating and dropping tablespaces; listing
available tablespaces; creating and dropping data files; setting tablespaces and data files
offline; removing corrupted data files. Topics included in this FAQ are:
1. What Is an Oracle Tablespace?
2. What Is an Oracle Data File?
3. How a Tablespace Is Related to Data Files?
4. How a Database Is Related to Tablespaces?
5. How To View the Tablespaces in the Current Database?
6. What Are the Predefined Tablespaces in a Database?
7. How To View the Data Files in the Current Database?
8. How To Create a new Oracle Data File?
9. How To Create a New Tablespace?
10. How To Rename a Tablespace?
11. How To Drop a Tablespace?
12. What Happens to the Data Files If a Tablespace Is Dropped?
13. How To Create a Table in a Specific Tablespace?
14. How To See Free Space of Each Tablespace?
15. How To Bring a Tablespace Offline?
16. How To Bring a Tablespace Online?
17. How To Add Another Datafile to a Tablespace?
18. What Happens If You Lost a Data File?
19. How Remove Data Files befor opening a Database?
Sample scripts used in this FAQ assumes that you are connected to the server with the
SYSTEM user account on the default database instance XE. See other FAQ collections
on how to connect to the server.
An Oracle data file is a big unit of physical storage in the OS file system. One or many
Oracle data files are organized together to provide physical storage to a single Oracle
tablespace.
Each tablespace in an Oracle database consists of one or more files called datafiles,
which are physical structures that conform to the operating system in which Oracle is
running.
If you want to get a list of all tablespaces used in the current database instance, you can
use the DBA_TABLESPACES view as shown in the following SQL script example:
SQL> connect SYSTEM/fyicenter
Connected.
When you create a new database, Oracle server will create 4 required tablespaces for the
new database:
• SYSTEM Tablespace - Every Oracle database contains a tablespace named
SYSTEM, which Oracle creates automatically when the database is created. The
SYSTEM tablespace is always online when the database is open.
• SYSAUX Tablespace - The SYSAUX tablespace was installed as an auxiliary
tablespace to the SYSTEM tablespace when you created your database. Some
database components that formerly created and used separate tablespaces now
occupy the SYSAUX tablespace.
• UNDO Tablespace - UNDO tablespaces are special tablespaces used solely for
storing undo information. You cannot create any other segment types (for example,
tables or indexes) in undo tablespaces. Each database contains zero or more undo
tablespaces. In automatic undo management mode, each Oracle instance is
assigned one (and only one) undo tablespace. Undo data is managed within an undo
tablespace using undo segments that are automatically created and maintained by
Oracle.
• TEMP Tablespace - When the SYSTEM tablespace is locally managed, you must
define at least one default temporary tablespace when creating a database. A locally
managed SYSTEM tablespace cannot be used for default temporary storage.
If you want to get a list of all tablespaces used in the current database instance, you can
use the DBA_TABLESPACES view as shown in the following SQL script example:
SQL> connect SYSTEM/fyicenter
Connected.
There is no dedicated statement to create a data file. Data files are created as part of
statements that manages other data structures, like tablespace and database.
If you want a new dataspace, you can use the CREATE TABLESPACE ... DATAFILE
statement as shown in the following script:
SQL> CREATE TABLESPACE my_space
2 DATAFILE '/temp/my_space.dbf' SIZE 10M;
Tablespace created.
So one statement created two structures: a tablespace and a data file. If you check your
file system with Windows file explorer, you will see the data file is located in the \temp
directory of. The data file size is about 10MB. Its contents should be blank and full of \x00
at this time.
How To Rename a Tablespace?
You can easily rename a tablespace by using the ALTER TABLESPACE ... RENAME TO
statement as shown in the example below:
SQL> CREATE TABLESPACE my_space
2 DATAFILE '/temp/my_space.dbf' SIZE 10M;
Tablespace created.
If you have an existing tablespace and you don't want it anymore. You can delete a
tablespace by using the DROP TABLESPACE statement as shown in the example below:
SQL> CREATE TABLESPACE my_space
2 DATAFILE '/temp/my_space.dbf' SIZE 10M;
Tablespace created.
If a tablespace is dropped, what happens to its data files? By default, data files will
remain in OS file system, even if the tablespace they are mapped is dropped. Of course,
you delete the data files using OS commands, if they are no longer needed.
Another way of deleting data files is to use the INCLUDING clause in the DROP
TABLESPACE statement. Here is a SQL sample script:
SQL> CREATE TABLESPACE my_space
2 DATAFILE '/temp/my_space.dbf' SIZE 10M;
Tablespace created.
With the INCLUDING CONTENTS AND DATAFILES clause, all contents and mapped
data files are also deleted.
How To Create a Table in a Specific Tablespace?
After you have created a new tablespace, you can give it to your users for them to create
tables in the new tablespace. To create a table in a specific tablespace, you need to use
the TABLESPACE clause in the CREATE TABLE statement. Here is a sample script:
SQL> connect SYSTEM/fyicenter
Connected.
One of the important DBA tasks is to watch the storage usage of all the tablespaces to
make sure there are enough free space in each tablespace for database applications to
function properly. Free space information can be monitored through the
USER_FREE_SPACE view. Each record in USER_FREE_SPACE represents an extent,
a contiguous area of space, of free space in a data file of a tablespace.
If you want to stop users using a tablespace, you can bring it offline using the ALTER
TABLESPACE ... OFFLINE statement as shown in the following script:
SQL> connect HR/fyicenter
Connected.
After bringing a tablespace offline, you can backup or rename the data file safely.
If you have brought a tablespace offline, now you want to make it available to users
again, you can use the ALTER TABLESPACE ... ONLINE statement as shown in the
following script:
SQL> connect HR/fyicenter
...
SQL> ALTER TABLESPACE my_space ONLINE;
Tablespace altered.
If you created a tablespace with a data file a month ago, now 80% of the data file is used,
you should add another data file to the tablespace. This can be done by using the ALTER
TABLESPACE ... ADD DATAFILE statement. See the following sample script:
SQL> connect HR/fyicenter
After you shuting down an Oracle database, you accidently deleted a data file from the
operating system. If you try to start the database again you will get error when Oracle
tries to open the database after mounting the database. The following tutorial examples
shows you what will happen if the data file c:\temp\my_space.dbf is deleted. Oracle can
still start the database instance and mount the database. But it will fail on openning the
database as shown below in a SQL*Plus session:
>sqlplus /nolog
SQL> SHUTDOWN;
ORA-01109: database not open
Database dismounted.
ORACLE instance shut down.
Let's say you have a corrupted data file or lost a data file. Oracle can mount the
database. But it will not open the database. What you can do is to set the bad data file as
offline befor opening the database. The tutorial exercise shows you how to set two data
files offline and open the database without them:
>sqlplus /nolog
At this point, if you don't care about the data in MY_SPACE, you can drop it now with the
database opened.