Comparison between temporary tables of Oracle and SQL Server

Source: Internet
Author: User
Tags table definition
In Oracle8i or a later version, you can create two types of temporary tables:
1. Session-specific temporary table
Create global temporary <TABLE_NAME> (<column specification>)
On commit preserve rows;

2. Temporary tables specific to transactions
Create global temporary <TABLE_NAME> (<column specification>)
On commit delete rows;
Create global temporary table MyTempTable
Although the created temporary table exists, you can try to insert a record and then mount it to select using other connections. The record is empty. See it, I will post the following two sentences:
-- On commit delete rows indicates that the temporary table is specified by a transaction. After each COMMIT, ORACLE truncates the table (DELETE all ROWS)
-- On commit preserve rows indicates that the temporary table is specified by the session. When the session is interrupted, ORACLE truncates the table.
Conflict issues should not be considered.

A temporary table only saves the data used by the current session. The data only exists during the transaction or session.

Use the create global temporary table command to CREATE a temporary table. For a temporary table of the transaction type,
Data only exists during the transaction. For temporary tables of the session type, data exists during the session.

The session data is private to the current session. Each session can only view and modify its own data. DML lock not added
Temporary table data. The following statement controls the existence of rows.

● On commit delete rows table name ROWS are only visible during the transaction
● On commit preserve rows table name ROWS are visible throughout the session

You can create indexes, views, and starters for temporary tables. You can use the export and import tools to import and export tables.
Definition, but data cannot be exported. The table definition is visible to all sessions.

Temporary Tables Temporary table
1 Overview
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 save the data required in a transaction. When the session exits or the user commits a commit and rollback transaction, the data in the temporary table is automatically cleared,
However, the structure and metadata of the temporary table are stored in the user's data dictionary.
Temporary tables are only supported in Oracle8i and later products.
2. Details
Oracle temporary tables are divided into session-level temporary tables and transaction-level temporary tables.
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.
Transaction-level temporary table means that the data in the temporary table only exists in the transaction lifecycle. When a transaction ends (commit or rollback), Oracle automatically clears the data in the temporary table.
The data in the temporary table is only valid for the current session. Each session has its own temporary data and cannot access the data in the temporary table of other sessions. Therefore,
Temporary tables do not require DML locks. When a session ends (the user Exits normally and the user does not exit normally) or a transaction ends
Execute the truncate statement to clear the data in the temporary table. However, the data in the temporary table of other sessions is not cleared.
You can index a temporary table and create a view on the temporary table. Similarly, the index on the temporary table is temporary and only valid for the current session or transaction.
Temporary tables can have triggers.
3. Create a temporary table
The definition of a temporary table is visible to all sessions, but the data in the table is only valid for the current SESSION or transaction.
Creation method:
1) on commit delete rows defines how to create a transaction-level temporary table.
Create global temporary table admin_work_area
(Startdate DATE,
Enddate DATE,
Class CHAR (20 ))
On commit delete rows;
EXAMPLE:
SQL> CREATE GLOBAL TEMPORARY TABLE admin_work_area
2 (startdate date,
3 enddate date,
4 class char (20 ))
5 on commit Delete rows;
SQL> Create Table permernate (a number );
SQL> insert into admin_work_area values (sysdate, sysdate, 'temperary table ');
SQL> insert into permernate values (1 );
SQL> commit;
SQL> select * From admin_work_area;
SQL> select * from permernate;
A
1
2) on commit preserve rows defines how to create a session-level temporary table.
Create global temporary table admin_work_area
(Startdate DATE,
Enddate DATE,
Class CHAR (20 ))
On commit preserve rows;
EXAMPLE:

Session 1:
SQL> drop table admin_work_area;
SQL> CREATE GLOBAL TEMPORARY TABLE admin_work_area
2 (startdate DATE,
3 enddate DATE,
4 class CHAR (20 ))
5 on commit preserve rows;
SQL> insert into permernate values (2 );
SQL> insert into admin_work_area values (sysdate, sysdate, 'session temperary ');
SQL> commit;
SQL> select * from permernate;

A
----------
1
2

SQL> select * From admin_work_area;

Startdate enddate class
----------------------------------------
17-1 ?? -03 17-1 ?? -03 session temperary

Session 2:

SQL> select * From permernate;

A
----------
1
2

SQL> select * From admin_work_area;

Row not selected.

Session 2 cannot see the data in the temporary table in session 1.

4 similarities and differences between ORACLE temporary tables and SQL Server temporary tables

SQL SERVER temporary table
You can also create a temporary table. Temporary tables are similar to permanent tables, but temporary tables are stored in tempdb. They are automatically deleted when they are no longer used.
There are two types of temporary tables: local and global, both of which have different names, visibility, and availability. The name of the local temporary table starts with a single digit;
They are only visible to the current user connection; when the user is from Microsoft? SQL Server? 2000 the instance is deleted when it is disconnected. The name of the global temporary table is a mathematical symbol.
(##) After the table is created, it is visible to all users and deleted when all users that reference the table are disconnected from SQL Server.
For example, if you create a table named employees, anyone who has the security permission to use the table in the database can use the table unless it has been deleted.
If you create a local temporary table named # employees, you can only perform operations on the table and delete the table when the connection is disconnected. If you create a global temporary table named # employees
Any user in the data table can perform operations on the table. If the table is not used by other users after you create it, delete it when you disconnect it. If the table is created
Otherwise, SQL Server deletes the table after all users are disconnected.
Different:
1. the SQL SERVER temporary TABLE is a "memory TABLE", which is stored in the memory. The ORACLE temporary TABLE is stored in the data dictionary unless DROP TABLE is executed.
2. SQL SERVER temporary tables do not support transaction-level functions similar to ORACLE temporary tables.
3. the SQL SERVER local temporary table (#) is similar to the ORACLE session-level temporary table, but ORACLE does not delete the table when the session exits.
4. SQL server's global temporary table (#) means that multiple connections share the same piece of memory. When no pointer references the memory area, SQL SERVER Automatically releases the global temporary table.
5 because ORACLE is not a database in memory, if ORACLE frequently creates and deletes temporary tables like SQL SERVER, it will definitely affect the performance.
Therefore, ORACLE retains the definition of the temporary TABLE until you DROP the TABLE.
6 in ORACLE, if multiple users need to share a table (similar to SQL SERVER's global temporary table #), you can use permanent tables,
Add columns in the table that can uniquely identify the user. Use triggers and views. When the user exits, delete the data in the corresponding table based on the unique information of the login user.
This method brings a certain amount of load to ORACLE.

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.