匯入匯出 Oracle 分割槽表資料
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
--****************************
-- 匯入匯出 Oracle 分割槽表資料
--****************************
匯入匯入Oracle 分割槽表資料是Oracle DBA 經常完成的任務之一。分割槽表的匯入匯出同樣普通表的匯入匯出方式,只不過匯入匯出需要考
慮到分割槽的特殊性,如分割槽索引,將分割槽遷移到普通表,或使用原始分割槽表匯入到新的分割槽表。下面將描述使用imp/exp,impdp/expdp匯入匯出
分割槽表資料。
有關分割槽表的特性請參考:
有關匯入匯出工具請參考:
有關匯入匯出的官方文件請參考:
一、分割槽級別的匯入匯出
可以匯出一個或多個分割槽,也可以匯出所有分割槽(即整個表)。
可以匯入所有分割槽(即整個表),一個或多個分割槽以及子分割槽。
對於已經存在資料的表,使用imp匯入時需要使用引數IGNORE=y,而使用impdp,加table_exists_action=append | replace 引數。
二、建立演示環境
1.檢視當前資料庫的版本
SQL> select * from v$version where rownum < 2;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
2.建立一個分割槽表
SQL> alter session set nls_date_format='yyyy-mm-dd';
SQL> CREATE TABLE tb_pt (
sal_date DATE NOT NULL,
sal_id NUMBER NOT NULL,
sal_row NUMBER(12) NOT NULL)
partition by range(sal_date)
(
partition sal_11 values less than(to_date('2012-01-01','YYYY-MM-DD')) ,
partition sal_12 values less than(to_date('2013-01-01','YYYY-MM-DD')) ,
partition sal_13 values less than(to_date('2014-01-01','YYYY-MM-DD')) ,
partition sal_14 values less than(to_date('2015-01-01','YYYY-MM-DD')) ,
partition sal_15 values less than(to_date('2016-01-01','YYYY-MM-DD')) ,
partition sal_16 values less than(to_date('2017-01-01','YYYY-MM-DD')) ,
partition sal_other values less than (maxvalue)
) nologging;
3.建立一個唯一索引
CREATE UNIQUE INDEX tb_pt_ind1
ON tb_pt(sal_date) nologging;
4.為分割槽表生成資料
SQL> INSERT INTO tb_pt
SELECT TRUNC(SYSDATE)+ROWNUM, dbms_random.random, ROWNUM
FROM dual
CONNECT BY LEVEL<=5000;
SQL> commit;
SQL> select count(1) from tb_pt partition(sal_11);
COUNT(1)
----------
300
SQL> select count(1) from tb_pt partition(sal_other);
COUNT(1)
----------
2873
SQL> select * from tb_pt partition(sal_12) where rownum < 3;
SAL_DATE SAL_ID SAL_ROW
--------- ---------- ----------
01-JAN-12 -1.356E+09 301
02-JAN-12 -761530183 302
三、使用exp/imp匯出匯入分割槽表資料
1.匯出整個分割槽表
[[email protected] ~]$ exp scott/tiger file='/u02/dmp/tb_pt.dmp' log='/u02/dmp/tb_pt.log' tables=tb_pt
Export: Release 11.2.0.1.0 - Production on Wed Mar 9 13:52:18 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing o
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses ZHS16GBK character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table TB_PT
. . exporting partition SAL_11 300 rows exported
. . exporting partition SAL_12 366 rows exported
. . exporting partition SAL_13 365 rows exported
. . exporting partition SAL_14 365 rows exported
. . exporting partition SAL_15 365 rows exported
. . exporting partition SAL_16 366 rows exported
. . exporting partition SAL_OTHER 2873 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
[[email protected] ~]$ oerr exp 00091
00091, 00000, "Exporting questionable statistics."
// *Cause: Export was able export statistics, but the statistics may not be
// usuable. The statistics are questionable because one or more of
// the following happened during export: a row error occurred, client
// character set or NCHARSET does not match with the server, a query
// clause was specified on export, only certain partitions or
// subpartitions were exported, or a fatal error occurred while
// processing a table.
// *Action: To export non-questionable statistics, change the client character
// set or NCHARSET to match the server, export with no query clause,
// export complete tables. If desired, import parameters can be
// supplied so that only non-questionable statistics will be imported,
// and all questionable statistics will be recalculated.
在上面的匯出中出現了錯誤提示,即EXP-00091,該錯誤表明exp工具所在的環境變數中的NLS_LANG與DB中的NLS_CHARACTERSET不一致
儘管該錯誤對最終的資料並無影響,但調整該引數來避免異常還是有必要的。因此需要將其設定為一致即可解決上述的錯誤提示。
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK
[[email protected] ~]$ export NLS_LANG='AMERICAN_AMERICA.ZHS16GBK'
經過上述設定之後再次匯出正常,過程略。
2.匯出單個分割槽
[[email protected] ~]$ exp scott/tiger file='/u02/dmp/tb_pt_sal_16.dmp' log='/u02/dmp/tb_pt_sal_16.log' tables=tb_pt:sal_16
Export: Release 11.2.0.1.0 - Production on Wed Mar 9 13:52:38 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing o
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table TB_PT
. . exporting partition SAL_16 366 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings
在上面的匯出過程中再次出現了統計資訊錯誤的情況,因此採取了對該物件收集統計資訊,但並不能解決該錯誤,但在exp命令列中增
加statistics=none即可,如下:
[[email protected] ~]$ exp scott/tiger file='/u02/dmp/tb_pt_sal_16.dmp' log='/u02/dmp/tb_pt_sal_16.log' /
> tables=tb_pt:sal_16 statistics=none
如果要匯出多個分割槽,則在tables引數中增加分割槽數。如:tables=(tb_pt:sal_15,tb_pt:sal_16)
3.使用imp工具生成建立分割槽表的DDL語句
[[email protected] ~]$ imp scott/tiger tables=tb_pt indexfile='/u02/dmp/cr_tb_pt.sql' /
> file='/u02/dmp/tb_pt.dmp' ignore=y
Export: Release 11.2.0.1.0 - Production on Wed Mar 9 13:54:38 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing o
Export file created by EXPORT:V11.02.00 via conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses ZHS16GBK character set (possible charset conversion)
. . skipping partition "TB_PT":"SAL_11"
. . skipping partition "TB_PT":"SAL_12"
. . skipping partition "TB_PT":"SAL_13"
. . skipping partition "TB_PT":"SAL_14"
. . skipping partition "TB_PT":"SAL_15"
. . skipping partition "TB_PT":"SAL_16"
. . skipping partition "TB_PT":"SAL_OTHER"
Import terminated successfully without warnings.
4.匯入單個分割槽(使用先前備份的單個分割槽匯入檔案)
SQL> alter table tb_pt truncate partition sal_16; --匯入前先將分割槽實現truncate
Table truncated.
SQL> select count(1) from tb_pt partition(sal_16);
COUNT(1)
----------
0
SQL> ho imp scott/tiger tables=tb_pt:sal_16 file='/u02/dmp/tb_pt_sal_16.dmp' ignore=y
Export: Release 11.2.0.1.0 - Production on Wed Mar 9 13:55:39 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing o
Export file created by EXPORT:V11.02.00 via conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses ZHS16GBK character set (possible charset conversion)
. importing SCOTT's objects into SCOTT
&nb