Oracle can hold session-private data which means data is stored for the duration of transaction or session. Two types of temporary tables can be created with “create global temporary table…” statement, transaction-specific or session-specific. These names are the data existence for the temporary tables. For transaction-specific tables, data is stored during the transaction, or for session-specific transaction during the session. The rows are private to the session in temporary tables to modify its own data.
There are lots of characteristics like permanent tables. Temporary tables can be indexed and analyzed, but indexes created on temporary tables will be temporary too. Views and triggers can be created on temporary tables. Also, export and import utilities can be used with temporary tables.
In Oracle, data changes are stored in redo logs. But, for temporary tables, DML statements <insert , update, delete> do not generate redo logs. However, temporary tables generate a minimum amount of redo. They are not stored in rollback segments.
Oracle allocate temporary segments for the user needs. Data are deallocated at the end of the session or transaction.
The syntax of temporary tables is “create global temporary table…” .
The statement “… on commit delete rows” is used to specify that rows are only visible within the transaction.
“…on commit preserve rows” is also used to specify that rows are visible for the session.
A note from High Performance Tuning by Guy Harrison:
“Take advantage of temporary tables when your Pl/Sql needs to store data that does not need to persist beyond the session or the transaction”.
I will explain the difference between permanent tables and temporary tables with a simple example:
*The first part is about permanent tables.
First, we connect to the database:
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr
Then, create a table named test with a parameter id type number:
SQL> create table test (id number);
Table created
After that, insert a row with a value of id 1:
SQL> insert into test values(1);
1 row inserted
When we show the table’s contents:
SQL> select * from test;
ID
———-
1
At the end of these executions, disconnect from the database:
SQL> disconnect;
Not logged on
Again we connect to the database, and show the data in our test table:
SQL> connect hr/hr;
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr
SQL> select * from test;
ID
———-
1
It seems that, Oracle hold data in permanent tables until you delete the row, truncate or drop the table.
*The second part of the example is about temporary tables.
We drop the permanent table and create a new one that is temporary:
SQL> drop table test purge;
Table dropped
Following this, create a temporary table which hold the data during the session:
SQL> create global temporary table test (id number) on commit preserve rows;
Table created
SQL> insert into test values (2);
1 row inserted
SQL> select * from test;
ID
———-
2
Again disconnect from the database:
SQL> disconnect;
Not logged on
Connect once more:
SQL> connect hr/hr;
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr
Finally, as select the data from the test table, there is no data to be stored:
SQL> select * from test;
ID
———-
In conclusion, temporary tables are useful in applications which you need to store data for transaction or session level.