Temporary Table:
A temporary table is a table that is created and used only for the duration of a transaction or a session. It is typically used to store intermediate results that are needed to perform complex queries or operations. Temporary tables are automatically dropped at the end of a transaction or a session. Temporary tables are useful in scenarios where you need to perform multiple queries on the same set of data or when you want to store intermediate results for further processing.
Syntax:
CREATE GLOBAL TEMPORARY TABLE table_name
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
) ON COMMIT [ PRESERVE ROWS | DELETE ROWS ];
Here, table_name is the name of the temporary table that you want to create, and datatype represents the data type of the columns in the table. You can define as many columns as needed, each separated by a comma.
The ON COMMIT clause is used to specify when the data in the temporary table should be cleared. You can choose to PRESERVE ROWS, which means that the data will be preserved until the end of the session, or DELETE ROWS, which means that the data will be deleted as soon as the transaction ends.
Example:
Here's an example of creating a temporary table:
CREATE GLOBAL TEMPORARY TABLE temp_customers
(
id NUMBER(10) NOT NULL,
name VARCHAR2(50) NOT NULL,
email VARCHAR2(50),
phone VARCHAR2(20),
city VARCHAR2(30),
country VARCHAR2(30)
) ON COMMIT PRESERVE ROWS;
This creates a temporary table called temp_customers with six columns: id, name, email, phone, city, and country. The data in this table will be preserved until the end of the session.
You can insert data into this table just like you would with a regular table:
INSERT INTO temp_customers (id, name, email, phone, city, country)
VALUES (1, 'John Doe', 'johndoe@example.com', '555-1234', 'New York', 'USA');
And you can select data from the temporary table just like any other table:
SELECT * FROM temp_customers;
External Table:
An external table is a table-like object that represents data stored outside of a database. It is a read-only table, which means that you cannot modify the data in the external table. You can create an external table to access data in a flat file, a remote database, or any other data source accessible by the database server. The structure of an external table is defined by a set of metadata that describes the data's layout, format, and location. An external table is accessed just like a regular database table, and you can query it using SQL.
Syntax:
CREATE TABLE table_name (
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
)
ORGANIZATION EXTERNAL (
TYPE type_name
DEFAULT DIRECTORY directory_name
ACCESS PARAMETERS (
param1_name [ = value1 ],
param2_name [ = value2 ],
...
)
LOCATION ( 'file_name' [ , 'file_name' ]* )
);
Let's break down the different parts of this syntax:
- CREATE TABLE: The statement used to create a new table in the database.
- table_name: The name of the table you want to create.
- column1, column2, etc.: The columns of the table, along with their data types.
- NULL | NOT NULL: An optional constraint that specifies whether a column can contain NULL values or not.
After the column definitions, you specify the ORGANIZATION EXTERNAL clause to indicate that you want to create an external table.
- TYPE type_name: The type of external table you want to create. For example, you can specify ORACLE_LOADER for a table that reads data from a flat file using SQL*Loader.
- DEFAULT DIRECTORY directory_name: The directory in the file system where the external data files are located. This directory must be defined in the database using the CREATE DIRECTORY statement.
- ACCESS PARAMETERS: A set of parameters that configure how the external table accesses the external data files.
- param1_name [ = value1 ], param2_name [ = value2 ], ...: The name and value of each parameter. The parameters you use depend on the type of external table you're creating.
- LOCATION ( 'file_name' [ , 'file_name' ]* ): The name or names of the external data files. You can specify multiple files separated by commas.
Example: 01
Here's an example of how you might define an external table that reads data from a CSV file using the ORACLE_LOADER type:
CREATE TABLE my_external_table (
id NUMBER,
name VARCHAR2(100),
salary NUMBER
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY my_data_dir
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
)
LOCATION ('employees.csv')
);
In this example, we're creating an external table called my_external_table that has three columns: id, name, and salary. The table is configured to use the ORACLE_LOADER type to read data from a CSV file called employees.csv located in the my_data_dir directory. We're also specifying some access parameters to indicate that the data is delimited by newlines and fields are separated by commas.
Example: 02
Suppose you have a CSV file containing sales data. You can create an external table to read data from that CSV file using the following SQL statement:
CREATE TABLE sales_ext (
id NUMBER,
product VARCHAR2(50),
sale_date DATE,
sale_amount NUMBER
)
ORGANIZATION EXTERNAL (
TYPE CSV
DEFAULT DIRECTORY data_dir
LOCATION ('sales.csv')
);
In summary, an external table is used to access data stored outside the database, while a temporary table is used to store intermediate results within the database for the duration of a transaction or a session.