2)external table example02
1@@@@transport and filter data between two database using externtal table.
guide:
  It is more convinent than sql*loader.
  Note the external type, we use the oracle_datapump, not oracle_loader privously.

@@@
@@@<1>(source_db)create directory object to save the dump file
@@@
SYS@ocp> !mkdir -p /home/oracle/datafiles          
SYS@ocp> !mkdir -p /home/oracle/logfiles
SYS@ocp> CREATE OR REPLACE DIRECTORY data_file_dir AS '/home/oracle/datafiles';
Directory created.
SYS@ocp> CREATE OR REPLACE DIRECTORY log_file_dir AS '/home/oracle/logfiles';
Directory created.
SYS@ocp> GRANT READ, WRITE ON DIRECTORY data_file_dir TO sh;
Grant succeeded.
SYS@ocp> GRANT READ, WRITE ON DIRECTORY log_file_dir TO sh;
Grant succeeded.

@@@
@@@<2>(source db)create external table which is mirror of source table.
@@@
SYS@ocp> ed
  1  CREATE TABLE sh.sales_xt
  2  ORGANIZATION EXTERNAL
  3  (TYPE oracle_datapump
  4   DEFAULT DIRECTORY data_file_dir
  5   LOCATION ('sales_xt.dmp')
  6  )
  7* AS SELECT * FROM sh.sales
SYS@ocp> /
Table created.

@@@
@@@Note: here no DML for external table
SYS@ocp> select count(*) from sh.sales_xt;
  COUNT(*)
----------
    918847
SYS@ocp> update sh.sales_xt set amount_sold=1232 where amount_sold=1232.16;
update sh.sales_xt set amount_sold=1232 where amount_sold=1232.16
          *
ERROR at line 1:
ORA-30657: operation not supported on external organized table


@@@
@@@<3>(source_db)remote copy the dump file to target host.
@@@
[root@station78 datafiles]# scp sales_xt.dmp station243:/home/oracle/datafiles
root@station243's password:
sales_xt.dmp                       100%   30MB  29.6MB


@@@
@@@<4>(target_db)change the right, and create directory object.
@@@
[root@station243 datafiles]# chown -R oracle:oinstall /home/oracle

@@@
SYS@orcl> !mkdir -p /home/oracle/datafiles          
SYS@orcl> !mkdir -p /home/oracle/logfiles
SYS@orcl> CREATE OR REPLACE DIRECTORY datafile_dir AS '/home/oracle/datafiles';
Directory created.
SYS@orcl> CREATE OR REPLACE DIRECTORY logfile_dir AS '/home/oracle/logfiles';
Directory created.
SYS@orcl> GRANT READ, WRITE ON DIRECTORY datafile_dir TO dw;
Grant succeeded.
SYS@orcl> GRANT READ, WRITE ON DIRECTORY logfile_dir TO dw;
Grant succeeded.

@@@
@@@<5>(target_db)create external table using sales_xt.dmp
@@@
@@@Note:
@@@here is a big question: external table do not support constraints.
SYS@orcl> ed
  1  CREATE TABLE dw.sales_xt2
  2  (prod_id NUMBER not null,
  3   cust_id NUMBER not null,
  4   time_id DATE not null,
  5   channel_id NUMBER not null,
  6   promo_id NUMBER not null,
  7   quantity_sold NUMBER(10,2) not null,
  8   amount_sold NUMBER(10,2) not null
  9   )
 10  ORGANIZATION EXTERNAL
 11  (TYPE ORACLE_DATAPUMP
 12   DEFAULT DIRECTORY datafile_dir
 13   LOCATION ('sales_xt.dmp')
 14* )
SYS@orcl> /
(prod_id NUMBER not null,
                *
ERROR at line 2:
ORA-30657: operation not supported on external organized table

@@@
SYS@orcl> ed
  1  CREATE TABLE dw.sales_xt2
  2  (prod_id NUMBER ,
  3   cust_id NUMBER ,
  4   time_id DATE ,
  5   channel_id NUMBER ,
  6   promo_id NUMBER ,
  7   quantity_sold NUMBER(10,2) ,
  8   amount_sold NUMBER(10,2)
  9   )
 10  ORGANIZATION EXTERNAL
 11  (TYPE ORACLE_DATAPUMP
 12   DEFAULT DIRECTORY datafile_dir
 13   LOCATION ('sales_xt.dmp')
 14* )
SYS@orcl> /
Table created.

SYS@orcl> create table dw.sales_copy_from_remote_db as select * from dw.sales_xt2;
Table created.

@@@
@@@<6>compare with the results, lacking of not null constraints.
@@@
SYS@orcl> desc dw.sales_copy_from_remote_db
 Name                       Null?    Type
 ----------------------------------------- -------- ----------------------------
 PROD_ID                        NUMBER
 CUST_ID                        NUMBER
 TIME_ID                        DATE
 CHANNEL_ID                        NUMBER
 PROMO_ID                        NUMBER
 QUANTITY_SOLD                        NUMBER(10,2)
 AMOUNT_SOLD                        NUMBER(10,2)

SYS@ocp> desc sh.sales;
 Name                       Null?    Type
 ----------------------------------------- -------- ----------------------------
 PROD_ID                   NOT NULL NUMBER
 CUST_ID                   NOT NULL NUMBER
 TIME_ID                   NOT NULL DATE
 CHANNEL_ID                   NOT NULL NUMBER
 PROMO_ID                   NOT NULL NUMBER
 QUANTITY_SOLD                   NOT NULL NUMBER(10,2)
 AMOUNT_SOLD                   NOT NULL NUMBER(10,2)