Legal | Privacy

Using External Tables for Data Unload and Projected Columns

This module shows you how to use two new Oracle10g features: Data Unloading and Projected Columns.

This module discusses the following topics:

Overview
Prerequisites
Populating External Tables
Using Projected Columns

Place the cursor on this icon to display all the screenshots. You can also place the cursor on each individual icon in the following steps to see only the screenshot that is associated with that step.

External Table Population

Prior to Oracle Database 10g, external tables were read-only. In Oracle Database 10g, external tables can also be written to. Although neither data manipulation language (DML) operations nor index creation is allowed on an external table, you can use the CREATE TABLE AS SELECT command to populate an external table composed of proprietary format (Direct Path API) flat files that are operating system independent.

In the context of external tables, loading data refers to the act of data being read from an external table and loaded into a table in the database. Unloading data refers to the act of reading data from a table in the database and inserting it into an external table. Both these operations can be used with external tables using the new Data Pump access driver.

Projected Columns

When dealing with external table files, which contain rows of data that may be rejected, the projected column feature enables you to get a consistent result set independent of the columns referenced by the SQL statement accessing your external table. Prior to Oracle Database 10g, only the columns referenced by the SQL statement were projected out by the access driver. Because of the access driver parsing the input data stream, and the external table service doing data conversions from the external representation to the internal representation, some rows may get rejected due to conversion errors or data format errors.

Before starting this module, you should have:

1.

Completed the Configuring Linux for the Installation of Oracle Database 10g lesson.

 

2.

Completed the Installing the Oracle Database 10g on Linux lesson.

 

3.

Completed the Postinstallation Tasks lesson.

 

4.

Downloaded and unzipped exttable.zip into your working directory (i.e. /home/oracle/wkdir).

 

5.

Created a directory for this lesson. Open a terminal window and execute the following:

cd wkdir
sqlplus /nolog
connect / as sysdba
create or replace directory ext_tab_dir as '/home/oracle/wkdir';
grant read, write on directory ext_tab_dir to oe;

Move your mouse over this icon to see the image

You will create an external table, unload data into it, and then query from the external table. Perform the following steps:

1.

Create an external table that will unload data into two files. From your terminal window, execute the following commands:

connect oe/oe
@crtabl01

The query in the crtabl01.sql script is as follows:

DROP TABLE emp_ext;

CREATE TABLE emp_ext
(first_name, last_name, department_name)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_DATAPUMP
DEFAULT DIRECTORY ext_tab_dir
LOCATION
('emp1.exp','emp2.exp')
)
PARALLEL
AS
SELECT e.first_name
, e.last_name
, d.department_name
FROM hr.employees e JOIN
hr.departments d USING (department_id)
WHERE d.department_name
in ('Marketing', 'Purchasing');

Note that you are using ORACLE_DATAPUMP as the type.

Move your mouse over this icon to see the image

 

2.

Now you can query the external table by executing the following script from your terminal window:

@query01

The query in the query01.sql script is as follows:

select * from emp_ext;
exit

Move your mouse over this icon to see the image

Note that the external table shows two employees in Marketing and six employees in Purchasing.

 

You will use external tables to project column data. Perform the following steps:

1.

Review the contents of the order_items.dat external data file. From your terminal window, execute the following command:

more order_items.dat

Move your mouse over this icon to see the image

Note the value 2000 in row 3, which is 4 characters. This may cause some trouble.

 

2.

Now you can create the external table by executing the following commands:

sqlplus oe/oe
@crtabl02

The command in the crtabl02.sql script is as follows:

drop table order_items_ext;

create table order_items_ext
( order_id number(12)
, line_id NUMBER(3)
, product_id number(6)
, unit_price number(8,2)
, quantity number(8)
)
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
DEFAULT DIRECTORY ext_tab_dir
ACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ',')
LOCATION ('order_items.dat')
)
REJECT LIMIT UNLIMITED;

Move your mouse over this icon to see the image

Note that the format of LINE_ID is NUMBER(3).

 

3.

You will alter the table to set the PROJECT COLUMN attribute to ALL. This is the default. Then you can query the external table. Execute the following command:

alter table order_items_ext project column all;
select count(order_id)from order_items_ext; 

Move your mouse over this icon to see the image

Two rows were found.

 

4.

To see why only two rows were found when there are three rows in the order_items.dat file, you should look at the Loader log file. Open ORDER_ITEMS_EXT_#####.log from gedit.

Move your mouse over this icon to see the image

Note that the record that had a LINE_ID greater than 3 numbers was rejected when the external table was accessed. That row never reached the SQL query processing.

 

5.

To see the row that was rejected, open ORDER_ITEMS_EXT_#####.bad from gedit.

Move your mouse over this icon to see the image

 

6.

Now you can change the PROJECT COLUMN attribute to REFERENCED and run the same query to see what happens. Execute the following command:

alter table order_items_ext project column referenced;
select count(order_id)from order_items_ext; 

Move your mouse over this icon to see the image

Three rows were found. This query projected the external data to the field needed (ORDER_ID) so all three records were accepted.

 

Place the cursor on this icon to hide all screenshots.

 

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy