Thursday, March 13, 2014

How to rename everything on Oracle Database (redolog members, tablespaces, schema objects, PDBs, partitions, constraints)

Few days ago I was wondering how many RENAME clauses are available on Oracle Database. I have summarized them here:
  1. Renaming redolog members;
  2. Renaming tablespaces;
  3. Renaming datafiles of a single offline tablespace;
  4. Renaming constraints;
  5. Renaming schema objects (tables, views, sequences, private synonyms, indexes, triggers);
  6. Renaming table columns;
  7. Renaming table and index partitions (subpartitions);
  8. Restoring and Renaming table from the Recycle Bin;
  9. Changing the domain in a global database name for a CDB;
  10. Renaming a PDB;
Let's review them with few examples.

  • Renaming redolog members:
To complete this requirement you have to shutdown the database, move the redo log files to the new destination, startup the database in mount mode, rename the log members and then open the database.
[oracle@localhost admin]$ sqlplus / as sysdba

SQL*Plus: Release 12.1.0.1.0 Production on Wed Mar 12 16:16:04 2014

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> set lines 180                        
SQL> col member format a50
SQL> select GROUP#, MEMBER, CON_ID from V$LOGFILE;

    GROUP# MEMBER        CON_ID
---------- -------------------------------------------------- ----------
  1 /app/oracle/oradata/CDB001/redo01.log         0
  2 /app/oracle/oradata/CDB001/redo02.log         0
  3 /app/oracle/oradata/CDB001/redo03.log         0

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> host mv /app/oracle/oradata/CDB001/redo01.log /app/oracle/oradata/CDB001/redo01_renamed.log

SQL> host mv /app/oracle/oradata/CDB001/redo02.log /app/oracle/oradata/CDB001/redo02_renamed.log

SQL> host mv /app/oracle/oradata/CDB001/redo03.log /app/oracle/oradata/CDB001/redo03_renamed.log

SQL> host ls -l /app/oracle/oradata/CDB001/*log
-rw-r-----. 1 oracle oinstall 52429312 Feb  7 15:59 /app/oracle/oradata/CDB001/redo01_renamed.log
-rw-r-----. 1 oracle oinstall 52429312 Mar  3 13:00 /app/oracle/oradata/CDB001/redo02_renamed.log
-rw-r-----. 1 oracle oinstall 52429312 Mar 12 16:17 /app/oracle/oradata/CDB001/redo03_renamed.log

SQL> startup mount;
ORACLE instance started.

Total System Global Area  626327552 bytes
Fixed Size      2291472 bytes
Variable Size    473958640 bytes
Database Buffers   146800640 bytes
Redo Buffers      3276800 bytes
Database mounted.
SQL> alter database rename file '/app/oracle/oradata/CDB001/redo01.log','/app/oracle/oradata/CDB001/redo02.log','/app/oracle/oradata/CDB001/redo03.log'
  2  to '/app/oracle/oradata/CDB001/redo01_renamed.log','/app/oracle/oradata/CDB001/redo02_renamed.log','/app/oracle/oradata/CDB001/redo03_renamed.log';

Database altered.

SQL> alter database open;

Database altered.

SQL> select GROUP#, MEMBER, CON_ID from V$LOGFILE;

    GROUP# MEMBER        CON_ID
---------- -------------------------------------------------- ----------
  1 /app/oracle/oradata/CDB001/redo01_renamed.log        0
  2 /app/oracle/oradata/CDB001/redo02_renamed.log        0
  3 /app/oracle/oradata/CDB001/redo03_renamed.log        0
  • Renaming tablespaces:
You can rename a permanent or temporary tablespace using the ALTER TABLESPACE RENAME statement. Just remember that you cannot rename SYSTEM or SYSAUX tablespace:
SQL> select a.con_id, a.name, b.name from v$containers a, v$tablespace b where a.con_id = b.con_id order by 1,3;

    CON_ID NAME      NAME
---------- ------------------------------ ------------------------------
  1 CDB$ROOT     SYSAUX
  1 CDB$ROOT     SYSTEM
  1 CDB$ROOT     TEMP
  1 CDB$ROOT     UNDOTBS1
  1 CDB$ROOT     USERS
  2 PDB$SEED     SYSAUX
  2 PDB$SEED     SYSTEM
  2 PDB$SEED     TEMP
  3 PDB001     SYSAUX
  3 PDB001     SYSTEM
  3 PDB001     TEMP
  3 PDB001     USERS
  4 PDB002     SYSAUX
  4 PDB002     SYSTEM
  4 PDB002     TEMP
  4 PDB002     USERS
  5 PDB003     SYSAUX
  5 PDB003     SYSTEM
  5 PDB003     TEMP
  5 PDB003     USERS

20 rows selected.

SQL> show con_name    

CON_NAME
------------------------------
CDB$ROOT
SQL> alter tablespace USERS rename to USERS_CDBROOT;

Tablespace altered.

SQL> select a.con_id, a.name, b.name from v$containers a, v$tablespace b where a.con_id = b.con_id order by 1,3;

    CON_ID NAME      NAME
---------- ------------------------------ ------------------------------
  1 CDB$ROOT     SYSAUX
  1 CDB$ROOT     SYSTEM
  1 CDB$ROOT     TEMP
  1 CDB$ROOT     UNDOTBS1
  1 CDB$ROOT     USERS_CDBROOT
  2 PDB$SEED     SYSAUX
  2 PDB$SEED     SYSTEM
  2 PDB$SEED     TEMP
  3 PDB001     SYSAUX
  3 PDB001     SYSTEM
  3 PDB001     TEMP
  3 PDB001     USERS
  4 PDB002     SYSAUX
  4 PDB002     SYSTEM
  4 PDB002     TEMP
  4 PDB002     USERS
  5 PDB003     SYSAUX
  5 PDB003     SYSTEM
  5 PDB003     TEMP
  5 PDB003     USERS

20 rows selected.

SQL> alter session set container=PDB001;

Session altered.

SQL> alter pluggable database PDB001 open;

Pluggable database altered.

SQL> alter tablespace USERS rename to USERS_PDB001;

Tablespace altered.

SQL> select a.con_id, a.name, b.name from v$containers a, v$tablespace b where a.con_id = b.con_id order by 1,3;

    CON_ID NAME      NAME
---------- ------------------------------ ------------------------------
  3 PDB001     SYSAUX
  3 PDB001     SYSTEM
  3 PDB001     TEMP
  3 PDB001     USERS_PDB001
  • Renaming datafiles of a single offline tablespace:
While the database is open, put the tablespace offline, rename the datafile at the operating system level, rename the datafile at the database level and finally take the tablespace online again.
SQL> col file_name format a50                                    
SQL> select file_name from dba_data_files where tablespace_name = 'USERS';

FILE_NAME
--------------------------------------------------
/app/oracle/oradata/CDB001/users01.dbf

SQL> alter tablespace USERS offline;

Tablespace altered.

SQL> host mv /app/oracle/oradata/CDB001/users01.dbf /app/oracle/oradata/CDB001/users01_renamed.dbf

SQL> host ls -l /app/oracle/oradata/CDB001/users01*   
-rw-r-----. 1 oracle oinstall 5251072 Mar 12 16:36 /app/oracle/oradata/CDB001/users01_renamed.dbf

SQL> alter tablespace USERS rename datafile '/app/oracle/oradata/CDB001/users01.dbf'
  2  to '/app/oracle/oradata/CDB001/users01_renamed.dbf';

Tablespace altered.

SQL> alter tablespace USERS online;

Tablespace altered.

SQL> select file_name from dba_data_files where tablespace_name = 'USERS';

FILE_NAME
--------------------------------------------------
/app/oracle/oradata/CDB001/users01_renamed.dbf
To rename datafiles included in multiple tablespaces follow the redo log file renaming procedure described above (alter database rename file ...).

  • Renaming constraints:
You can rename any constraint defined on a table
SQL> show user;
USER is "MARCOV"

SQL> select dbms_metadata.get_ddl('TABLE', 'T1') from dual;

DBMS_METADATA.GET_DDL('TABLE','T1')
---------------------------------------------------------

  CREATE TABLE "MARCOV"."T1"
   ( "A" NUMBER
   ) SEGMENT CREATION IMMEDIATE


SQL> alter table T1 add constraint t1_mypk primary key (a);

Table altered.

SQL> select dbms_metadata.get_ddl('TABLE', 'T1') from dual;

DBMS_METADATA.GET_DDL('TABLE','T1')
---------------------------------------------------------

  CREATE TABLE "MARCOV"."T1"
   ( "A" NUMBER,
  CONSTRAINT "T1_MYPK" PRIMARY KEY ("A")


SQL> alter table T1 rename constraint T1_MYPK to T1_PK; 

Table altered.

SQL> select dbms_metadata.get_ddl('TABLE', 'T1') from dual;

DBMS_METADATA.GET_DDL('TABLE','T1')
---------------------------------------------------------

  CREATE TABLE "MARCOV"."T1"
   ( "A" NUMBER,
  CONSTRAINT "T1_PK" PRIMARY KEY ("A")
  • Renaming schema objects (tables, views, sequences, private synonyms, indexes, triggers):
You can rename tables, views, sequences and private synonym using the rename statement.
SQL> show user;
USER is "MARCOV"

SQL> create sequence T1_MYSEQ;

Sequence created.

SQL> rename T1_MYSEQ to T1_S001;

Table renamed.

SQL> create table mysecondtable (a number);

Table created.

SQL> rename mysecondtable to T2;

Table renamed.

SQL> select dbms_metadata.get_ddl('TABLE', 'T2') from dual;

DBMS_METADATA.GET_DDL('TABLE','T2')
---------------------------------------------------------

  CREATE TABLE "MARCOV"."T2"
   ( "A" NUMBER
   ) SEGMENT CREATION DEFERRED

SQL> create or replace view T1_MYVIEW as select * from T1 where a <= 10;

View created.

SQL> rename T1_MYVIEW to T1_VIEW;

Table renamed.

SQL> select dbms_metadata.get_ddl('VIEW', 'T1_VIEW') from dual;

DBMS_METADATA.GET_DDL('VIEW','T1_VIEW')
---------------------------------------------------------

  CREATE OR REPLACE FORCE EDITIONABLE VIEW "MARCOV"."T1_VIEW" ("A") AS
  select "A" from T1 where a <= 10

SQL> create public synonym pub_t1 for t1;

Synonym created.

SQL> create synonym priv_t1 for t1;

Synonym created.

As you can see it is not possible to rename public synonymns, just the privates.
SQL> rename pub_t1 to public_t1;
rename pub_t1 to public_t1
*
ERROR at line 1:
ORA-04043: object PUB_T1 does not exist


SQL> rename priv_t1 to private_t1;

Table renamed.

Synonym of a renamed object returns instead an error when used:
SQL> select count(*) from private_t1;

  COUNT(*)
----------
  1

SQL> rename t1 to t1_renamed;

Table renamed.

SQL> select count(*) from private_t1;
select count(*) from private_t1
                     *
ERROR at line 1:
ORA-00980: synonym translation is no longer valid


SQL> rename t1_renamed to t1;

Table renamed.

SQL> select count(*) from private_t1;

  COUNT(*)
----------
  1
To rename schema objects such as indexes and triggers you can use the ALTER ... RENAME statement
SQL> show con_name;

CON_NAME
------------------------------
PDB001
SQL> show user
USER is "SYS"
SQL> select index_name from dba_indexes where table_name = 'T1';

INDEX_NAME
----------------------------------------
T1_MYPK

SQL> alter index MARCOV.T1_MYPK rename to T1_INDEX_PK;

Index altered.

SQL> create or replace trigger marcov.t1_mytrigger 
  2  before insert
  3  on marcov.t1
  4  for each row
  5  declare
  6  i number;
  7  begin 
  8  i := 0;
  9  end;
 10  /

Trigger created.

SQL> alter trigger marcov.t1_mytrigger rename to t1_trigger;

Trigger altered.

SQL> select owner, trigger_name from dba_triggers where trigger_name = 'T1_TRIGGER';

OWNER       TRIGGER_NAME
-------------------- ----------------------------------------
MARCOV       T1_TRIGGER
  • Renaming table columns:
It's possible to rename existing columns of a table using the ALTER TABLE ... RENAME COLUMN statement
SQL> alter table t1 rename column a to b;

Table altered.
  • Renaming table and index partitions (subpartitions):
The same RENAME TO statement could be applied to table or index partitions as in the following examples:
SQL> alter table t1 add (a number);

Table altered.

SQL> create index T1_index_partitioned on T1 (a)
  2  global partition by range (a)
  3  (partition p1 values less than (10),
  4  partition p2 values less than (100),
  5  partition p3 values less than (maxvalue));

Index created.

SQL> alter index T1_index_partitioned rename partition p3 to pmax;

Index altered.


SQL> drop table t2 purge;

Table dropped.

SQL> create table T2 (a number, quarter date) partition by range (quarter) 
  2  (partition Q1_2012 values less than (to_date('01/04/2012','DD/MM/YYYY')),
  3  partition Q2_2012 values less than (to_date('01/07/2012','DD/MM/YYYY')),
  4  partition Q3_2012 values less than (to_date('01/10/2012','DD/MM/YYYY')),
  5  partition Q4_2012 values less than (to_date('01/01/2013','DD/MM/YYYY')),
  6  partition Q1_2013 values less than (to_date('01/04/2013','DD/MM/YYYY')),
  7  partition Q2_2013 values less than (to_date('01/07/2013','DD/MM/YYYY')),
  8  partition Q3_2013 values less than (to_date('01/10/2013','DD/MM/YYYY')),
  9  partition Q4_2013 values less than (to_date('01/01/2014','DD/MM/YYYY')),
 10  partition Q1_2014 values less than (to_date('01/04/2014','DD/MM/YYYY')),
 11  partition Q2_2014 values less than (to_date('01/07/2014','DD/MM/YYYY')),
 12  partition Q3_2014 values less than (to_date('01/10/2014','DD/MM/YYYY')),
 13  partition Q4_2014 values less than (maxvalue));

Table created.

SQL> alter table t2 rename partition Q4_2014 to Q_MAX;

Table altered.
  • Restoring and Renaming table from the Recycle Bin:
You have a dropped table, it is still available in the recycle bin and you want to recover it using the FLASHBACK TABLE ... TO BEFORE DROP statement. With the clause RENAME TO you can rename the original table name and assign a new one during the recovery process.
SQL> show user
USER is "MARCOV"
SQL> select * from tab;

no rows selected

SQL> create table T1 (a number);

Table created.

SQL> drop table t1;

Table dropped.

SQL> create table T1 (a number);

Table created.

SQL> select * from tab;

TNAME      TABTYPE  CLUSTERID
---------------------------------------- ------- ----------
BIN$9IBy6OCMQ0zgRQAAAAAAAQ==$0   TABLE
T1      TABLE

SQL> show recyclebin
ORIGINAL NAME  RECYCLEBIN NAME  OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
T1   BIN$9IBy6OCMQ0zgRQAAAAAAAQ==$0 TABLE      2014-03-13:17:32:48
SQL> flashback table "BIN$9IBy6OCMQ0zgRQAAAAAAAQ==$0" to before drop rename to T2;

Flashback complete.

SQL> select * from tab;

TNAME      TABTYPE  CLUSTERID
---------------------------------------- ------- ----------
T1      TABLE
T2      TABLE

SQL> show recyclebin
SQL>
An equivalent statement to recover and rename the same table could be: flashback table T1 to before drop rename to T2; 
Don't forget that double quotes are required when dealing with system generated names such as BIN$9IBy6OCMQ0zgRQAAAAAAAQ==$0.
Dependent objects of a restored table from the recycle bin such as indexes mantains the system generated names, but you can rename them using the ALTER INDEX ... RENAME TO statement described above in the "Renaming Schema Objects" section.

  • Changing the domain in a global database name for a CDB:
It's possible to modify the domain of a global database name using the ALTER DATABASE RENAME GLOBAL_NAME TO database_name.network_domain_name statement
SQL> select * from global_name;

GLOBAL_NAME
------------------------------------------------------
CDB001.MARCOV.COM

SQL> alter database rename global_name to CDB001.IT.MARCOV.COM;

Database altered.

SQL> select * from global_name;

GLOBAL_NAME
------------------------------------------------------
CDB001.IT.MARCOV.COM
Also the domain of each PDBs is affected when the previous statement is applied to the domain name of a CDB.

  • Renaming a PDB:
For a pluggable database you cannot modify the domain name directly. When you only want to change the name of a specific PDB you can use the ALTER PLUGGABLE DATABASE RENAME GLOBAL_NAME TO statement. The pluggable database must be open in restricted mode. 
SQL> alter session set container=PDB001;

Session altered.

SQL> select * from global_name;
select * from global_name
              *
ERROR at line 1:
ORA-01219: database or pluggable database not open: queries allowed on fixed
tables or views only

SQL> alter pluggable database PDB001 open;

Pluggable database altered.

SQL> select * from global_name;

GLOBAL_NAME
------------------------------------------------------
PDB001.IT.MARCOV.COM

SQL> alter pluggable database rename global_name to PDB001.ROME.IT.MARCOV.COM;
alter pluggable database rename global_name to PDB001.ROME.IT.MARCOV.COM
                                               *
ERROR at line 1:
ORA-65045: pluggable database not in a restricted mode


SQL> alter pluggable database PDB001 close;

Pluggable database altered.

SQL> alter pluggable database PDB001 open restricted;

Pluggable database altered.

SQL> select * from global_name;

GLOBAL_NAME
------------------------------------------------------
PDB001.IT.MARCOV.COM

SQL> alter pluggable database rename global_name to PDB001.ROME.IT.MARCOV.COM;
alter pluggable database rename global_name to PDB001.ROME.IT.MARCOV.COM
                                               *
ERROR at line 1:
ORA-65042: name is already used by an existing container


SQL> alter pluggable database rename global_name to PDB001_ROME.IT.MARCOV.COM;

Pluggable database altered.

SQL> select name, open_mode from V$PDBS;

NAME          OPEN_MODE
------------------------------ ----------
PDB001_ROME         READ WRITE

SQL> select * from global_name;

GLOBAL_NAME
------------------------------------------------------
PDB001_ROME.IT.MARCOV.COM

That's all.


9 comments:

Karla Bathrick said...

great post very informational blog keep it up I have some submit date base information if your want to read visit our website
trevor attridge

udhaya kumar said...

easy to understand really clear explanation thanks lot!!!!
Visit Oracle Training

Coach Factory said...

oakley sunglasses, oakley vault, jordan shoes, polo ralph lauren outlet, burberry outlet online, michael kors outlet online, true religion, christian louboutin shoes, tiffany and co jewelry, louis vuitton outlet, gucci handbags, christian louboutin outlet, nike shoes, michael kors outlet online, longchamp outlet, tory burch outlet, coach outlet, red bottom shoes, louboutin shoes, ray ban sunglasses, coach purses, michael kors outlet store, air max, louis vuitton outlet online, cheap oakley sunglasses, coach outlet store online, coach factory outlet, prada handbags, chanel handbags, ray ban outlet, longchamp outlet online, kate spade outlet online, burberry outlet online, louis vuitton, polo ralph lauren, nike free, prada outlet, louis vuitton handbags, michael kors outlet online, michael kors outlet, michael kors outlet online, louis vuitton outlet, kate spade handbags, longchamp handbags, nike air max

Coach Factory said...

barbour, oakley pas cher, abercrombie and fitch, nike air force, mac cosmetics, ray ban uk, air max, guess pas cher, vans outlet, hermes pas cher, michael kors uk, nike roshe, ralph lauren pas cher, timberland, hollister, longchamp pas cher, sac michael kors, lululemon, sac vanessa bruno, michael kors canada, sac louis vuitton, mulberry, north face pas cher, ray ban pas cher, longchamp, converse pas cher, chaussure louboutin, nike blazer pas cher, longchamp, nike trainers, nike roshe run, lacoste pas cher, louis vuitton uk, scarpe hogan, nike huarache, tn pas cher, louis vuitton, hollister, louis vuitton pas cher, north face, ralph lauren, nike air max, new balance pas cher, vans pas cher, roshe run, abercrombie and fitch, nike free, nike free pas cher, hollister, burberry pas cher

Coach Factory said...

north face outlet, bottega veneta, rolex watches, canada goose, reebok outlet, north face jackets, moncler outlet, beats headphones, ugg boots, nfl jerseys, marc jacobs outlet, chi flat iron, canada goose uk, babyliss pro, insanity workout, timberland shoes, birkin bag, canada goose pas cher, giuseppe zanotti, lululemon outlet, jimmy choo shoes, soccer jerseys, canada goose outlet, new balance outlet, asics shoes, canada goose outlet, celine handbags, moncler, ugg outlet, moncler, mont blanc pens, instyler ionic styler, iphone 6 case, hollister clothing, ghd, p90x workout, ugg soldes, soccer shoes, uggs on sale, nike air max, herve leger, wedding dresses, canada goose outlet, baseball bats, mcm handbags, moncler, ferragamo shoes, valentino shoes, uggs outlet, ugg

oakleyses said...

voakley sunglasses, prada handbags, oakley sunglasses, longchamp handbags, longchamp handbags, louboutin shoes, louis vuitton handbags, coach factory outlet, tiffany and co, coach purses, louis vuitton outlet, polo ralph lauren outlet, air max, prada outlet, longchamp outlet, oakley sunglasses cheap, ray ban sunglasses, louboutin outlet, michael kors outlet, michael kors outlet, tiffany and co, burberry outlet, christian louboutin shoes, coach outlet store online, jordan shoes, polo ralph lauren outlet, louboutin, kate spade handbags, michael kors outlet, coach outlet, air max, gucci outlet, michael kors outlet, ray ban sunglasses, chanel handbags, michael kors outlet, tory burch outlet, nike free, kate spade outlet, louis vuitton outlet, burberry outlet, louis vuitton outlet stores, louis vuitton, nike shoes, michael kors outlet

oakleyses said...

air max, hollister, true religion outlet, nike blazer, louboutin, ray ban sunglasses, polo ralph lauren, michael kors, true religion jeans, sac guess, sac longchamp, hogan outlet, ralph lauren, vans pas cher, sac louis vuitton, air max pas cher, nike free pas cher, nike free, air max, mulberry, nike roshe run, sac burberry, hollister, vanessa bruno, louis vuitton, lululemon, michael kors pas cher, oakley pas cher, air jordan, ray ban pas cher, new balance pas cher, polo lacoste, converse pas cher, north face, sac louis vuitton, michael kors, sac hermes, nike tn, timberland, louis vuitton uk, longchamp, true religion jeans, nike air max, air force, north face

oakleyses said...

abercrombie and fitch, instyler, ghd, bottega veneta, ugg boots, jimmy choo outlet, soccer shoes, ugg pas cher, herve leger, beats by dre, birkin bag, abercrombie and fitch, north face jackets, soccer jerseys, mont blanc, rolex watches, lululemon outlet, celine handbags, nike roshe run, nike trainers, giuseppe zanotti, hollister, wedding dresses, nike huarache, mcm handbags, vans shoes, chi flat iron, babyliss pro, north face outlet, nike roshe, ugg australia, ugg, marc jacobs, barbour, nfl jerseys, p90x, new balance shoes, asics running shoes, ferragamo shoes, mac cosmetics, insanity workout, uggs outlet, reebok outlet, longchamp, valentino shoes

oakleyses said...

converse, air max, gucci, canada goose, juicy couture outlet, canada goose, wedding dresses, moncler, ralph lauren, lancel, montre homme, moncler, louboutin, oakley, karen millen, vans, coach outlet store online, air max, canada goose jackets, ugg, hollister clothing store, louis vuitton, baseball bats, hollister, rolex watches, juicy couture outlet, iphone 6 cases, canada goose uk, canada goose outlet, ugg, moncler, moncler outlet, timberland boots, hollister, supra shoes, moncler, canada goose, converse shoes, toms shoes, moncler, moncler, canada goose, ugg boots, ray ban, parajumpers, canada goose