05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

404<br />

CHAPTER 10 ■ DATABASE TABLES<br />

ops$tkyte@ORA10G> create global temporary table temp_table_session<br />

2 on commit preserve rows<br />

3 as<br />

4 select * from scott.emp where 1=0<br />

5 /<br />

Table created.<br />

The ON COMMIT PRESERVE ROWS clause makes this a session-based temporary table. Rows<br />

will stay in this table until my session disconnects or I physically remove them via a DELETE or<br />

TRUNCATE. Only my session can see these rows; no other session will ever see “my” rows, even<br />

after I COMMIT.<br />

ops$tkyte@ORA10G> create global temporary table temp_table_transaction<br />

2 on commit delete rows<br />

3 as<br />

4 select * from scott.emp where 1=0<br />

5 /<br />

Table created.<br />

The ON COMMIT DELETE ROWS makes this a transaction-based temporary table. When my<br />

session commits, the rows disappear. The rows will disappear by simply giving back the temporary<br />

extents allocated to my table—there is no overhead involved in the automatic clearing<br />

of temporary tables.<br />

Now, let’s look at the differences between the two types:<br />

ops$tkyte@ORA10G> insert into temp_table_session select * from scott.emp;<br />

14 rows created.<br />

ops$tkyte@ORA10G> insert into temp_table_transaction select * from scott.emp;<br />

14 rows created.<br />

We’ve just put 14 rows into each TEMP table, <strong>and</strong> this shows we can “see” them:<br />

ops$tkyte@ORA10G> select session_cnt, transaction_cnt<br />

2 from ( select count(*) session_cnt from temp_table_session ),<br />

3 ( select count(*) transaction_cnt from temp_table_transaction );<br />

SESSION_CNT TRANSACTION_CNT<br />

----------- ---------------<br />

14 14<br />

ops$tkyte@ORA10G> commit;<br />

Since we’ve committed, we’ll see the session-based rows but not the transaction-based<br />

rows:<br />

ops$tkyte@ORA10G> select session_cnt, transaction_cnt<br />

2 from ( select count(*) session_cnt from temp_table_session ),<br />

3 ( select count(*) transaction_cnt from temp_table_transaction );

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!