Unloading and Loading Data with the ORACLE_DATAPUMP Access Driver

As part of creating an external table with a SQL CREATE TABLE AS SELECT statement, the ORACLE_DATAPUMP access driver can write data to a dump file. The data in the file is written in a binary format that can only be read by the ORACLE_DATAPUMP access driver. Once the dump file is created, it cannot be modified (that is, no data manipulation language (DML) operations can be performed on it). However, the file can be read any number of times and used as the dump file for another external table in the same database or in a different database.

The following steps use the sample schema, oe, to show an extended example of how you can use the ORACLE_DATAPUMP access driver to unload and load data. (The example assumes that the directory object def_dir1 already exists, and that user oe has read and write access to it.)

  1. An external table will populate a file with data only as part of creating the external table with the AS SELECT clause. The following example creates an external table named inventories_xt and populates the dump file for the external table with the data from table inventories in the oe schema.
    SQL> CREATE TABLE inventories_xt
      2  ORGANIZATION EXTERNAL
      3  (
      4    TYPE ORACLE_DATAPUMP
      5    DEFAULT DIRECTORY def_dir1
      6    LOCATION ('inv_xt.dmp')
      7  )
      8  AS SELECT * FROM inventories;
    
    Table created.
    
  2. Describe both inventories and the new external table, as follows. They should both match.
    SQL> DESCRIBE inventories
     Name                                      Null?    Type
     ---------------------------------------- --------- ----------------
     PRODUCT_ID                                NOT NULL NUMBER(6)
     WAREHOUSE_ID                              NOT NULL NUMBER(3)
     QUANTITY_ON_HAND                          NOT NULL NUMBER(8)
    
    SQL> DESCRIBE inventories_xt
     Name                                      Null?    Type
     ----------------------------------------- -------- -----------------
     PRODUCT_ID                                NOT NULL NUMBER(6)
     WAREHOUSE_ID                              NOT NULL NUMBER(3)
     QUANTITY_ON_HAND                          NOT NULL NUMBER(8)
    
  3. Now that the external table is created, it can be queried just like any other table. For example, select the count of records in the external table, as follows:
    SQL> SELECT COUNT(*) FROM inventories_xt;
    
      COUNT(*)
    ----------
          1112
    
  4. Compare the data in the external table against the data in inventories. There should be no differences.
    SQL> SELECT * FROM inventories MINUS SELECT * FROM inventories_xt;
    
    no rows selected
    
  5. After an external table has been created and the dump file populated by the CREATE TABLE AS SELECT statement, no rows may be added, updated, or deleted from the external table. Any attempt to modify the data in the external table will fail with an error.

    The following example shows an attempt to use data manipulation language (DML) on an existing external table. This will return an error, as shown.

    SQL> DELETE FROM inventories_xt WHERE warehouse_id = 5;
    DELETE FROM inventories_xt WHERE warehouse_id = 5
                *
    ERROR at line 1:
    ORA-30657: operation not supported on external organized table
    
  6. The dump file created for the external table can now be moved and used as the dump file for another external table in the same database or different database. Note that when you create an external table that uses an existing file, there is no AS SELECT clause for the CREATE TABLE statement.
    SQL> CREATE TABLE inventories_xt2
      2  (
      3    product_id          NUMBER(6),
      4    warehouse_id        NUMBER(3),
      5    quantity_on_hand    NUMBER(8)
      6  )
      7  ORGANIZATION EXTERNAL
      8  (
      9    TYPE ORACLE_DATAPUMP
     10    DEFAULT DIRECTORY def_dir1
     11    LOCATION ('inv_xt.dmp')
     12  );
    
    Table created.
    
  7. Compare the data for the new external table against the data in the inventories table. The product_id field will be converted to a compatible data type before the comparison is done. There should be no differences.
    SQL> SELECT * FROM inventories MINUS SELECT * FROM inventories_xt2;
    
    no rows selected
    
  8. Create an external table with three dump files and with a degree of parallelism of three.
    SQL> CREATE TABLE inventories_xt3
      2  ORGANIZATION EXTERNAL
      3  (
      4    TYPE ORACLE_DATAPUMP
      5    DEFAULT DIRECTORY def_dir1
      6    LOCATION ('inv_xt1.dmp', 'inv_xt2.dmp', 'inv_xt3.dmp')
      7  )
      8  PARALLEL 3
      9  AS SELECT * FROM inventories;
    
    Table created.
    
  9. Compare the data unload against inventories. There should be no differences.
    SQL> SELECT * FROM inventories MINUS SELECT * FROM inventories_xt3;
    
    no rows selected
    
  10. Create an external table containing some rows from table inventories.
    SQL> CREATE TABLE inv_part_xt
      2  ORGANIZATION EXTERNAL
      3  (
      4  TYPE ORACLE_DATAPUMP
      5  DEFAULT DIRECTORY def_dir1
      6  LOCATION ('inv_p1_xt.dmp')
      7  )
      8  AS SELECT * FROM inventories WHERE warehouse_id < 5;
     
    Table created.
    
  11. Create another external table containing the rest of the rows from inventories.
    SQL> drop table inv_part_xt;
     
    Table dropped.
     
    SQL> 
    SQL> CREATE TABLE inv_part_xt
      2  ORGANIZATION EXTERNAL
      3  (
      4  TYPE ORACLE_DATAPUMP
      5  DEFAULT DIRECTORY def_dir1
      6  LOCATION ('inv_p2_xt.dmp')
      7  )
      8  AS SELECT * FROM inventories WHERE warehouse_id >= 5;
     
    Table created.
    
  12. Create an external table that uses the two dump files created in Steps 10 and 11.
    SQL> CREATE TABLE inv_part_all_xt
      2  (
      3  product_id NUMBER(6),
      4  warehouse_id NUMBER(3),
      5  quantity_on_hand NUMBER(8)
      6  )
      7  ORGANIZATION EXTERNAL
      8  (
      9  TYPE ORACLE_DATAPUMP
     10  DEFAULT DIRECTORY def_dir1
     11  LOCATION ('inv_p1_xt.dmp','inv_p2_xt.dmp')
     12  );
     
    Table created.
    
  13. Compare the new external table to the inventories table. There should be no differences. This is because the two dump files used to create the external table have the same metadata (for example, the same table name inv_part_xt and the same column information).
    SQL> SELECT * FROM inventories MINUS SELECT * FROM inv_part_all_xt;
    
    no rows selected