基本流程如下 :
· 在源数据库导出DUMP文件
· 在目标数据库TRUNCATE TABLE
· 在目标数据库导入DUMP文件
exp user_name/password tables = tableA,tableB file=xxx_20140113.dmp
truncate table tableA;
imp user_name/password @targetTNS full=y ignore=y file= xxx_20140113.dmp
需要注意的两点:
1. sequence 在 imp,exp时没有导出到新数据库
2. truncate table时需要注意因外键而无法truncate的情况。
sequence 的问题推荐方案:
可以在目标数据库先drop ,再 create.
先在源数据库运行如下语句。
SELECT 'create sequence TABLE_A_SEQ START WITH ' || TABLE_A_SEQ.nextval || ';'FROM dual;
将生成的SQL 去双引号等到基本创建语句。
目标数据库上:
drop sequence TABLE_A_SEQ;
create sequence TABLE_A_SEQ START WITH 275802;
truncate table 的问题推荐方案:
a) disable foreign key
b) truncate
c) enable foreign key
以上操作都是在目标数据库。
· @ disable_fk tableA 得到的SQL 去双引号后运行。
· Truncate tableA
· 第一步中生成的SQL把disable替换成enable运行 (enable 时出现子表中部分数据不在父表中,按以下方式处理)
o @ddl tableA 找出关联 tableB父表的列C
o delete from tableA where C not in (select C fromtableB);
引用的工具脚本如下。
注意以下脚本要用owner跑,如果以DBA身份,要把USER_换成DBA_
1.disable_fk.sql
column fkey format a80 word_wrapped
select
'alter table "' || child_tname || '"' || chr(10) ||
'disable constraint "' || child_cons_name || '"' ||';' fkey
from
( select a.table_name child_tname, a.constraint_name
child_cons_name,
b.r_constraint_name parent_cons_name,
max(decode(position, 1, '"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 2,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 3,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 4,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 5,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 6,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 7,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 8,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 9,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,10,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,11,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,12,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,13,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,14,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,15,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,16,', '||'"'||
substr(column_name,1,30)||'"',NULL))
child_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type = 'R'
group by a.table_name, a.constraint_name,
b.r_constraint_name ) child,
( select a.constraint_name parent_cons_name, a.table_name
parent_tname,
max(decode(position, 1, '"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 2,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 3,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 4,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 5,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 6,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 7,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 8,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position, 9,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,10,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,11,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,12,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,13,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,14,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,15,', '||'"'||
substr(column_name,1,30)||'"',NULL)) ||
max(decode(position,16,', '||'"'||
substr(column_name,1,30)||'"',NULL))
parent_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type in ( 'P', 'U' )
group by a.table_name, a.constraint_name ) parent
where child.parent_cons_name = parent.parent_cons_name
and parent.parent_tname like upper('%&1%');
2.ddl.sql
--------------------------------------------------------------------------------
--
-- File name: ddl.sql
-- Purpose: Extracts DDL statements for specified objects
--
-- Author: Tanel Poder
-- Copyright: (c) http://www.tanelpoder.com
--
-- Usage: @ddl [schema.]<object_name_pattern>
-- @ddl mytable
-- @ddl system.table
-- @ddl sys%.%tab%
--
--------------------------------------------------------------------------------
exec dbms_metadata.set_transform_param( dbms_metadata.session_transform,'SQLTERMINATOR', TRUE);
select
dbms_metadata.get_ddl( case when object_type like 'PACKAGE%' then 'PACKAGE' when object_type like 'DATABASE LINK' then 'DB_LINK' when object_type like 'MATERIALIZED VIEW' then 'MATERIALIZED_VIEW' else object_type end, object_name, owner )
from
all_objects
where
upper(object_name) LIKE
upper(CASE
WHEN INSTR('&1','.') > 0 THEN
SUBSTR('&1',INSTR('&1','.')+1)
ELSE
'&1'
END
)
AND owner LIKE
CASE WHEN INSTR('&1','.') > 0 THEN
UPPER(SUBSTR('&1',1,INSTR('&1','.')-1))
ELSE
user
END
AND object_type not like '%PARTITION'
/