Managing Oracle Tablespaces and Data Files

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.

What Is an Oracle Tablespace?

An Oracle tablespace is a big unit of logical storage in an Oracle database. It is managed and used by the Oracle server to store structures data objects, like tables and indexes.

What Is an Oracle Data File?

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.

How a Tablespace Is Related to Data Files?

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.

How a Database Is Related to Tablespaces?

A database’s data is collectively stored in the datafiles that constitute each tablespace of the database. For example, the simplest Oracle database would have one tablespace and one datafile. Another database can have three tablespaces, each consisting of two datafiles (for a total of six datafiles).

How To View the Tablespaces in the Current Database?

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.

SQL> SELECT TABLESPACE_NAME, STATUS, CONTENTS
  2  FROM USER_TABLESPACES;
TABLESPACE_NAME                STATUS    CONTENTS
------------------------------ --------- ---------
SYSTEM                         ONLINE    PERMANENT
UNDO                           ONLINE    UNDO
SYSAUX                         ONLINE    PERMANENT
TEMP                           ONLINE    TEMPORARY
USERS                          ONLINE    PERMANENT

What Are the Predefined Tablespaces in a Database?

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.

How To View the Data Files in the Current Database?

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.

SQL> col tablespace_name format a16;
SQL> col file_name format a36;
SQL> SELECT TABLESPACE_NAME, FILE_NAME, BYTES
  2  FROM DBA_DATA_FILES;
TABLESPACE_NAME FILE_NAME                           BYTES
--------------- ------------------------------- ---------
USERS           \ORACLEXE\ORADATA\XE\USERS.DBF  104857600
SYSAUX          \ORACLEXE\ORADATA\XE\SYSAUX.DBF 461373440
UNDO            \ORACLEXE\ORADATA\XE\UNDO.DBF    94371840
SYSTEM          \ORACLEXE\ORADATA\XE\SYSTEM.DBF 356515840

How To Create a new Oracle Data File? 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. How To Create a New Tablespace? 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.

SQL> SELECT TABLESPACE_NAME, STATUS, CONTENTS
  2  FROM USER_TABLESPACES;
TABLESPACE_NAME  STATUS          CONTENTS
---------------- --------------- ---------
SYSTEM           ONLINE          PERMANENT
UNDO             ONLINE          UNDO
SYSAUX           ONLINE          PERMANENT
TEMP             ONLINE          TEMPORARY
USERS            ONLINE          PERMANENT
MY_SPACE         ONLINE          PERMANENT

SQL> SELECT TABLESPACE_NAME, FILE_NAME, BYTES
  2  FROM DBA_DATA_FILES;
TABLESPACE_NAME FILE_NAME                            BYTES
--------------- -------------------------------- ---------
USERS           \ORACLEXE\ORADATA\XE\USERS.DBF   104857600
SYSAUX          \ORACLEXE\ORADATA\XE\SYSAUX.DBF  461373440
UNDO            \ORACLEXE\ORADATA\XE\UNDO.DBF     94371840
SYSTEM          \ORACLEXE\ORADATA\XE\SYSTEM.DBF  356515840
MY_SPACE        \TEMP\MY_SPACE.DBF                10485760

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.

SQL> ALTER TABLESPACE my_space RENAME TO your_space;
Tablespace created.

SQL> SELECT TABLESPACE_NAME, STATUS, CONTENTS
  2  FROM USER_TABLESPACES;
TABLESPACE_NAME  STATUS          CONTENTS
---------------- --------------- ---------
SYSTEM           ONLINE          PERMANENT
UNDO             ONLINE          UNDO
SYSAUX           ONLINE          PERMANENT
TEMP             ONLINE          TEMPORARY
USERS            ONLINE          PERMANENT
YOUR_SPACE       ONLINE          PERMANENT

How To Drop a Tablespace?

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.

SQL> DROP TABLESPACE my_space;
Tablespace dropped.

What Happens to the Data Files If a Tablespace Is Dropped?

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.

SQL> DROP TABLESPACE my_space INCLUDING CONTENTS
  2  AND DATAFILES;
Tablespace dropped.

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.

SQL> CREATE TABLESPACE my_space
  2  DATAFILE '/temp/my_space.dbf' SIZE 10M;
Tablespace created.

SQL> connect HR/fyicenter
Connected.

SQL> CREATE TABLE my_team TABLESPACE my_space
  2  AS SELECT * FROM employees;
Table created.

SQL> SELECT table_name, tablespace_name, num_rows
  2  FROM USER_TABLES
  3  WHERE tablespace_name in ('USERS', 'MY_SPACE');

TABLE_NAME                     TABLESPACE_NAME    NUM_ROWS
------------------------------ ---------------- ----------
MY_TEAM                        MY_SPACE           -
EMPLOYEES                      USERS              107
...

How To See Free Space of Each Tablespace?

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.

Here is SQL script example on how to see free space of a tablespace:

SQL> connect HR/fyicenter
Connected.

SQL> SELECT TABLESPACE_NAME, FILE_ID, BYTES
  2  FROM USER_FREE_SPACE
  3  WHERE TABLESPAE_NAME IN ('USERS', 'MY_SPACE');
TABLESPACE_NAME                   FILE_ID      BYTES
------------------------------ ---------- ----------
MY_SPACE                                5   10354688
USERS                                   4  101974016
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536
USERS                                   4      65536

This tells us that:

  • MY_SPACE has a single free extent of 10MB.
  • USERS has one big free extent of 100MB, and many small free extents

of 64KB.

    How To Bring a Tablespace Offline?

    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.
    
    SQL> CREATE TABLESPACE my_space
      2  DATAFILE '/temp/my_space.dbf' SIZE 10M;
    Tablespace created.
    
    SQL> ALTER TABLESPACE my_space OFFLINE NORMAL;
    Tablespace altered.

    After bringing a tablespace offline, you can backup or rename the

    data file safely.

    How To Bring a Tablespace Online?

    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> CREATE TABLESPACE my_space
      2  DATAFILE '/temp/my_space.dbf' SIZE 10M;
    Tablespace created.
    
    SQL> ALTER TABLESPACE my_space OFFLINE NORMAL;
    Tablespace altered.
    
    ...
    
    SQL> ALTER TABLESPACE my_space ONLINE;
    Tablespace altered.

    How To Add Another Datafile to a Tablespace?

    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
    
    SQL> CREATE TABLESPACE my_space
      2  DATAFILE '/temp/my_space.dbf' SIZE 10M;
    Tablespace created.
    
    SQL> ALTER TABLESPACE my_space
      2  DATAFILE '/temp/my_space_2.dbf' SIZE 5M;
    Tablespace altered.
    
    SQL> SELECT TABLESPACE_NAME, FILE_NAME, BYTES
      2  FROM DBA_DATA_FILES;
    TABLESPACE_NAME FILE_NAME                             BYTES
    --------------- --------------------------------- ---------
    USERS           C:\ORACLEXE\ORADATA\XE\USERS.DBF  104857600
    SYSAUX          C:\ORACLEXE\ORADATA\XE\SYSAUX.DBF 461373440
    UNDO            C:\ORACLEXE\ORADATA\XE\UNDO.DBF    94371840
    SYSTEM          C:\ORACLEXE\ORADATA\XE\SYSTEM.DBF 356515840
    MY_SPACE        C:\TEMP\MY_SPACE.DBF               10485760
    MY_SPACE        C:\TEMP\MY_SPACE_2.DBF              5242880
    
    SQL> SELECT TABLESPACE_NAME, FILE_ID, BYTES
      2  FROM USER_FREE_SPACE
      3  WHERE TABLESPAE_NAME IN ('MY_SPACE');
    TABLESPACE_NAME                   FILE_ID      BYTES
    ------------------------------ ---------- ----------
    MY_SPACE                                6    5177344
    MY_SPACE                                5   10354688

    This script created one tablespace with two data files.

    What Happens If You Lost a Data File?

    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> connect SYSTEM/fyicenter AS SYSDBA
    
    SQL> STARTUP
    ORACLE instance started.
    
    Total System Global Area  100663296 bytes
    Fixed Size                  1285956 bytes
    Variable Size              58720444 bytes
    Database Buffers           37748736 bytes
    Redo Buffers                2908160 bytes
    Database mounted.
    ORA-01157: cannot identify/lock data file 5 - see DBWR
      trace file
    ORA-01110: data file 5: 'C:\TEMP\MY_SPACE.DBF'
    
    SQL> SHUTDOWN;
    ORA-01109: database not open
    Database dismounted.
    ORACLE instance shut down.

    How Remove Data Files befor opening a Database?

    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
    
    SQL> connect SYSTEM/fyicenter AS SYSDBA
    
    SQL> STARTUP MOUNT;
    ORACLE instance started.
    
    Total System Global Area  100663296 bytes
    Fixed Size                  1285956 bytes
    Variable Size              58720444 bytes
    Database Buffers           37748736 bytes
    Redo Buffers                2908160 bytes
    Database mounted.
    
    SQL> ALTER DATABASE DATAFILE '\temp\my_space.dbf'
      2  OFFLINE DROP;
    Database altered.
    
    SQL> ALTER DATABASE DATAFILE '\temp\my_space_2.dbf'
      2  OFFLINE DROP;
    Database altered.
    
    SQL> ALTER DATABASE OPEN;
    Database altered.
    
    SQL> col file_name format a36;
    SQL> col tablespace_name format a16;
    SQL> SELECT TABLESPACE_NAME, FILE_NAME, BYTES
      2  FROM DBA_DATA_FILES;
    
    TABLESPACE_NAME FILE_NAME                             BYTES
    --------------- --------------------------------- ---------
    USERS           C:\ORACLEXE\ORADATA\XE\USERS.DBF  104857600
    SYSAUX          C:\ORACLEXE\ORADATA\XE\SYSAUX.DBF 503316480
    UNDO            C:\ORACLEXE\ORADATA\XE\UNDO.DBF    94371840
    SYSTEM          C:\ORACLEXE\ORADATA\XE\SYSTEM.DBF 367001600
    MY_SPACE        C:\TEMP\MY_SPACE.DBF
    MY_SPACE        C:\TEMP\MY_SPACE_2.DBF

    At this point, if you don’t care about the data in MY_SPACE, you can

    drop it now with the database opened.

    Send article as PDF to PDF Creator

    22 comments

    1. banping says:

      Asher都用E文写这么好的资料,强啊。

    2. Scooby says:

      Great post, keep up the good work.

    3. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

    4. Nice design, what is the name of template ?

    5. Danilo Mruk says:

      Good post. I want to start working out more. Need to find me a scholarship for moms too lol jeez!

    6. I have been just studying this page it is very well written, What i’m looking around through the internet searching for precisely how to do this blog site thing and your site is almost certainly quite professional.

    7. Cheers for the info. Greatly appreciated.

    8. 2 more weeks until Giants training camp!!! Well 1 week really but 2 weeks for me.

    9. You guys possess a wonderful weblog proceeding the following, KIU!

    10. Football season is right around the corner , my Giants are ready to go back to superbowl, see the rest of the losers in the draft.

    11. Sick and tired of getting low amounts of useless visitors for your site? Well i wish to share with you a fresh underground tactic that makes myself $900 per day on 100% AUTOPILOT. I really could be here all day and going into detail but why dont you just check their website out? There is really a great video that explains everything. So if your serious about producing effortless money this is the website for you. Auto Traffic Avalanche

    12. Why have you removed my post? It was very useful information and i assure atleast one person found it helpful unlike the rest of the comments on this web site. I’ll post it again. Fed up with obtaining low numbers of useless visitors to your site? Well i wish to tell you about a brand new underground tactic which makes myself $900 daily on 100% AUTOPILOT. I possibly could be here all day and going into detail but why dont you simply check their site out? There is a great video that explains everything. So if your seriously interested in producing quick cash this is the site for you. Auto Traffic Avalanche

    13. Ryan Bachar says:

      Why have you removed my post? It was very useful information and i promise atleast 1 person found it helpful unlike the rest of the comments on this website. I’ll post it again. Sick and tired of obtaining low numbers of useless traffic to your website? Well i want to inform you of a brand new underground tactic that produces myself $900 per day on 100% AUTOPILOT. I could truthfully be here all day and going into detail but why dont you just check their site out? There is a excellent video that explains everything. So if your seriously interested in making quick money this is the site for you. Auto Traffic Avalanche

    14. Why have you deleted my post? It was very beneficial information and i guarantee atleast 1 person found it helpful unlike the rest of the comments on this site. I’ll post it again. Fed up with getting low amounts of useless visitors to your site? Well i wish to inform you of a new underground tactic that produces myself $900 every day on 100% AUTOPILOT. I could truthfully be here all day and going into detail but why dont you just check their website out? There is really a excellent video that explains everything. So if your serious about making effortless money this is the website for you. Auto Traffic Avalanche

    15. Sal Mozer says:

      Good infomation here, thanks.

    16. I’ve wanted to advise you, u r friggin on. I got to your post from another website and am really fascinated by this topic and reading more. Do you mind if I comment to this website from my site?

    17. Hey guys i want to share with you a way i make $500 daily and i only spend 5 minuites doing it a day! I strongly suggest you check their website out as there is really a brilliant video that explains everything you have to know. Check them out at DOMINATE MOBILE MARKETING

    18. Benita Heber says:

      Hey guys i wish to share with you a way i make $500 every day and i only spend 5 minuites doing it a day! I strongly suggest you check their website out as there is a brilliant video that explains everything you need to know. Check them out at DOMINATE MOBILE MARKETING

    19. Hi, admin, can I copy some information from the post?

    20. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post

    21. Nice post! I want to subscribe this blog, but I don’t know how to do, can anybody give me a hand?

    22. Fed up with obtaining low numbers of useless visitors for your site? Well i want to let you know about a fresh underground tactic which makes myself $900 per day on 100% AUTOPILOT. I really could be here all day and going into detail but why dont you just check their website out? There is really a excellent video that explains everything. So if your seriously interested in producing easy cash this is the site for you. Auto Traffic Avalanche

    Leave a Reply

    Spam Protection by WP-SpamFree