36.1k views
3 votes
How to create a temporary table in oracle

User Kurren
by
7.4k points

1 Answer

2 votes

Final answer:

To create a temporary table in Oracle, use the 'CREATE GLOBAL TEMPORARY TABLE' statement specifying your table name, column definitions, and either 'ON COMMIT DELETE ROWS' or 'ON COMMIT PRESERVE ROWS' depending on your needs.

Step-by-step explanation:

To create a temporary table in Oracle, you can use the CREATE GLOBAL TEMPORARY TABLE statement. This statement creates a table that is temporary to the session or transaction depending on the option you choose. A temporary table is useful for storing intermediate results that you need to process other data. Here is a step-by-step explanation on how to create one:

Step 1: Connect to the Oracle Database using your preferred SQL client or command-line interface.

Step 2: Begin your SQL statement with CREATE GLOBAL TEMPORARY TABLE followed by your desired table name.

Step 3: Define your columns and their data types within parentheses, just like you would with a regular table.

Step 4: Specify either ON COMMIT DELETE ROWS for a transaction-specific temporary table or ON COMMIT PRESERVE ROWS for a session-specific temporary table.

Step 5: Execute the statement to create the table.

An example statement might look something like:

CREATE GLOBAL TEMPORARY TABLE my_temp_table (
id INT,
name VARCHAR2(50)
) ON COMMIT DELETE ROWS;

Remember that the data in a temporary table is private to the session and will be lost when the session ends or when the transaction is completed, depending on your ON COMMIT option.

User Thomas Hsieh
by
7.3k points