Temporary table use - the creation and use of oracle two temporary tables

The temporary table of the oracle database can be used to save the data of a session session, or the data in a transaction. When the query statement is very complex, we can create a temporary table to save the query results and perform a series of operations.

1. Create a temporary table


There are two ways to create a temporary table:
    1. Session-level temporary table on commit delete rows; delete data when COMMIT (default)
    2. Transaction-level temporary table on commit preserve rows; retain data when COMMIT

1. Session-level temporary table


A session-level temporary table means that the data in the temporary table only exists during the session life cycle. When the user exits the session, Oracle automatically clears the data in the temporary table.

Execute the commit or rollback operation, the data in the table still exists, create a new command window (equivalent to opening a new session), and the data in the table will not be queried.
Format:
 

Create Global Temporary Table Temp_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Preserve Rows;

example:

create global temporary table temp_tb (col1 varchar(20)) on commit preserve rows;
insert into temp_tb values('test');
select * from temp_tb;

2. Transaction-level temporary table 


A transaction-level temporary table means that the data in the temporary table only exists during the transaction life cycle. When a transaction ends (commit or rollback), Oracle automatically clears the data in the temporary table.

Create Global Temporary Table Temp_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Delete Rows;

example

create global temporary table temp_tb (col1 varchar(20)) on commit delete rows;
insert into  temp_tb values('test');
select * from  temp_tb;

2. Save the query results in a temporary table that did not exist before 


Example: this is session-level 

CREATE GLOBAL TEMPORARY TABLE tmptable  
ON COMMIT PRESERVE ROWS   
AS  
SELECT *  
FROM tablename  

Guess you like

Origin blog.csdn.net/u010919402/article/details/127690316