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.
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.
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;
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.
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
Note that the external table shows two employees in
Marketing and six employees in Purchasing.
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;
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.
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.
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;
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.