当前位置:Gxlcms > mysql > 转载-HowtorenameeverythingonOracleDatabase(re

转载-HowtorenameeverythingonOracleDatabase(re

时间:2021-07-01 10:21:17 帮助过:20人阅读

看到一遍关于oracle rename 语法的总结文章,特转载于此原文地址 Renaming redolog members; Renaming tablespaces; Renaming datafiles of a single offline tablespace; Renaming constraints; Renaming schema objects (tables, views, sequences, privat

看到一遍关于oracle rename 语法的总结文章,特转载于此原文地址

  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.
oracle_rename

人气教程排行