How to create an external table in Oracle
To create an external table in Oracle, you need to perform the following steps:
Connect to the Oracle database server as a user with the CREATE TABLE privilege. You can do this using SQL*Plus or any other Oracle client.
Create a directory object that points to the location of the data file on the file system. You can do this using the following SQL statement:
CREATE DIRECTORY my_dir AS '/path/to/data/files';
- Grant read permission on the directory object to the user who will be querying the external table. You can do this using the following SQL statement:
GRANT READ ON DIRECTORY my_dir TO my_user;
- Create the external table using the CREATE TABLE statement. The table definition should include the
ORGANIZATION EXTERNAL
clause and aLOCATION
clause that specifies the directory object and file name.
CREATE TABLE my_table (
col1 NUMBER,
col2 VARCHAR2(20),
col3 DATE
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY my_dir
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
BADFILE my_dir:'my_table.bad'
LOGFILE my_dir:'my_table.log'
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
)
LOCATION ('my_table.csv')
)
In this example, the external table is defined to read data from a CSV file called
my_table.csv
located in the directory object my_dir
. The table has three columns: col1
is a NUMBER, col2
is a VARCHAR2, and col3
is a DATE. The ACCESS PARAMETERS
clause specifies how the data in the file should be interpreted, such as the field delimiter and the character encoding.
Once the external table is created, you can query it like any other table in the database using a SELECT statement. For example:
SELECT * FROM my_table;
Comments