1. 程式人生 > >oracle rename數據文件的兩種方法

oracle rename數據文件的兩種方法

ati oar online 技術 off target pen 分類 alt

oracle rename數據文件的兩種方法

技術分享 分類:

第一種

alter tablespace users rename datafile ‘==‘ to ‘***‘;

這種方式需要數據庫處於open狀態,表空間在offline的狀態下才能更改。

[sql] view plain copy
  1. SQL> alter tablespace users rename datafile ‘/opt/ora10g/oradata/orcl/user0100.dbf‘,‘/opt/ora10g/oradata/orcl/user099.dbf‘ to ‘/opt/ora10g/oradata/orcl/userrename1.dbf‘,‘/opt/ora10g/oradata/orcl/userrename2.dbf‘;
  2. alter tablespace users rename datafile ‘/opt/ora10g/oradata/orcl/user0100.dbf‘,‘/opt/ora10g/oradata/orcl/user099.dbf‘ to ‘/opt/ora10g/oradata/orcl/userrename1.dbf‘,‘/opt/ora10g/oradata/orcl/userrename2.dbf‘
  3. *
  4. ERROR at line 1:
  5. ORA-01525: error in renaming data files
  6. ORA-01121: cannot rename database file 107 - file is in use or recovery
  7. ORA-01110: data file 107: ‘/opt/ora10g/oradata/orcl/user0100.dbf‘
  8. SQL> alter tablespace users offline;
  9. Tablespace altered.
  10. SQL> alter tablespace users rename datafile ‘/opt/ora10g/oradata/orcl/user0100.dbf‘,‘/opt/ora10g/oradata/orcl/user099.dbf‘ to ‘/opt/ora10g/oradata/orcl/userrename1.dbf‘,‘/opt/ora10g/oradata/orcl/userrename2.dbf‘;
  11. alter tablespace users rename datafile ‘/opt/ora10g/oradata/orcl/user0100.dbf‘,‘/opt/ora10g/oradata/orcl/user099.dbf‘ to ‘/opt/ora10g/oradata/orcl/userrename1.dbf‘,‘/opt/ora10g/oradata/orcl/userrename2.dbf‘
  12. *
  13. ERROR at line 1:
  14. ORA-01525: error in renaming data files
  15. ORA-01141: error renaming data file 107 - new file ‘/opt/ora10g/oradata/orcl/userrename1.dbf‘ not found
  16. ORA-01110: data file 107: ‘/opt/ora10g/oradata/orcl/user0100.dbf‘
  17. ORA-27037: unable to obtain file status
  18. Linux-x86_64 Error: 2: No such file or directory
  19. Additional information: 3
  20. SQL> !
  21. [[email protected] ~]$ cp /opt/ora10g/oradata/orcl/user0100.dbf /opt/ora10g/oradata/orcl/userrename1.dbf[[email protected] ~]$ cp /opt/ora10g/oradata/orcl/user099.dbf /opt/ora10g/oradata/orc
  22. l/userrename2.dbf
  23. [[email protected] ~]$ exit
  24. exit
  25. SQL> alter tablespace users rename datafile ‘/opt/ora10g/oradata/orcl/user0100.dbf‘,‘/opt/ora10g/oradata/orcl/user099.dbf‘ to ‘/opt/ora10g/oradata/orcl/userrename1.dbf‘,‘/opt/ora10g/oradata/orcl/userrename2.dbf‘;
  26. Tablespace altered.
  27. SQL> alter tablespace users online;
  28. Tablespace altered.

第二種

alter database rename file ‘===‘ to ‘***‘;

這種方式需要數據庫處於mount狀態

[sql] view plain copy
  1. SQL> startup mount
  2. ORACLE instance started.
  3. Total System Global Area 788529152 bytes
  4. Fixed Size 2087216 bytes
  5. Variable Size 423626448 bytes
  6. Database Buffers 356515840 bytes
  7. Redo Buffers 6299648 bytes
  8. Database mounted.
  9. SQL> alter database rename file ‘/opt/ora10g/oradata/orcl/userrename2.dbf‘,‘/opt/ora10g/oradata/orcl/userrename1.dbf‘ to ‘/opt/ora10g/oradata/orcl/user099.dbf‘,‘/opt/ora10g/oradata/orcl/user0100.dbf‘;
  10. Database altered.
  11. SQL> alter database open;
  12. alter database open
  13. *
  14. ERROR at line 1:
  15. ORA-01113: file 106 needs media recovery
  16. ORA-01110: data file 106: ‘/opt/ora10g/oradata/orcl/user099.dbf‘
  17. --這裏不能open的原因是剛剛關閉數據庫寫了userrename2.dbf和userrename1.dbf這兩個數據文件的scn,而user099.dbf和user0100.dbf的scn還是offline的時候的,這樣控制文件的頭和數據文件頭不一致,所以數據庫打不開。
  18. SQL> alter database rename file ‘/opt/ora10g/oradata/orcl/user099.dbf‘,‘/opt/ora10g/oradata/orcl/user0100.dbf‘ to ‘/opt/ora10g/oradata/orcl/userrename2.dbf‘,‘/opt/ora10g/oradata/orcl/userrename1.dbf‘;
  19. Database altered.
  20. SQL> alter database open;
  21. Database altered.

另外附上批量修改數據文件名的語句

[sql] view plain copy
  1. set pagesize 999
  2. set linesize 999
  3. select ‘alter database rename file ‘||‘‘‘‘||member||‘‘‘‘||‘ to ‘||chr(39)||replace(member,‘/paic/hq/bk/restore/data/oradata/lass/‘,‘/paic/z4ah8020/stg/lass/oradata/hs03lass/‘)||‘‘‘;‘
  4. from v$logfile
  5. where member like ‘/paic/hq/bk/restore/data/oradata/lass/%‘;
  6. select ‘alter database rename file ‘||‘‘‘‘||name||‘‘‘‘||‘ to ‘||chr(39)||replace(name,‘/paic/hq/bk/restore/data/oradata/lass/‘,‘/paic/z4ah8020/stg/lass/oradata/hs03lass/‘)||‘‘‘;‘
  7. from v$datafile
  8. where name like ‘/paic/hq/bk/restore/data/oradata/lass/%‘
  9. select ‘alter database rename file ‘||‘‘‘‘||name||‘‘‘‘||‘ to ‘||chr(39)||replace(name,‘/paic/hq/bk/restore/data/oradata/lass/‘,‘/paic/z4ah8020/stg/lass/oradata/hs03lass/‘)||‘‘‘;‘
  10. from v$tempfile
  11. where name like ‘/paic/hq/bk/restore/data/oradata/lass/%‘

oracle rename數據文件的兩種方法