Monday, June 9, 2008

Create Temporary Table in Oracle

To create a table named test with column col1 type varchar2 length 10, col2 type number. col3 type clob we can use CREATE TABLE statement as,

CREATE TABLE TEST(col1 VARCHAR2(10),
col2 NUMBER,
col3 CLOB);


Now if I insert data into the table the data is visible and accessible to all users. In many cases it is needed the data inside a table will be reside temporarily. In that case we can use temporary tables. Temporary tables are useful in applications where a result set is to be buffered. To create temporary table we have to issue CREATE GLOBAL TEMPORARY clause.

Temporary table can be of two types based on ON COMMIT clause settings.

1)ON COMMIT DELETE ROWS specifies temporary table would be transaction specific. Data persist within table up to transaction ending time. If you end the transaction the database truncates the table (delete all rows). Suppose if you issue commit or run ddl then data inside the temporary table will be lost. It is by default option.

2)ON COMMIT PRESERVE ROWS
specifies temporary table would be session specific. Data persist within table up to session ending time. If you end the session the database truncates the table (delete all rows). Suppose you type exit in SQL*Plus then data inside the temporary table will be lost.

1)This statement creates a temporary table that is transaction specific:

SQL>CREATE GLOBAL TEMPORARY TABLE test_temp
(col1 number,
col2 number)
ON COMMIT DELETE ROWS;

Table created.

2)Insert row in to the temporary table.
SQL> insert into test_temp values(1,2);
1 row created.

3)Look at the data in the table.
SQL> select * from test_temp;
COL1 COL2
---------- ----------
1 2
4)Issue Commit.
SQL> commit;
Commit complete.

5)Now look at the data in the temporary table. As I created transaction specific temporary table(on commit delete rows) so data is lost after commit.

SQL> select * from test_temp;

no rows selected

Example of Session Specific Temporary Tables.
----------------------------------------------------

1)Create Session Specific Temporary Table test_temp2.

CREATE GLOBAL TEMPORARY TABLE test_temp2
(col1 number,
col2 number)
ON COMMIT PRESERVE ROWS;


2)Insert data into it and look at data both before commit and after commit.
SQL> insert into test_temp2 values(3,4);
1 row created.

SQL> select * from test_temp2;

COL1 COL2
---------- ----------
3 4

SQL> commit;
Commit complete.

SQL> select * from test_temp2;

COL1 COL2
---------- ----------
3 4

3)End the Session.
SQL> exit;
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
4)Connect in a new session and look at data again.
-bash-3.00$ sqlplus arju/a
SQL*Plus: Release 10.2.0.1.0 - Production on Tue Jun 10 00:06:27 2008

Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select * from test_temp2;

no rows selected

Note: Never confuse with the GLOBAL keyword. After seeing GLOBAL keyword you may thing LOCAL keyword may have. But it is not true. To create temporary table we have to specify GLOBAL keyword. No LOCAL keyword exist there.


Related Documents:
---------------------------

Feature and Restriction of Temporary Table

6 comments:

  1. When I do this:
    1. I create a global temp table with COMMIT PRESERVE ROWS with no indexes
    2. insert a row
    3. attempt to drop above table

    I get this message:

    ORA-14452: attempt to create, alter or drop an index on temporary table already in use

    How do I get around this?

    ReplyDelete
  2. Truncate the table stupid, then drop it.

    ReplyDelete
  3. don't call people stupid on this forum please... you are stupid to leave stupid comments like this. You should really look at a mirror everyday and see how stupid your stupid face is stupid.

    ReplyDelete
  4. Hi, dont quarrel here. It is very good forum and we everyday read comments.

    ReplyDelete
  5. guess he wanted so say: truncate it a couple of times, then drop it

    ReplyDelete