How to create temporary tables and import data in Oracle?
To create a temporary table and import data in Oracle, follow these steps:
- Use the “CREATE GLOBAL TEMPORARY TABLE” statement to create a temporary table. For example, create a temporary table named “temp_table” with columns “id” and “name”.
CREATE GLOBAL TEMPORARY TABLE temp_table (
id NUMBER,
name VARCHAR2(50)
) ON COMMIT DELETE ROWS;
- Use the INSERT INTO statement to import data into a temporary table. For example, insert the data (1, ‘John’) and (2, ‘Jane’) into temp_table.
INSERT INTO temp_table (id, name) VALUES (1, 'John');
INSERT INTO temp_table (id, name) VALUES (2, 'Jane');
- In queries where temporary tables are needed, temporary tables can be used just like regular tables. For example, data can be retrieved from temporary tables using a SELECT statement.
SELECT * FROM temp_table;
Things to note:
- The temporary table created is a global temporary table, which automatically deletes all rows in the table after the session ends. If you need to delete all rows in the table after the transaction ends, you can use the ON COMMIT DELETE ROWS option.
- The structure of a global temporary table is shared across sessions, but the data is private to each session.
- Temporary tables will be automatically deleted after the session ends, so there is no need to manually delete them and they will not take up table space.
- Using temporary tables can improve query performance, especially when dealing with large amounts of data.