How to Create a Tablespace in Oracle Database

 

Prerequisites

Before creating a tablespace, ensure you have the following:

  • Oracle Database Installed: This guide assumes Oracle Database 11g or later.
  • DBA (Database Administrator) Privileges: You need to have sufficient privileges to create a tablespace.
  • Sufficient Disk Space: Ensure there is enough disk space for the new tablespace.

Step-by-Step Guide to Creating a Tablespace

1. Connect to Oracle Database

First, connect to your Oracle Database using SQL*Plus or another SQL client (like Oracle SQL Developer).


sqlplus sys as sysdba

Enter your password to log in.

2. Check Existing Tablespaces

Before creating a new tablespace, it’s useful to check the existing ones. Run the following query to list the current tablespaces in your Oracle database.


SELECT tablespace_name FROM dba_tablespaces;

This will return a list of all the tablespaces in the database.

3. Create the Tablespace

To create a tablespace, use the CREATE TABLESPACE command. Below is the syntax to create a basic tablespace.


CREATE TABLESPACE <tablespace_name> DATAFILE '<file_path>/<filename>.dbf' SIZE <size> [K | M | G] AUTOEXTEND ON NEXT <increment> [K | M | G] MAXSIZE <max_size> [K | M | G];
Example:

Let’s create a tablespace called USER_DATA with a 500MB data file and enable auto-extension to handle growth:


CREATE TABLESPACE USER_DATA DATAFILE '/u01/app/oracle/oradata/mydb/user_data01.dbf' SIZE 500M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED;
  • DATAFILE: Specifies the path where the tablespace data file will be stored.
  • SIZE: Initial size of the data file (in this case, 500MB).
  • AUTOEXTEND: Allows the data file to automatically grow when more space is needed.
  • MAXSIZE: The maximum size the data file can grow to. In this example, it is unlimited.

4. Verify the Tablespace Creation

Once the tablespace is created, you can verify its existence by querying the dba_tablespaces table.


SELECT tablespace_name FROM dba_tablespaces WHERE tablespace_name = 'USER_DATA';

If the tablespace is created successfully, it will appear in the results.

5. Assign a Default Storage Location

You may want to assign the newly created tablespace as the default tablespace for users. Use the following command:


ALTER USER <username> DEFAULT TABLESPACE USER_DATA;

This command sets USER_DATA as the default tablespace for the specified user.

6. Add Datafiles (Optional)

In some cases, you may want to add additional data files to the tablespace to increase storage capacity. To do this, you can use the ALTER TABLESPACE command:


ALTER TABLESPACE USER_DATA ADD DATAFILE '/u01/app/oracle/oradata/mydb/user_data02.dbf' SIZE 500M;

This adds a second datafile to the USER_DATA tablespace.

Managing Tablespaces in Oracle

1. Altering a Tablespace

If you need to resize or change the properties of an existing tablespace, you can use the ALTER TABLESPACE command.


ALTER TABLESPACE USER_DATA RESIZE 1G;

This resizes the tablespace to 1GB.

2. Dropping a Tablespace

To drop a tablespace, use the DROP TABLESPACE command. Be cautious, as this operation will remove all data associated with the tablespace.


DROP TABLESPACE USER_DATA INCLUDING CONTENTS AND DATAFILES;
  • INCLUDING CONTENTS: Removes all objects within the tablespace.
  • AND DATAFILES: Deletes the data files associated with the tablespace.

3. Checking Tablespace Usage

To monitor the usage of a tablespace, you can run a query on the dba_data_files and v$tablespace views:


SELECT tablespace_name, file_name, bytes/1024/1024 "Size (MB)" FROM dba_data_files;

This query will return the current size of each tablespace in MB.

Useful srvctl commands

Useful srvctl commands for ORACLE RAC MAINTENANCE

srvctl -help or srvctl -v


srvctl commands to startup database:

srvctl start database -d db_name [-o start_options] [-c connect_str|-q]
srvctl start database -d db_name [-o open]
srvctl start database -d db_name -o nomount
srvctl start database -d db_name -o mount

srvctl commands to shutdown database:

srvctl stop database -d db_name [-o stop_options] [-c connect_str|-q]
srvctl stop database -d db_name [-o normal]
srvctl stop database -d db_name -o transactional
srvctl stop database -d db_name -o immediate
srvctl stop database -d db_name -o abort


srvctl config database
srvctl config database -d db_name [-a] [-t]


srvctl start listener -n node_name [-l listener_names]
srvctl stop listener -n node_name [-l listener_names]


srvctl status listener [-n node_name] [-l listener_names] 
srvctl config listener -n node_name

What causes Node eviction in Oracle RAC?

What is node eviction?

Node eviction in RAC is done when a heartbeat indicates that a node is not responding, 

and the evicted node is re-started to make it a part of cluster.


Causes for RAC node eviction:


Node eviction on Oracle RAC environment can be due to any of the below reasons. 

– A failure of any of the major hardware components (CPU, RAM, network interconnect).

– A server that is experiencing RAM swapping.

– When communications to the voting disk is interrupted, causing the disconnected node to be evicted and re-boot.

– Database or ASM hang condition.


Below is the list of important log files to review in case of a node eviction

– Clusterware alert log

– Database alert log

– CSSD agent logs

– CSSD monitor logs

– System Message logs (/var/log/messages)

What is Flashback feature in Oracle Database?

Oracle Flashback Database and restore points are related data protection features that enable you to rewind data back in time to correct any problems caused by logical data corruption or user errors within a designated time window.

Points to Remember

– Flashback Database command can be run either from SQLPLUS or using RMAN Utility.

– FLASHBACK DATABASE command can be used to rewind the database to a target time, SCN or a log sequence number.

– Flashback command works by undoing the changes made to the data files that exist when you run the command.

– Flashback can fix only logical failures, not physical failures.

– If the database control file is restored from backup or re-created, then all existing flashback log information is discarded.

– Avoid using FLASHBACK DATABASE with a target time or SCN that coincides with a NOLOGGING operation, it can cause block corruption.




How to enable Flashback Database feature in Oracle?

 Prerequisites for Flashback Database and Guaranteed Restore Points

Flashback Database

Configure the following database settings before enabling Flashback Database:

-Your database must be running in ARCHIVELOG mode.

-You must have a fast recovery area enabled.

-For Oracle Real Application Clusters (Oracle RAC) databases, the fast recovery area must be in a clustered file system or in ASM.

Guaranteed Restore Points

To use guaranteed restore points,the COMPATIBLE initialization parameter must be set to 10.2.0 or greater.


Steps to enable Flashback Database:

1. Connect to sqlplus as sysdba and set  the desired value for Flashback retention target using below command.


SQL>ALTER SYSTEM SET DB_FLASHBACK_RETENTION_TARGET=4320;

Here, Flashback retention target is set to window of 3 days(4320 Minutes), default value is 1 day (1440 Minutes).


2. Enable the Flashback Database feature for the whole database using the following command:

SQL>ALTER DATABASE FLASHBACK ON;


3.Use the following command to check if Flashback Database is enabled for your target database:

SQL>SELECT FLASHBACK_ON FROM V$DATABASE;



Connecting to a Database from SQLDeveloper fails with IO Error: The Network Adapter Could Not Establish The Connection

Issue: New Users were trying to access a 3-node RAC Oracle Database system using SQLDeveloper


Below is the connection string provided to user for Oracle Database Connection. RAC Virtual IP is provided to connect to Database.




TESTDB01=
       (DESCRIPTION=
               (ADDRESS=(PROTOCOL=tcp)(HOST=test001-vip1.domain.com)(PORT=1521))
               (ADDRESS=(PROTOCOL=tcp)(HOST=test002-vip1.domain.com)(PORT=1521))
               (ADDRESS=(PROTOCOL=tcp)(HOST=test003-vip1.domain.com)(PORT=1521))
           (CONNECT_DATA=
               (SERVICE_NAME=TESTDB01)
               (INSTANCE_NAME=TESTDB01)
           )
       )




User tried to connect using one of the VIP and got the below error in SQLDeveloper.
Status : Failure -Test failed: IO Error: The Network Adapter Could Not Establish The Connection.


Analysis:


Verified the Oracle Database Services and Listener Services are running fine.
Other Users were able to connect to SQLDeveloper using the same Connection String.


Solution:


User does not have network communication open to port 1521 on Oracle RAC Virtual IP.


ping  test001-vip1.domain.com
ping  test002-vip1.domain.com
ping  test003-vip1.domain.com


telnet test001-vip1.domain.com 1521
telnet test002-vip1.domain.com 1521
telnet test003-vip1.domain.com 1521


Ping to VIP address works fine but Telnet to port 1521 on the VIP address is giving “Connect Failed” Error.


When Network communication is opened on port 1521 on all the Oracle RAC Virtual IP servers/Addresses, User can connect to Oracle Database using SQLDeveloper.

Steps to create Database link from Oracle Database to Microsoft SQLServer Database:

Steps to create Database link from Oracle Database to Microsoft SQLServer Database:


Pre-requisites


1. Check Network communication is Opened from Oracle Database Server to MSSQL Database Server using telnet
2. Download the Oracle Gateway Server Software same as your Oracle Database version.


Installation of Oracle Gateway Software
=========


Hope you have downloaded the Dg4msql software for sql server 


1> Create a directory as gateway under $ORACLE_BASE directory. 


2) Invoke the GUI , Select Oracle Gateway for MYSQL and enter the sql server details at the time of installation 


3) Before completing the installation, OUI invokes the NETCA and create a new listener for gateway with new port (ex:1526) 


Note; In case if you have defined any bash profile for TNS_ADMIN, then please comment it 


4. Follow the below note for pre-req for gateway listener 


How to Configure DG4MSQL (Oracle Database Gateway for MS SQL Server) 64bit Unix OS (Linux, Solaris, AIX,HP-UX) post install ( Doc ID 562509.1 ) 


edit your gateway listener as recommended in the above note. 


5. create a tnsentry in $ORACLE_HOME/network/admin 


6. export TNS_ADMIN=$gateway home/network/admin 


lsnrctl stop <gateway listener> 


lsnrctl start <gateway listener> 


tnsping <gateway alias> 


conn / as sysdba 


create public database link <test> connect to “<sql server user>” identified by “<password>” using ‘<tnsnames.ora>’ 


select * from dual@test; 

Script to check free space and used space in Oracle Database tablespaces

select a.tablespace_name,
       a.bytes_alloc/(1024*1024) “TOTAL ALLOC (MB)”,
 a.physical_bytes/(1024*1024) “TOTAL PHYS ALLOC (MB)”,
       nvl(b.tot_used,0)/(1024*1024) “USED (MB)”,
       (nvl(b.tot_used,0)/a.bytes_alloc)*100 “% USED”
from ( select tablespace_name,
       sum(bytes) physical_bytes,
sum(decode(autoextensible,’NO’,bytes,’YES’,maxbytes)) bytes_alloc
       from dba_data_files
       group by tablespace_name ) a,
     ( select tablespace_name, sum(bytes) tot_used
       from dba_segments
group by tablespace_name ) b
where a.tablespace_name = b.tablespace_name (+)
–and   (nvl(b.tot_used,0)/a.bytes_alloc)*100 > 10
and   a.tablespace_name not in (select distinct tablespace_name from dba_temp_files)
and   a.tablespace_name not like ‘UNDO%’
order by 1
 –order by 5