Functions and Applications Oracle temporary table

What is a temporary table, you do a query for the hundreds of thousands of pieces of data, we can put the data in memory. When there are many users do this, out of memory space, this time on the need to save the data on the disk. Oracle provides for a temporary table for storing the data.
 
Transaction Level
Session Level
 
on commit preserve rows --session temporary table
on commit delete rows --transaction temporary table
 
The following demonstrates creating a session-level transaction level and two temporary tables
 
SQL> conn test/test
Connected.
SQL> desc test; 
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER
 NAME                                               VARCHAR2(10)

 

Creating sessione level in the sys temporary table
SQL> conn / as sysdba
Connected.
SQL> create global temporary table session_test on commit preserve rows as select * from test.test;
Table created.
SQL> commit; Commit complete.

  

on commit preserve rows of a table is created session level, as long as the user does not exit, temporary tables remain in effect.
 
SQL> select * from session_test;

        ID NAME
---------- ----------
         0 orcl
         1 orcl
         3 orcl
         2 orcl

  

Exit and re-enter and verify the existence of the table of contents.

SQL> exit
……

SQL> select * from session_test; no rows selected SQL>

  

View session-level temporary table after disconnected, log back view becomes empty.

 

Next, create transaction-level temporary table

SQL> create global temporary table transaction_test on commit delete rows as select * from test.test;

Table created.

  

on commit delete rows of a table is to create a transaction level, the scope of the temporary table is valid only within a transaction.

Verify the transaction scope of temporary tables:
Although the copy of test.test table, but still there is no data.

SQL> select * from transaction_test;

no rows selected

  

Next add data to a temporary table transaction_test Affairs

SQL> insert into transaction_test select * from test.test; 

4 rows created.

SQL> select * from transaction_test;

        ID NAME
---------- ----------
         0 orcl
         1 orcl
         3 orcl
         2 orcl

  

As we will test.test table of contents is inserted into the transaction_test table and direct access, can be found just inserted.

Next submission and queries. (Submitted equivalent to commit the transaction, which is the end of a transaction)

SQL> select * from transaction_test;

        ID NAME
---------- ----------
         0 orcl
         1 orcl
         3 orcl
         2 orcl

SQL> commit;

Commit complete.

SQL> select * from transaction_test;

no rows selected

  

This shows

Temporary table session-level, as long as the conversation continued on, temporary tables remain in effect after the session is disconnected, empty the contents of the temporary table;
Temporary table transaction level, is only effective after the transaction commits, empty the contents of the temporary tables within a transaction.

 

Guess you like

Origin www.cnblogs.com/black-start/p/11007967.html