Create and use two temporary tables in Oracle

Source: Internet
Author: User

Create and use two temporary tables in Oracle

In addition to permanent tables, the ORACLE database can also create temporary tables. These temporary tables are used to save the data of a SESSION or the data required in a transaction. When the session exits or the user commits a commit or rollback transaction, the data in the temporary table is automatically cleared, but the structure and metadata of the temporary table are also stored in the user's data dictionary.


Category:
1. Session-level temporary table

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

Format:

Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Preserve Rows;
2. Transaction-level temporary table
Transaction-level temporary table means that the data in the temporary table only exists in the transaction lifecycle.
Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Delete Rows;

When a transaction ends (commit or rollback), Oracle automatically clears the data in the temporary table.


The following shows how to create and use a temporary table in Oracle 10 GB:


1. Create a temporary transaction table, insert a piece of data, and query:

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

2. Execute the commit or rollback operation to clear the data in the temporary table, but the table structure still exists:


3. Create a session-level temporary table, insert a piece of data, and query:

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


4. Execute the commit or rollback operation. The data in the table still exists. Creating a new command window (equivalent to opening a new session) means that the data in the table cannot be queried:


5. if the session for creating a session temporary table does not end, the temporary table cannot be deleted because the temporary table is still in use but ends the session (close the command window for creating a session-level temporary table) then you can delete it:





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.