Академический Документы
Профессиональный Документы
Культура Документы
The external tables feature is a complement to existing SQL*Loader functionality. It enables you
to access data in external sources as if it were in a table in the database.
Prior to Oracle Database 10g, external tables were read-only. However, as of Oracle Database
10g, external tables can also be written to. Note that SQL*Loader may be the better choice in
data loading situations that require additional indexing of the staging table. See Behavior
Differences Between SQL*Loader and External Tables for more information about how load
behavior differs between SQL*Loader and external tables.
To use the external tables feature, you must have some knowledge of the file format and record
format of the datafiles on your platform if the ORACLE_LOADER access driver is used and the
datafiles are in text format. You must also know enough about SQL to be able to create an
external table and perform queries against it.
This chapter discusses the following topics:
How Are External Tables Created?
Using External Tables to Load and Unload Data
Datatype Conversion During External Table Use
Parallel Access to External Tables
Performance Hints When Using External Tables
External Table Restrictions
Behavior Differences Between SQL*Loader and External Tables
The information you provide through the access driver ensures that data from the data source is
processed so that it matches the definition of the external table. The fields listed after CREATE
TABLE emp_load are actually defining the metadata for the data in the info.dat source file. The
access parameters are optional.
Access Parameters
When you create an external table of a particular type, you can specify access parameters to
modify the default behavior of the access driver. Each access driver has its own syntax for access
parameters.
See Also:
Chapter 14, " The ORACLE_LOADER Access Driver"
Chapter 15, " The ORACLE_DATAPUMP Access Driver"
Directory objects can be created by DBAs or by any user with the CREATE ANY DIRECTORY
privilege.
After a directory is created, the user creating the directory object needs to grant READ and WRITE
privileges on the directory to other users. These privileges must be explicitly granted, rather than
assigned through the use of roles. For example, to allow the server to read files on behalf of user
scott in the directory named by ext_tab_dir, the user who created the directory object must
execute the following command:
GRANT READ ON DIRECTORY ext_tab_dir TO scott;
The name of the directory object can appear in the following places in a CREATE
TABLE...ORGANIZATION EXTERNAL statement:
The DEFAULT DIRECTORY clause, which specifies the default directory to use for all input and
output files that do not explicitly name a directory object.
The LOCATION clause, which lists all of the datafiles for the external table. The files are
named in the form directory:file. The directory portion is optional. If it is missing,
the default directory is used as the directory for the file.
The ACCESS PARAMETERS clause where output files are named. The files are named in the
form directory:file. The directory portion is optional. If it is missing, the default
directory is used as the directory for the file. Syntax in the access parameters enables you
to indicate that a particular output file should not be created. This is useful if you do not
care about the output files or if you do not have write access to any directory objects.
The SYS user is the only user that can own directory objects, but the SYS user can grant other
users the privilege to create directory objects. Note that READ or WRITE permission to a directory
object means only that the Oracle database will read or write that file on your behalf. You are not
given direct access to those files outside of the Oracle database unless you have the appropriate
operating system privileges. Similarly, the Oracle database requires permission from the
operating system to read and write files in the directories.
Example: Creating and Loading an External Table Using
ORACLE_LOADER
The information you provide through the access driver ensures that data from the data source is
processed so that it matches the definition of the external table. The fields listed after CREATE
TABLE emp_load are actually defining the metadata for the data in the info.dat source file. The
access parameters are optional.
Access Parameters
When you create an external table of a particular type, you can specify access parameters to
modify the default behavior of the access driver. Each access driver has its own syntax for access
parameters.
See Also:
Chapter 14, " The ORACLE_LOADER Access Driver"
Chapter 15, " The ORACLE_DATAPUMP Access Driver"
Location of Datafiles and Output Files
The access driver runs inside the database server. This is different from SQL*Loader, which is a
client program that sends the data to be loaded over to the server. This difference has the
following implications:
The server must have access to any files to be loaded by the access driver.
The server must create and write the output files created by the access driver: the log file, bad
file, and discard file, as well as any dump files created by the ORACLE_DATAPUMP access
driver.
The access driver does not allow you to specify a complete specification for files. This is because
the server may have access to files that you do not, and allowing you to read this data would
affect security. Similarly, you might overwrite a file that you normally would not have privileges
to delete.
Instead, you are required to specify directory objects as the locations from which to read files
and write files. A directory object maps a name to a directory name on the file system. For
example, the following statement creates a directory object named ext_tab_dir that is mapped
to a directory located at /usr/apps/datafiles.
CREATE DIRECTORY ext_tab_dir AS '/usr/apps/datafiles';
Directory objects can be created by DBAs or by any user with the CREATE ANY DIRECTORY
privilege.
After a directory is created, the user creating the directory object needs to grant READ and WRITE
privileges on the directory to other users. These privileges must be explicitly granted, rather than
assigned through the use of roles. For example, to allow the server to read files on behalf of user
scott in the directory named by ext_tab_dir, the user who created the directory object must
execute the following command:
GRANT READ ON DIRECTORY ext_tab_dir TO scott;
The name of the directory object can appear in the following places in a CREATE
TABLE...ORGANIZATION EXTERNAL statement:
The DEFAULT DIRECTORY clause, which specifies the default directory to use for all input and
output files that do not explicitly name a directory object.
The LOCATION clause, which lists all of the datafiles for the external table. The files are
named in the form directory:file. The directory portion is optional. If it is missing,
the default directory is used as the directory for the file.
The ACCESS PARAMETERS clause where output files are named. The files are named in the
form directory:file. The directory portion is optional. If it is missing, the default
directory is used as the directory for the file. Syntax in the access parameters enables you
to indicate that a particular output file should not be created. This is useful if you do not
care about the output files or if you do not have write access to any directory objects.
The SYS user is the only user that can own directory objects, but the SYS user can grant other
users the privilege to create directory objects. Note that READ or WRITE permission to a directory
object means only that the Oracle database will read or write that file on your behalf. You are not
given direct access to those files outside of the Oracle database unless you have the appropriate
operating system privileges. Similarly, the Oracle database requires permission from the
operating system to read and write files in the directories.
2. Execute the following SQL statements to set up a default directory (which contains the
data source) and to grant access to it:
CREATE DIRECTORY ext_tab_dir AS '/usr/apps/datafiles';
GRANT READ ON DIRECTORY ext_tab_dir TO SCOTT;
5. Load the data from the external table emp_load into the table emp:
INSERT INTO emp (emp_no, first_name, middle_initial, last_name)
(SELECT employee_number, employee_first_name,
substr(employee_middle_name, 1, 1),
employee_last_name
FROM emp_load);
6. Perform the following select operation to verify that the information in the .dat file was
loaded into the emp table:
SQL> SELECT * FROM emp;
Note:
Data can only be unloaded using the ORACLE_DATAPUMP access driver.
Loading Data
When data is loaded, the data stream is read from the files specified by the LOCATION and
DEFAULT DIRECTORY clauses. The INSERT statement generates a flow of data from the external
data source to the Oracle SQL engine, where data is processed. As data from the external source
is parsed by the access driver and provided to the external table interface, it is converted from its
external representation to its Oracle internal datatype.
To load table roster from roster_data, you would specify something similar to the following:
INSERT INTO roster (student, grade)
(SELECT student_type(student_no, name), grade FROM roster_data);
See Also:
Choosing External Tables Versus SQL*Loader
See Also:
Reserved Words for the ORACLE_LOADER Access Driver
Reserved Words for the ORACLE_DATAPUMP Access Driver
Byte-Order Marks
With SQL*Loader, if a primary datafile uses a Unicode character set (UTF8 or UTF16) and it
also contains a byte-order mark (BOM), then the byte-order mark is written at the beginning of
the corresponding bad and discard files. With external table loads, the byte-order mark is not
written at the beginning of the bad and discard files.
In external tables, the use of the backslash escape character within a string will raise an error.
The workaround is to use double quotation marks to mark the separation string, as follows:
TERMINATED BY ',' ENCLOSED BY "'"
part_et. et_ ../..../../na toc.ht ind ../../mix. ../../dc
htm par /in v/port m ex. 101/b12 omm
ams de al_3.h htm039/toc. on/ht
.ht x.h tm htm ml/fe
m tm edbac
k.htm
Previous Content
Copyright © 1996, 2003 Oracle Corporation s Inde
All Rights Reserved. Book x Master
Next Ho List Index
me Feedbac
k
Note:
Data can only be unloaded using the ORACLE_DATAPUMP access driver.
Loading Data
When data is loaded, the data stream is read from the files specified by the LOCATION and
DEFAULT DIRECTORY clauses. The INSERT statement generates a flow of data from the external
data source to the Oracle SQL engine, where data is processed. As data from the external source
is parsed by the access driver and provided to the external table interface, it is converted from its
external representation to its Oracle internal datatype.
Unloading Data Using the ORACLE_DATAPUMP Access Driver
To unload data, you use the ORACLE_DATAPUMP access driver. The data stream that is unloaded is
in a proprietary format and contains all the column data for every row being unloaded.
An unload operation also creates a metadata stream that describes the contents of the data stream.
The information in the metadata stream is required for loading the data stream. Therefore, the
metadata stream is written to the datafile and placed before the data stream.
To load table roster from roster_data, you would specify something similar to the following:
INSERT INTO roster (student, grade)
(SELECT student_type(student_no, name), grade FROM roster_data);
See Also:
Choosing External Tables Versus SQL*Loader
See Also:
Reserved Words for the ORACLE_LOADER Access Driver
Reserved Words for the ORACLE_DATAPUMP Access Driver
Byte-Order Marks
With SQL*Loader, if a primary datafile uses a Unicode character set (UTF8 or UTF16) and it
also contains a byte-order mark (BOM), then the byte-order mark is written at the beginning of
the corresponding bad and discard files. With external table loads, the byte-order mark is not
written at the beginning of the bad and discard files.
Previous Content
Copyright © 1996, 2003 Oracle Corporation s Inde
All Rights Reserved. Book x Master
Next Ho List Index
me Feedbac
k
New notes
How Are External Tables Created?
• TYPE - specifies the type of external table. The two available types are
the ORACLE_LOADER type and the ORACLE_DATAPUMP type. Each type
of external table is supported by its own access driver.
o The ORACLE_LOADER access driver is the default. It can perform
only data loads, and the data must come from text datafiles.
Loads from external tables to internal tables are done by reading
from the text-only datafiles in the external table.
o The ORACLE_DATAPUMP access driver can perform both loads
and unloads. The data must come from binary dump files. Loads
to internal tables from external tables are done by fetching from
the binary dump files. Unloads from internal tables to external
tables are done by populating the binary dump files of the
external table.
• DEFAULT DIRECTORY - specifies the default location of files that are
read or written by external tables. The location is specified with a
directory object, not a directory path. See Location of Datafiles and
Output Files for more information.
• ACCESS PARAMETERS - describe the external data source and
implements the type of external table that was specified. Each type of
external table has its own access driver that provides access
parameters unique to that type of external table. See Access
Parameters.
• LOCATION - specifies the location of the external data. The location is
specified as a list of directory objects and filenames. If the directory
object is not specified, then the default directory object is used as the
file location.
Table created.
The information you provide through the access driver ensures that data
from the data source is processed so that it matches the definition of the
external table. The fields listed after CREATE TABLE emp_load are actually
defining the metadata for the data in the info.dat source file. The access
parameters are optional.
Access Parameters
When you create an external table of a particular type, you can specify
access parameters to modify the default behavior of the access driver. Each
access driver has its own syntax for access parameters.
Note:
These access parameters are collectively referred to as the
opaque_format_spec in the SQL CREATE TABLE...ORGANIZATION
EXTERNAL statement.
See Also:
The access driver runs inside the database server. This is different from
SQL*Loader, which is a client program that sends the data to be loaded over
to the server. This difference has the following implications:
• The server must have access to any files to be loaded by the access
driver.
• The server must create and write the output files created by the
access driver: the log file, bad file, and discard file, as well as any
dump files created by the ORACLE_DATAPUMP access driver.
The access driver does not allow you to specify a complete specification for
files. This is because the server may have access to files that you do not, and
allowing you to read this data would affect security. Similarly, you might
overwrite a file that you normally would not have privileges to delete.
Instead, you are required to specify directory objects as the locations from
which to read files and write files. A directory object maps a name to a
directory name on the file system. For example, the following statement
creates a directory object named ext_tab_dir that is mapped to a
directory located at /usr/apps/datafiles.
Directory objects can be created by DBAs or by any user with the CREATE
ANY DIRECTORY privilege.
After a directory is created, the user creating the directory object needs to
grant READ and WRITE privileges on the directory to other users. These
privileges must be explicitly granted, rather than assigned through the use of
roles. For example, to allow the server to read files on behalf of user scott
in the directory named by ext_tab_dir, the user who created the directory
object must execute the following command:
The name of the directory object can appear in the following places in a
CREATE TABLE...ORGANIZATION EXTERNAL statement:
The SYS user is the only user that can own directory objects, but the SYS
user can grant other users the privilege to create directory objects. Note that
READ or WRITE permission to a directory object means only that the Oracle
database will read or write that file on your behalf. You are not given direct
access to those files outside of the Oracle database unless you have the
appropriate operating system privileges. Similarly, the Oracle database
requires permission from the operating system to read and write files in the
directories.
See Also:
Oracle Database SQL Language Reference for detailed information about the
correct way to specify date and time formats