對於資料庫表級上的資料複製,我們最常用的是CREATE TABLE AS(CTAS)..方式。其實在SQL*Plus下面copy命令可以完成同樣的工作,而且更加出色,效能也比較優異。更突出的是支援跨平台,異構資料庫之間的資料複製。copy命令可以類似地完成一些stream完成的功能,儘管copy命令與stream方式不是一個重量級。下面描述copy命令的主要用法。
1、copy命令的協助資訊
scott@SYBO2SZ> help copy
COPY
----
Copies data from a query to a table in the same or another
database. COPY supports CHAR, DATE, LONG, NUMBER and VARCHAR2.
COPY {FROM database | TO database | FROM database TO database}
{APPEND|CREATE|INSERT|REPLACE} destination_table
[(column, column, column, ...)] USING query
where database has the following syntax:
username[/password]@connect_identifier
上面列出了copy支援的資料類型以及copy命令的用法
from database 子句指定串連的來源資料庫,如果省略則為當前串連的資料庫
to database子句指定串連的目的資料庫,如果省略則為當前資料庫
from database TO database 同時指定了串連的原資料庫以及目的資料庫
支援幾種不同的表間資料複製方式:APPEND|CREATE|INSERT|REPLACE
支援跨Oracle版本,不同schema之間,相同schema之間的資料複製
支援異構資料庫間的資料複製,如Oracle到非Oracle資料庫
支援Oracle跨平台間的資料庫複寫,如windows平到到linux平台
支援本機資料庫到遠端資料庫,遠端資料庫到本地,遠端資料庫到另一個遠端資料庫之間資料複製
複製資料時,使用Oracle net來傳輸資料
2、同一資料庫相同schema之間資料複製
--create 方式,僅指定from子句
--注,下面的樣本中,符號"-"表示是串連符號,用於換行書寫
scott@SYBO2SZ> copy from scott/tiger@sybo2sz -
> create tb_emp -
> using select * from emp;
Array fetch/bind size is 2000. (arraysize is 2000)
Will commit when done. (copycommit is 0)
Maximum long size is 5000. (long is 5000)
Table TB_EMP created.
14 rows selected from scott@sybo2sz.
14 rows inserted into TB_EMP.
14 rows committed into TB_EMP at DEFAULT HOST connection.
--append方式,僅指定to子句
scott@SYBO2SZ> copy to scott/tiger@sybo2sz -
> append tb_emp -
> using select * from emp;
Array fetch/bind size is 2000. (arraysize is 2000)
Will commit when done. (copycommit is 0)
Maximum long size is 5000. (long is 5000)
14 rows selected from DEFAULT HOST connection.
14 rows inserted into TB_EMP.
14 rows committed into TB_EMP at scott@sybo2sz.
scott@SYBO2SZ> select count(*) from tb_emp;
COUNT(*)
----------
28
--insert 方式
scott@SYBO2SZ> copy from scott/tiger@sybo2sz -
> insert tb_emp2 using select * from emp where deptno=20;
Array fetch/bind size is 2000. (arraysize is 2000)
Will commit when done. (copycommit is 0)
Maximum long size is 5000. (long is 5000)
5 rows selected from scott@sybo2sz.
5 rows inserted into TB_EMP2.
5 rows committed into TB_EMP2 at DEFAULT HOST connection.
--replace方式,上一次實驗得到的表記錄數為5,使用replace後記錄數為14,如下,
scott@SYBO2SZ> copy from scott/tiger@sybo2sz -
> replace tb_emp2 using select * from emp;
Array fetch/bind size is 2000. (arraysize is 2000)
Will commit when done. (copycommit is 0)
Maximum long size is 5000. (long is 5000)
Table TB_EMP2 dropped.
Table TB_EMP2 created.
14 rows selected from scott@sybo2sz.
14 rows inserted into TB_EMP2.
14 rows committed into TB_EMP2 at DEFAULT HOST connection.
--使用列別名的方式
--下面使用了列別名,且只複製其中的幾列資料
scott@SYBO2SZ> copy from scott/tiger@sybo2sz -
> replace tb_emp2(eno,name,job_name) using select empno,ename,job from emp;
Array fetch/bind size is 2000. (arraysize is 2000)
Will commit when done. (copycommit is 0)
Maximum long size is 5000. (long is 5000)
Table TB_EMP2 dropped.
Table TB_EMP2 created.
14 rows selected from scott@sybo2sz.
14 rows inserted into TB_EMP2.
14 rows committed into TB_EMP2 at DEFAULT HOST connection.