Sunday, October 19, 2014

What are Global Temporary Tables in Oracle?


Global Temporary Tables, introduced by Oracle since 8i and mainly they are the private tables which stored data inserted by a session only and only accessed by that session itself.
Widely used in large applications make extensive use of temporary data storage which preserves data for the whole session or just for current transaction.

CREATE GLOBAL TEMPORARY TABLE GTT_TEMP  
(
AAAA  Number,
BBBB  Varchar2(10),
CCCC  Number
)
ON COMMIT [DELETE | PRESERVE] ROWS;

ON COMMIT DELETE ROWS : It sets the life of the data contained by the table to a single TRANSACTION.
The data is automatically flushed away after each COMMIT/ROLLBACK is executed - such tables are Transaction-specific Temporary tables.

ON COMMIT PRESERVE ROWS: It restricts the life of the data to a single SESSION.
Data is preserved in the table for a session only - such tables are Session-specific Temporary tables.

Below are the major features due to that it is used in large applications:


  • If the TRUNCATE statement is issued against a temporary table, only the session specific data is trucated. There is no affect on the data of other sessions.
  • Data in temporary tables is automatically deleted at the end of the database session, even if it ends abnormally.
  • Indexes can be created on temporary tables. The content of the index and the scope of the index is the same as the database session.
  • Views can be created against temporary tables and combinations of temporary and permanent tables.
  • Triggers can alo be associated with such tables.
  • Export and Import utilities can be used to transfer the table definitions, but no data rows are processed.
  • Table Statistics of temporary tables are common to all sessions.
  • Foreign key constraints are not applicable in case of Temporary tables.
  • Global Temporary tables cannot be partitioned.
  • Data in Global Temporary Tables is stored in temp segments in the temp tablespace only.
  • In Oracle 8i,9i & 10g, TABLESPACE cannot be defined for GTT i.e. GTT segments were created in user’s default tablespace. 
  • After Oracle 11g, GTT segments can be created on other Temporary Tablespaces too, it must be create on a TEMP Tablespace not any other.
  • Use of LOB_STORAGE_CLAUSE, LOGGING/ NOLOGGING, MONITORING/ NOMONITORING, LOB_INDEX_CLAUSE is restricted in GTT definition. 


PIVOT & UNPIVOT functions in Oracle 11g

PIVOT Function is used in oracle 11g to produce query output in matrix style same as what we are doing in xls or xlsx.

in oracle 10g,

select deptid,jobid,sum(sal) from emp
group by deptid,jobid
order by deptno,jobid;  

for to get ouput of a query like in Excel Pivot funcationality.

select deptid, 
sum(decode(jobid, 101, sal)) "Analyst",
sum(decode(jobid, 102, sal)) "Programmer",
sum(decode(jobid, 103, sal)) "DBA",
sum(decode(jobid, 104, sal)) "Developer"
from emp
group by deptid
order by deptid;

Using the same output in oracle 11g with below query.

select * from(select deptid,jobid,sal from emp)
PIVOT (sum(sal) FOR jobid 
in (
101 "Analyst",
102, "Programmer",
103, "DBA",
104, "Developer"
))order by deptid;

same way we can achieve the first query output using UNPIVOT function.

select * from(select deptid,jobid,sal from emp)
UNPIVOT (sum(sal) FOR jobid 
in (
101, "Analyst",
102, "Programmer",
103, "DBA",
104, "Developer",
))order by deptid;