Creating External Table.

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Concepts Of External Tables

DEFINITION:
External tables uses the SQL*Loader functionality to access data in the operating system files without ever loading the data into a real oracle table. It is possible to read and write to a external table. Basically the data dictionary holds the table definition but the data remains in the o/s file but you can access the table just like any other table. External table offers some advantages over SQL*Loader

How To Create External Tables:


External tables are created using the SQL CREATE TABLE...ORGANIZATION EXTERNAL statement. When you create an external table, you specify the following attributes:
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.


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.
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.

- 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.
DEFAULT DIRECTORY

- 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.
ACCESS PARAMETERS

- 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.
LOCATION

Dependency/Prereqs Creating External Tables:


1. Execute the following SQL statements to set up a default directory (which contains the data source).
sql> create or replace directory ODPDIR as '/tmp';

2. Grant directory access to Scott user.


sql> GRANT READ ON DIRECTORY ODPDIR TO SCOTT;

3. Syntax Internal Representation of the External Table.


Example CREATE TABLE <table_name> ( <column_definitions>) ORGANIZATION EXTERNAL (TYPE oracle_loader DEFAULT DIRECTORY <oracle_directory_object_name> ACCESS PARAMETERS ( RECORDS DELIMITED BY newline BADFILE <file_name> DISCARDFILE <file_name> LOGFILE <file_name> [READSIZE <bytes>] [SKIP <number_of_rows> FIELDS TERMINATED BY '<terminator>' OPTIONALLY ENCLOSED BY '<character>' REJECT ROWS WITH ALL NULL FIELDS MISSING FIELD VALUES ARE NULL (<column_name_list>))\ LOCATION ('<file_name>')) [PARALLEL] REJECT LIMIT <UNLIMITED | integer>;

4. Save external file as /tmp/info.dat


cat > /tmp/info.dat 1705 1770 1771 1783 1792 01 02 03 04 05 Rao Rajesh Gangai Pushpa Ram Narasim Dheenan Nathan Raj Kumar

5. Create External Table : Actual Query


CREATE TABLE external_tab1 (employee_number CHAR(8), deptno CHAR(8), employee_name CHAR(20), employee_last_name CHAR(20)) ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT DIRECTORY ODPDIR ACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINE FIELDS (employee_number CHAR(8), deptno CHAR(8), employee_name CHAR(20), employee_last_name CHAR(20) ) ) LOCATION ('info.dat') );

6. Output Of Above Query:

7. Load the data from the external table (i.e external_tab1) into normal table (i.e external2):

8. To view Externale Table In Dynamic Tables:


col table_name format a15

col type_name format a15 col reject_limit format a15 SELECT table_name, type_name, default_directory_name,ACCESS_PARAMETERS, reject_limit, access_type FROM user_external_tables; output:

9. All OS flat files associated to the external table exist in the OS directory
select * from user_external_locations where table_name='EXTERNAL_TAB1'; output:

Backup For External Tables:


External backup done only by datapump. External tablesRMAN cannot back up those files. You must use some nonRMAN backup solution for any files not in the preceding list.

Export Scott user using expdp command: expdp scott/tiger DIRECTORY=ODPDIR DUMPFILE=externaldat.dmp SCHEMAS=scott ACCESS_METHOD=EXTERNAL_TABLE parallel=2 Import dumpfile to rajesh user using impdp command: impdp scott/tiger DIRECTORY=ODPDIR DUMPFILE=externaldat.dmp REMAP_SCHEMA=scott:rajesh parallel=2

Benefits Of External Tables:


Query data before it's loaded into the tables Perform extensive range of transformations on the data during the load process itself. You can choose to perform data transformation at the same time you're loading the data into the tables, this is called pipelining Suitable for large data loads Saves time creating table to aggregate the data in to other tables Eliminate the creation of staging or temporary tables You do not need physical space for a external table You cannot perform any update or delete operations on external tables. One small note is that you cannot index a external table, so high powered query work is impractical, if you must have indexes then use SQL*Loader and load the data into staging table or a temporary table. External tables are heavy used in data warehousing environments. Queries of external tables complete very quickly, even though a full table scan is required with each access. There is I/O involved, but modern I/O systems use caching and RAID techniques to significantly reduce the performance penalty associated with repeated full scans of the same file.

Millions of data insert in External Tables: In flat file demo.dat stored 134099 datas. It takes few seconds to create external tables with 134099 records.

You might also like