Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Oracle External Tables allow you to access external data, such as CSV files, directly from within the Oracle Database. In this guide, we’ll walk through setting up an external table, loading data from a CSV file, and performing SQL queries on that data.
Start by creating a directory on your file system where the CSV file will be stored.
bashCopiar códigomkdir /u01/19c/oracle/external_table_dir
Next, create a sample CSV file with some data and save it in the directory you just created.
bashCopiar códigovi data.csv
--
id,name,age
1,John Doe,30
2,Jane Smith,25
3,Bob Johnson,40
In Oracle SQL*Plus or any other database management tool, create an Oracle directory that points to the file system directory.
sqlCopiar códigoCREATE DIRECTORY ext_tab_dir AS '/u01/19c/oracle/external_table_dir';
Grant read permissions on the Oracle directory to the appropriate user.
sqlCopiar códigoGRANT READ ON DIRECTORY ext_tab_dir TO your_user;
Create the external table, specifying the file format and location.
sqlCopiar códigoCREATE TABLE external_table (
id CHAR(10),
name CHAR(50),
age CHAR(10)
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ext_tab_dir
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
SKIP 1
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
(
id CHAR(10),
name CHAR(50),
age CHAR(10)
)
)
LOCATION ('data.csv')
)
REJECT LIMIT UNLIMITED;
This will create a table that reads directly from the data.csv
file without loading the data permanently into the database.
You can now query the external table just like any other Oracle table.
sqlCopiar códigoSELECT * FROM external_table;
You can perform SQL operations such as filtering and aggregation on the external data.
sqlCopiar códigoSELECT name, age
FROM external_table
WHERE age > 30;
If there are any issues reading the file, check the rejection log for records that couldn’t be processed.
sqlCopiar códigoSELECT * FROM external_table_bad;
When the CSV file is updated, changes will be immediately available when querying the external table. No need to reload the data manually.
Oracle External Tables provide a powerful way to integrate external data without loading it permanently into the database. This guide demonstrated how to configure and use external tables effectively, allowing you to seamlessly query external files like CSVs.