Presentation on theme: "CHAPTER 14 External Tables. External Table Features An external table allows you to create a database table object that uses as its source an operating."— Presentation transcript:
CHAPTER 14 External Tables
External Table Features An external table allows you to create a database table object that uses as its source an operating system file. Directly select information from operating-system flat files via SQL, which allows you to do tasks such as loading operating-system comma-separated-value (CSV) files into the database. Create platform-independent dump files that can be used to transfer data. You can also create these files as compressed and encrypt them for efficient and secure data transportation.
SQL*Loader versus External Tables Loading data with external tables is more straightforward and requires fewer steps. The interface for creating and loading from external tables is SQL*Plus. Many DBAs/developers find using SQL*Plus more intuitive and powerful than SQL*Loader’s parameter-file interface. You can view data in an external table before it’s loaded into a database table. You can load, transform, and aggregate the data without an intermediate staging table. For large amounts of data, this can be a huge space savings.
Loading an External Table from a CSV File 1. Create a database-directory object that points to the location of the CSV file. 2. Grant read and write privileges on the directory object to the user creating the external table. I usually use a DBA privileged account, so I don’t need to perform this step. 3. Run the CREATE TABLE...ORGANIZATION EXTERNAL statement. 4. Use SQL*Plus to access the contents of the CSV file.
Loading an External Table from a CSV File
Creating External Table Statement create table exadata_et( exa_id NUMBER,machine_count NUMBER,hide_flag NUMBER,oracle NUMBER,ship_date DATE,rack_type VARCHAR2(32) ) organization external ( type oracle_loader default directory exa_dir access parameters ( records delimited by newline fields terminated by '|' missing field values are null (exa_id,machine_count,hide_flag,oracle,ship_date char date_format date mask "mm/dd/yyyy",rack_type) ) location ('ex.csv') ) reject limit unlimited;
Viewing External-Table Metadata Use the DBA_EXTERNAL_TABLES, DBA_EXTERNAL_LOCATIONS views: select owner,table_name,default_directory_name,access_parameters from dba_external_tables; select owner,table_name,location from dba_external_locations;
Load a Regular Table with Data from an External Table Use APPEND hint to direct path load. Very efficient way to transfer data from an external table to a regular table where the data can be manipulated. SQL> insert /*+ APPEND */ into exa_info select * from exadata_et;
Performing Advanced Transformations 1. Create an external table. 2. Create a record type that maps to the columns in the external table. 3. Create a table based on the record type created in Step 2 4. Create a piplelined function that is used to inspect each row as it’s loaded and transform data based on business requirements. 5. Use an INSERT statement that selects from the external table and uses the pipelined function to transform data as it’s loaded.
Basing an External Table on an OS Text File This example bases an external table on the alert.log file. This allows users to query the alert.log file via SQL*Plus
Basing an External Table on an OS Text File create table alert_log_file( alert_text varchar2(4000)) organization external ( type oracle_loader default directory t_loc access parameters ( records delimited by newline nobadfile nologfile nodiscardfile fields terminated by '#$~=ui$X' missing field values are null (alert_text) ) location ('alert_O11R2.log') ) reject limit unlimited;
Transferring Data using External Tables
Using Parallelism when Unloading CREATE TABLE inv_et ORGANIZATION EXTERNAL ( TYPE ORACLE_DATAPUMP DEFAULT DIRECTORY dp LOCATION ('inv1.dmp','inv2.dmp') ) PARALLEL 2 AS SELECT * FROM inv;
Compressing a Dump File ACCESS PARAMETERS clause: CREATE TABLE inv_et ORGANIZATION EXTERNAL ( TYPE ORACLE_DATAPUMP DEFAULT DIRECTORY dp ACCESS PARAMETERS (COMPRESSION ENABLED) LOCATION ('inv1.dmp') ) AS SELECT * FROM inv;
Encrypting a Dump File CREATE TABLE inv_et ORGANIZATION EXTERNAL ( TYPE ORACLE_DATAPUMP DEFAULT DIRECTORY dp ACCESS PARAMETERS (ENCRYPTION ENABLED) LOCATION ('inv1.dmp') ) AS SELECT * FROM inv;
Summary External tables are flexible objects that allow you to create a database table that uses an operating system file as its input. Allows for easy loading of data from CSV and text files. External tables can also be used to efficiently and securely transfer data from one database environment to another.