Tuesday, April 14, 2009

Global Temporary Tables

CREATE GLOBAL TEMPORARY TABLE was introduced in Oracle 8i.The CREATE GLOBAL TEMPORARY TABLE command creates a temporary table which can be transaction specific or session specific. For transaction-specific temporary tables, data exists for the duration of the transaction while for session-specific temporary tables, data exists for the duration of the session. Data in a temporary table is private to the session. Each session can only see and modify its own data.

Transaction specific table:
CREATE GLOBAL TEMPORARY TABLE T_TEMP (
column1 NUMBER,
column2 NUMBER
) ON COMMIT DELETE ROWS;

Session Specific table

CREATE GLOBAL TEMPORARY TABLE T_TEMP
(
column1 NUMBER,
column2 NUMBER
) ON COMMIT PRESERVE ROWS;
Features:

Data in temporary tables is automatically deleted at the end of the database session,, no matter what happens.
If the TRUNCATE statement is carried out on a temporary table, only the session specific data is truncated. Other sessions are not affectd.
We can perform DDL commands like ALTER TABLE, DROP TABLE etc only when no session is currently bound to it. A session gets bound to a temporary table when an INSERT is performed on it. The session gets unbound by a TRUNCATE, at session termination, or by doing a COMMIT or ABORT for a transaction-specific temporary table.

No comments:

Post a Comment