Oracle DataBase Lesson 7 – Tablespaces & Data Files | Dataplexa

Tablespaces & Data Files

When you create a table in Oracle, you do not specify a file path. You specify a tablespace — a named, logical storage container. Oracle handles where on disk the data physically lands. This separation between logical and physical storage is one of Oracle's most important architectural features. It lets DBAs add storage, move data, and manage growth without changing a single line of application SQL. This lesson explains how tablespaces and data files work, which ones Oracle creates by default, and how to create and manage your own.

What Is a Tablespace?

A tablespace is a logical storage unit that groups related database objects — tables, indexes, sequences, and other schema objects. Every object in Oracle belongs to exactly one tablespace. A tablespace is made up of one or more physical data files stored on disk. The relationship is:

  • Schema objects (tables, indexes) → belong to a tablespace → stored in one or more data files

This three-level separation means you can add a new data file to grow a tablespace without touching any application code. You can move a tablespace to a faster disk by adding new data files and dropping old ones. You can restrict different applications to different tablespaces for easier management and monitoring.

-- dba_tablespaces lists every tablespace in the database
-- Requires DBA privilege — regular users should query user_tablespaces instead
-- contents: PERMANENT stores tables/indexes, TEMPORARY is for sorts, UNDO is for rollback
-- logging: NOLOGGING is used by TEMP — its contents are never written to the redo log
SELECT tablespace_name,
       status,
       contents,
       logging
FROM   dba_tablespaces
ORDER BY tablespace_name;

-- dba_data_files shows the physical .dbf files that back each tablespace
-- A tablespace can span multiple data files — each appears as a separate row here
-- bytes is stored in bytes by Oracle — divide by 1024 twice to convert to MB
-- autoextensible: YES means Oracle grows this file automatically when full
SELECT file_name,
       tablespace_name,
       ROUND(bytes    / 1024 / 1024, 2)  AS size_mb,
       autoextensible,
       ROUND(maxbytes / 1024 / 1024, 2)  AS max_size_mb
FROM   dba_data_files
ORDER BY tablespace_name;
-- dba_tablespaces:
TABLESPACE_NAME STATUS CONTENTS LOGGING
──────────────── ──────── ───────── ───────
SYSAUX ONLINE PERMANENT LOGGING
SYSTEM ONLINE PERMANENT LOGGING
TEMP ONLINE TEMPORARY NOLOGGING
UNDOTBS1 ONLINE UNDO LOGGING
USERS ONLINE PERMANENT LOGGING

-- dba_data_files:
FILE_NAME TABLESPACE_NAME SIZE_MB AUTOEXTENSIBLE MAX_SIZE_MB
───────────────────────────────────── ─────────────── ─────── ────────────── ───────────
/opt/oracle/oradata/XE/system01.dbf SYSTEM 870.00 YES 32767.98
/opt/oracle/oradata/XE/sysaux01.dbf SYSAUX 560.00 YES 32767.98
/opt/oracle/oradata/XE/undotbs01.dbf UNDOTBS1 215.00 YES 32767.98
/opt/oracle/oradata/XE/users01.dbf USERS 5.00 YES 32767.98
  • dba_tablespaces and dba_data_files are Oracle data dictionary views — they require DBA privileges; if you are connected as a regular user, use user_tablespaces instead
  • AUTOEXTENSIBLE = YES means Oracle will automatically grow the data file when it runs out of space, up to the MAX_SIZE_MB limit — this prevents the "tablespace full" error in development but should be configured carefully in production where disk capacity must be managed
  • The TEMP tablespace is used for sorting, hashing, and other temporary operations that exceed PGA memory — it is not used for permanent storage and its contents are never written to the redo log

Oracle's Default Tablespaces

Oracle XE creates five tablespaces automatically during installation. Each has a specific purpose and should not be used for your own application data.

Tablespace Type Purpose
SYSTEM Permanent Oracle's internal data dictionary — never store application data here
SYSAUX Permanent Auxiliary components such as AWR, Statspack, and Oracle Text
UNDOTBS1 Undo Stores old row versions for read consistency and ROLLBACK support
TEMP Temporary Sort and hash operations that overflow from PGA memory
USERS Permanent Default tablespace for user-created schema objects in XE

Creating a Tablespace

In a real project you create a dedicated tablespace for your application data rather than using the shared USERS tablespace. This makes storage management, monitoring, and backup easier. The CREATE TABLESPACE statement defines the tablespace name, its initial data file, and how it should grow.

-- Create a dedicated tablespace for DataplexaStore table data
-- SIZE allocates disk immediately; NEXT is the growth increment; MAXSIZE is the ceiling
CREATE TABLESPACE dataplexa_store_ts
DATAFILE '/opt/oracle/oradata/XE/dataplexa_store01.dbf'
SIZE 100M
AUTOEXTEND ON
NEXT 50M
MAXSIZE 2G
LOGGING
ONLINE;

-- Separate tablespace for indexes — allows table scans and index lookups to run
-- on different physical disks, reducing I/O contention
-- Indexes typically need less initial space than table data
CREATE TABLESPACE dataplexa_store_idx
DATAFILE '/opt/oracle/oradata/XE/dataplexa_store_idx01.dbf'
SIZE 50M
AUTOEXTEND ON
NEXT 25M
MAXSIZE 1G
LOGGING
ONLINE;

-- Dedicated tablespace for DataplexaHR — each schema gets its own for clean separation
CREATE TABLESPACE dataplexa_hr_ts
DATAFILE '/opt/oracle/oradata/XE/dataplexa_hr01.dbf'
SIZE 100M
AUTOEXTEND ON
NEXT 50M
MAXSIZE 2G
LOGGING
ONLINE;
Tablespace DATAPLEXA_STORE_TS created.
Tablespace DATAPLEXA_STORE_IDX created.
Tablespace DATAPLEXA_HR_TS created.
  • SIZE 100M sets the initial data file size — Oracle allocates this space immediately on disk when the tablespace is created
  • AUTOEXTEND ON NEXT 50M MAXSIZE 2G means the file grows by 50 MB each time it fills, up to a maximum of 2 GB — without AUTOEXTEND, a full tablespace causes all INSERT and UPDATE operations on its tables to fail with an ORA-01653 error
  • Separating table data and index data into different tablespaces is a production best practice — it allows each to be placed on different physical disks for better I/O performance and makes tablespace-level backups more granular

Adding a Data File and Monitoring Space

As data grows, a tablespace can be expanded in two ways: by enabling autoextend on an existing data file, or by adding a new data file to the tablespace. Adding a new data file is preferred in production because it gives more control over exactly where the new storage is placed.

-- Add a second data file to an existing tablespace
-- Preferred over AUTOEXTEND alone — gives full control over file location and size
-- Second file starts larger as the application has grown
ALTER TABLESPACE dataplexa_store_ts
ADD DATAFILE '/opt/oracle/oradata/XE/dataplexa_store02.dbf'
SIZE 200M
AUTOEXTEND ON
NEXT 100M
MAXSIZE 4G;

-- Monitor free space across all permanent tablespaces
-- Joins dba_data_files for total allocated size and dba_free_space for unused extents
-- TEMP and UNDO manage space differently so they are excluded with the WHERE filter
-- Run this regularly — a tablespace approaching 90% used needs immediate attention
SELECT ts.tablespace_name,
       ROUND(SUM(df.bytes) / 1024 / 1024, 2)       AS total_mb,
       ROUND(SUM(fs.bytes) / 1024 / 1024, 2)       AS free_mb,
       ROUND(
           (1 - SUM(fs.bytes) / SUM(df.bytes)) * 100
       , 2)                                          AS used_pct
FROM   dba_tablespaces  ts
JOIN   dba_data_files   df ON ts.tablespace_name = df.tablespace_name
JOIN   dba_free_space   fs ON ts.tablespace_name = fs.tablespace_name
WHERE  ts.contents = 'PERMANENT'
GROUP BY ts.tablespace_name
ORDER BY used_pct DESC;

-- INCLUDING CONTENTS removes all objects stored inside the tablespace
-- AND DATAFILES deletes the .dbf file from the OS — without it the file becomes orphaned on disk
DROP TABLESPACE dataplexa_store_idx
INCLUDING CONTENTS AND DATAFILES;
-- ALTER TABLESPACE:
Tablespace DATAPLEXA_STORE_TS altered.

-- Free space report:
TABLESPACE_NAME TOTAL_MB FREE_MB USED_PCT
──────────────────── ───────── ──────── ────────
SYSTEM 870.00 142.30 83.64
SYSAUX 560.00 198.40 64.57
DATAPLEXA_STORE_TS 300.00 248.10 17.30
DATAPLEXA_HR_TS 100.00 98.20 1.80
USERS 5.00 4.60 8.00

-- DROP TABLESPACE:
Tablespace DATAPLEXA_STORE_IDX dropped.
  • The free space query joins dba_data_files and dba_free_space — monitoring this regularly prevents surprises; a USED_PCT approaching 90% on a non-autoextend tablespace needs immediate attention
  • INCLUDING CONTENTS AND DATAFILES in the DROP statement removes both the Oracle tablespace definition and the physical .dbf file from disk — omitting AND DATAFILES removes the tablespace from Oracle but leaves orphaned files on disk that waste storage
  • You cannot drop a tablespace that contains objects currently in use — Oracle raises ORA-01549 if any session has an open transaction touching the tablespace's objects

Summary

Concept What It Is Key Point
Tablespace Logical storage container for schema objects Every table and index belongs to exactly one tablespace
Data file (.dbf) Physical file on disk that stores tablespace data One tablespace can span multiple data files
SYSTEM Oracle's internal data dictionary tablespace Never store application data here
UNDOTBS1 Undo tablespace — stores old row versions Required for read consistency and ROLLBACK
TEMP Temporary tablespace for sort and hash operations Used when PGA sort space is exceeded
AUTOEXTEND Automatic data file growth setting NEXT sets growth increment, MAXSIZE sets the ceiling
CREATE TABLESPACE Creates a new tablespace with a named data file Create a dedicated tablespace per application
dba_tablespaces / dba_data_files Data dictionary views for storage monitoring Join with dba_free_space to monitor usage percentage

Practice Questions

Practice 1. What is the relationship between a schema object, a tablespace, and a data file in Oracle?



Practice 2. What is the UNDOTBS1 tablespace used for and why does Oracle need it?



Practice 3. A tablespace has AUTOEXTEND OFF and its data file is 95% full. What will happen when the next INSERT runs and how do you fix it?



Practice 4. Why is it a best practice in production to create separate tablespaces for table data and index data?



Practice 5. What is the difference between DROP TABLESPACE dataplexa_store_ts and DROP TABLESPACE dataplexa_store_ts INCLUDING CONTENTS AND DATAFILES?



Quiz

Quiz 1. Which Oracle dictionary view lists all data files and the tablespaces they belong to?






Quiz 2. What does AUTOEXTEND ON NEXT 50M MAXSIZE 2G mean in a CREATE TABLESPACE statement?






Quiz 3. Which default Oracle tablespace should you use to store your application tables and why?






Quiz 4. What SQL statement adds a new data file to an existing tablespace?






Quiz 5. What is the SQL Server equivalent of an Oracle tablespace?






Next up — Tables, Rows & Columns — Create your first Oracle tables, understand how data is physically organised into rows and columns, and start building the DataplexaStore and DataplexaHR schemas used throughout this course.