SQL statements related to oracle database migration

I use PLSQL tool to do database migration. The example is to migrate the data of apps users of the same database to sccsrm users, which are all in the same database.

A common way of database migration is to use the command to export all data of a user and then import it into a new user.

DBA permissions are required to create users.

sqlplus /nolog
conn /as sysdba

1. Creating users and authorizations generally requires creating new and temporary tablespaces

----delete user
drop user sccsrm cascade;

----Temporary tablespace
create temporary tablespace SCCSRM_TMP_TS
tempfile '/u01/app/oracle/oradata/SRM/datafile/SCCSRM_TMP_TS.dbf'      
size 50m                                                     
autoextend on                                              
next 50m maxsize 20480m                            
extent management local;

----Datasheet space
create tablespace SCCSRM_DATA_TS
logging
datafile '/u01/app/oracle/oradata/SRM/datafile/SCCSRM_DATA_TS.dbf'      
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;

----Create user sccsrm
create user sccsrm identified by sccsrm default tablespace SCCSRM_DATA_TS temporary tablespace SCCSRM_TMP_TS;

----Tablespace authorized to sccsrm
alter user sccsrm quota unlimited on SCCSRM_DATA_TS;

----To grant authorization sccsrm
grant connect,resource to sccsrm;

---View the location of tablespaces
select * from dba_data_files;

---Modify tablespace dbf File, set to increase by 50 each time by default M,Infinite -- when there is insufficient table space
alter database datafile '/u01/app/oracle/oradata/SRM/datafile/SCCSRM_DATA_TS.dbf' autoextend on next 50M maxsize unlimited;

2. In the database migration step, you need to prepare the table list of imported Excel


-----1,Create tables for temporary tables sccsrm.TABLE_NAME_TEMP
create table sccsrm.TABLE_NAME_TEMP
(
  table_name VARCHAR2(100)
)
tablespace SCCSRM_DATA_TS;

-----2,Query current user apps All table names, exporting them as Excel,Be used for sccsrm User's temporary table sccsrm.table_name_temp Import data for
select t.table_name from user_tables t order by t.TABLE_NAME;

-----List of tables to be exported Excel Import sccsrm.table_name_temp
-----3,surface sccsrm.TABLE_NAME_TEMP Data De null
update sccsrm.table_name_temp t
set t.table_name=trim(t.table_name);


-----4,stay apps Under user login, the query authority of authorization table is given to sccsrm user
-----Authorization limited to sccsrm Query permission for
declare
  cursor vdata is select t.table_name from sccsrm.table_name_temp t order by t.TABLE_NAME;
  --Declare variables,Record the number of lines
  v          vdata%rowtype;
  v_sql varchar2(200);
begin
  --Open cursor
   open vdata;
   --Loop traversal data retrieval
   loop
     fetch vdata into v;
     exit when vdata%NOTFOUND;
     v_sql := 'grant select on apps.'||v.table_name||' to sccsrm';
     execute immediate v_sql;
   end loop;  
   --Close cursor
   close vdata;
end;

-----5,Copy table data - will apps Migration of table data under users to sccsrm Under user
declare
  cursor vdata is select t.table_name from sccsrm.table_name_temp t where not exists(select tt.TABLE_NAME from user_tables tt where tt.TABLE_NAME=t.table_name) order by t.TABLE_NAME;
  --Declare variables,Record the number of lines
  v          vdata%rowtype;
  v_sql varchar2(600);
  n number;
begin
  --Open cursor
   open vdata;
   --Loop traversal data retrieval
   loop
     fetch vdata into v;
     exit when vdata%NOTFOUND;
     /*dbms_output.put_line(v.table_name);*/
     v_sql := 'create table sccsrm.'||v.table_name||' as select * from apps.'||v.table_name;
     execute immediate v_sql;
   end loop;  
   --Close cursor
   close vdata;
end;

-----6,View this user apps All indexes under
select t.index_name,t.index_type,t.table_name from user_indexes t order by t.table_name;
-----Find out apps All indexes, exporting them as Excel,Again Excel Copy data to PLSQL,Then modify it to sccsrm User execution
SELECT dbms_lob.substr(dbms_metadata.get_ddl('INDEX', INDEX_NAME)) || ';' from dba_indexes where owner = 'APPS' and index_type='NORMAL';


-----7,stay apps Under user login, recycle to sccsrm Query permission for
declare
  cursor vdata is select t.table_name from sccsrm.table_name_temp t order by t.TABLE_NAME;
  --Declare variables,Record the number of lines
  v          vdata%rowtype;
  v_sql varchar2(200);
begin
  --Open cursor
   open vdata;
   --Loop traversal data retrieval
   loop
     fetch vdata into v;
     exit when vdata%NOTFOUND;
     v_sql := 'revoke select on apps.'||v.table_name||' from sccsrm';
     execute immediate v_sql;
   end loop;  
   --Close cursor
   close vdata;
end;


-----8,stay apps Under user login, query out apps All sequences under the user
SELECT 'create sequence sccsrm.'||SEQUENCE_NAME||' minvalue '||MIN_VALUE||' maxvalue '||MAX_VALUE||
' start with '||LAST_NUMBER||' increment by '||INCREMENT_BY||' cache '||' 100;'   
  FROM DBA_SEQUENCES   
WHERE SEQUENCE_OWNER = 'APPS';
-----View all sequences for the current user
select * from user_sequences t;


-----9,stay apps Under user login, delete the table and its data—— apps For the old data of users, it is strongly recommended to back up the data and then delete it to avoid data loss caused by misoperation
declare
  cursor vdata is select t.table_name from sccsrm.table_name_temp t order by t.TABLE_NAME;
  --Declare variables,Record the number of lines
  v          vdata%rowtype;
  v_sql varchar2(600);
begin
  --Open cursor
   open vdata;
   --Loop traversal data retrieval
   loop
     fetch vdata into v;
     exit when vdata%NOTFOUND;
     /*dbms_output.put_line(v.table_name);*/
     v_sql := 'drop table apps.'||v.table_name;
     execute immediate v_sql;
   end loop;  
   --Close cursor
   close vdata;
end;
---View empty table
select 'alter table '||table_name||' allocate extent;'from user_tables WHERE SEGMENT_CREATED='NO';
select * from user_indexes WHERE SEGMENT_CREATED='NO';
select * from user_lobs where segment_created='NO';
select count(*) from v$process --Current connections
select value from v$parameter where name = 'processes' --Maximum number of connections allowed for the database

---Modify maximum connections:
alter system set processes = 300 scope = spfile;

---Restart database:
shutdown immediate;
startup;

---View which users are currently using data
SELECT osuser, a.username,cpu_time/executions/1000000||'s', sql_fulltext,machine 
from v$session a, v$sqlarea b
where a.sql_address =b.address order by cpu_time/executions desc;

---See oracle Performance of
show parameter sga;

Note: when migrating data, you need to migrate the table structure, table data, table index, function, view, trigger, and finally the table sequence. Because in the process of migrating the database, the sequence of the old database may still be added, and finally the sequence of the new data can avoid the sequence conflict.

Tags: Database Excel Oracle sqlplus

Posted on Wed, 29 Apr 2020 11:59:36 -0400 by cheikhbouchihda