Managing Availibility of Tablespaces in Oracle

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.

We usually take tablespaces offline for maintenance purposes.

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 ica offline;

To again bring it back online give the following command.

SQL>alter tablespace ica online;

To take individual datafile offline type the following command

SQL>alter database datafile ‘/u01/oracle/ica/ica_tbs01.dbf’ offline;

Again to bring it back online give the following command

SQL> alter database datafile ‘/u01/oracle/ica/ica_tbs01.dbf’ online;

Note: You can’t take individual datafiles offline it the database is running in NOARCHIVELOG mode.  If the datafile has become corrupt or missing when the database is running in NOARCHIVELOG mode then you can only drop it by giving the following command

SQL>alter database datafile ‘/u01/oracle/ica/ica_tbs01.dbf’ offline for drop;

Making a Tablespace Read only.

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 ica read only

Again to make it read write

SQL>alter tablespace ica read write;

Renaming Tablespaces

Using the RENAME TO clause of the ALTER TABLESPACE, you can rename a permanent or temporary tablespace. For example, the following statement renames the users tablespace:

ALTER TABLESPACE users RENAME TO usersts;

The following affect the operation of this statement:

  • The COMPATIBLE parameter must be set to 10.0 or higher.
  • If the tablespace being renamed is the SYSTEM tablespace or the SYSAUX tablespace, then it will not be renamed and an error is raised.
  • If any datafile in the tablespace is offline, or if the tablespace is offline, then the tablespace is not renamed and an error is raised.

Dropping Tablespaces

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 DROP TABLESPACE 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 ica;

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

Disabling ARCHIVELOG mode

To disable ARCHIVELOG mode status, complete the following:

Procedure

  1. Log in as OS user, oracle and enter the following commands:
    $ export ORACLE_SID=<MYDB>

    where <MYDB> is the name of the database

    $ sqlplus /nolog
    SQL> connect / as sysdba
  2. To enable ARCHIVELOG mode status, enter the following SQL commands:
    SQL> Shutdown
    SQL> Startup mount
    SQL> Alter database noarchivelog;
    SQL> alter database open;
  3. To check the ARCHIVELOG mode status, enter the following SQL command:
    SQL> archive log list;
    Database log mode 						No Archive Mode
    Automatic archival 					Disabled
    Archive destination 					/oradump/<MYDB>
    Oldest online log sequence 		7
    Current log sequence 					9

Enabling ARCHIVELOG mode

To enable ARCHIVELOG mode status, complete the following steps:

  1. Log in as user oracle and enter the following commands:
    $ export ORACLE_SID=<MYDB>

    where <MYDB> is the name of the database

    $ sqlplus /nolog
    SQL> connect / as sysdba
  2. To enable ARCHIVELOG mode status, enter the following SQL commands:
    SQL> Shutdown
    SQL> Startup mount
    SQL> Alter database archivelog;
    SQL> alter database open;
  3. To check the ARCHIVELOG mode status, enter the following SQL command:
    SQL> archive log list;
    Database log mode 						Archive Mode
    Automatic archival 					Enabled
    Archive destination 					/oradump/<MYDB>
    Oldest online log sequence 		7
    Next log sequence to archive 	7
    Current log sequence 					9

Results

The database is now in ARCHIVELOG mode.

Find redo log members / redo log file size / redo log status

Find redo log file members.

SQL> col member format a50
SQL> select GROUP#,TYPE,MEMBER from v$logfile;

GROUP#       TYPE     MEMBER
----------  -------  --------------------------------------------------
 3           ONLINE  /oracle/openprod/origlogA/openprod/redo03a.log
 3           ONLINE  /oracle/openprod/mirrlogA/openprod/redo03b.log
 2           ONLINE  /oracle/openprod/origlogA/openprod/redo02a.log
 2           ONLINE  /oracle/openprod/mirrlogA/openprod/redo02b.log
 1           ONLINE  /oracle/openprod/origlogA/openprod/redo01a.log
 1           ONLINE  /oracle/openprod/mirrlogA/openprod/redo01b.log

To find redo log file size.

SQL> select GROUP#,THREAD#,SEQUENCE#,bytes/1024/1024,
MEMBERS,STATUS from v$log;

GROUP#      THREAD#   SEQUENCE#  BYTES/1024/1024 MEMBERS   STATUS
---------- ---------- ---------- --------------- ---------- ----------------
 1              1        10        100                 2       INACTIVE
 2              1        11        100                 2       INACTIVE
 3              1        12        100                 2       CURRENT

Combined view of both v$logfile & v$log.

Steps for Enabling/Disable ARCHIVELOG mode for SAP with Oracle

SQL> select log_mode from v$database;

LOG_MODE|
————
NOARCHIVELOG

  • Take a trace of controlfile for backup.

SERVER: sidadm 1> sqlplus “/as sysdba”

SQL> alter database backup controlfile to trace;
Database altered.
SQL> exit

  • Now stop sap and oracle database normally. Listener process need not to be stopped.

SERVER:sidadm 2> stopsap

  • Now we have to start oracle in mount stage.

    SERVER:orasid 1> sqlplus “/as sysdba”
    SQL> startup mount;

  • We will now change to archivelog mode for oracle and then will open the database.

SQL> alter database archivelog;
Database altered.

SQL> alter database open;
Database altered. 

  • Archivelog mode can be verified by command select log_mode from v$database
    SQL> select log_mode from v$database;
    LOG_MODE
    ————
    ARCHIVELOG
  • For enabling automatic archival we can use the following SQL commands
    SQL> alter system archive log start;
    System altered.
  • Now we can see that automatic archival is enabled

SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /oracle/SID/oraarch/SIDarch
Oldest online log sequence 28151
Next log sequence to archive 28154
Current log sequence 28154

  • Now we can start sap system.
    SERVER:sidadm 1> startsap

How to find the tables with no Primary Key in Oracle Database

This a Script that will help you find all the tables with no Primary Key in Oracle Database.

set echo on pages 1000;
break on owner;
select owner, table_name
from dba_tables a
where table_name not in
         (select table_name
          from dba_constraints
          where owner=a.owner
           and constraint_type=‘P’)
and owner not in (‘SYS’,‘SYSTEM’)
order by owner;