首页 > 数据库 > Oracle > temporary table

temporary table

原创 Oracle 作者:lovehewenyu 时间:2012-04-24 11:05:41 0 删除 编辑

Creating a Temporary Table

It is also possible to create a temporary table. The definition of a temporary table is visible to all sessions, but the data in a temporary table is visible only to the session that inserts the data into the table. Use the CREATE GLOBAL TEMPORARY TABLEstatement to create a temporary table. The ON COMMITclause indicate if the data in the table is transaction-specific (the default) or session-specific, the implications of which are as follows:




This creates a temporary table that is transaction specific. A session becomes bound to the temporary table with a transactions first insert into the table. The binding goes away at the end of the transaction. The database truncates the table (delete all rows) after each commit.


This creates a temporary table that is session specific. A session gets bound to the temporary table with the first insert into the table in the session. This binding goes away at the end of the session or by issuing a TRUNCATEof the table in the session. The database truncates the table when you terminate the session.

Temporary tables are useful in applications where a result set is to be buffered, perhaps because it is constructed by running multiple DML operations. For example, consider the following:

A Web-based airlines reservations application allows a customer to create several optional itineraries. Each itinerary is represented by a row in a temporary table. The application updates the rows to reflect changes in the itineraries. When the customer decides which itinerary she wants to use, the application moves the row for that itinerary to a persistent table.

During the session, the itinerary data is private. At the end of the session, the optional itineraries are dropped.

This statement creates a temporary table that is transaction specific:

CREATE GLOBAL TEMPORARY TABLE admin_work_area         (startdate DATE,          enddate DATE,          class CHAR(20))       ON COMMIT DELETE ROWS; 

Indexes can be created on temporary tables. They are also temporary and the data in the index has the same session or transaction scope as the data in the underlying table.

Unlike permanent tables, temporary tables and their indexes do not automatically allocate a segment when they are created. Instead, segments are allocated when the first INSERT(or CREATETABLEASSELECT) is performed. This means that if a SELECT, UPDATE, or DELETEis performed before the first INSERT, the table appears to be empty.

DDL operations (except TRUNCATE) are allowed on an existing temporary table only if no session is currently bound to that temporary table.

If you rollback a transaction, the data you entered is lost, although the table definition persists.

A transaction-specific temporary table allows only one transaction at a time. If there are several autonomous transactions in a single transaction scope, each autonomous transaction can use the table only as soon as the previous one commits.

Because the data in a temporary table is, by definition, temporary, backup and recovery of temporary table data is not available in the event of a system failure. To prepare for such a failure, you should develop alternative methods for preserving temporary table data.




临时表有2种类型的临时表,即事务级(delete rows)和会话级(perserve rows)

Delete rows:当你commit或rollback 临时表数据都会被清空。

Preserve rows: 当你当前session退出 临时表数据会被清空。



事务级: 网络商城选购物品,选的都可以放进购物车(temparmry table记录上),当你选定购买物品,并取消购物车物品时,(COMMIT)这个时候临时表数据被清空。你选定的物品被记录到真实表中。(表结构还存在)




来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录


  • 博文量
  • 访问量