Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a temporary table vs global temporary table in Oracle?

I have heard these two terms "temporary table" and "global temporary table" used pretty much in similar context.

What is the difference between the two?

like image 234
MOZILLA Avatar asked Jan 06 '09 19:01

MOZILLA


People also ask

What is the difference between temporary table and global temporary table?

Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.

What is global temporary table in Oracle?

Global Temporary Tables (GTTs) are the Oracle tables, having data type as private; such that data inserted by a session can be accessed by that session only. The session-specific rows in a GTT can be preserved for the entire session, as AI report tables are created using ON COMMIT PRESERVE ROWS clause.

What is the difference between global temporary tables and volatile temporary tables?

Global Temporary table definition persist across session and Volatile table are session specific tables. Index can be created on Global Temporary table and not on Volatile table. In a single session 2000 Global temporary table can be materialized and 1000 Volatile tables can be materialized.

How do I know if a table is a global temporary table?

We can also use the following query to display all Oracle global temporary tables: select table_name from all_tables where temporary = 'Y';


2 Answers

Contrast this with MS SQL-Server, where temporary tables are local. If you create one, no one besides you knows that your temporary table exists. In Oracle, creating the temporary table allows everyone (well everyone with access to your schema) to see the table. When you log out of your session, the SQL-Server table is deleted and will need to be recreated for the next session. In Oracle, the temporary table is now a permanent part of your schema, even if the data isn't (if not so you can decide whether to preserve it). The Oracle supports only global temporary table saves you from having to create the table in each session; it 'exists' but it is empty, and its content is unique (and private) per session.

like image 27
Joby Thomas Avatar answered Sep 30 '22 19:09

Joby Thomas


In Oracle there isn't any difference. When you create a temporary table in an Oracle database, it is automatically global, and you are required to include the "Global" key word.

The SQL standard, which defines how the term "GLOBAL TEMPORARY TABLE" is interpreted, allows for either a LOCAL or GLOBAL scope. This would allow for either a user specific table (LOCAL) or everyone (GLOBAL). Oracle implements only the GLOBAL version.

The data you put into an Oracle Temporary table is specific to your session. That is, only you can see your data even if there are 100 users all using the same table, and your data is deleted from the table when you disconnect (or when you commit the current transaction) depending upon table settings.

Contrast this with MS SQL-Server, where temporary tables are local. If you create one, no one besides you knows that your temporary table exists. In Oracle, creating the temporary table allows everyone (well everyone with access to your schema) to see the table. When you log out of your session, the SQL-Server table is deleted and will need to be recreated for the next session. In Oracle, the temporary table is now a permanent part of your schema, even if the data isn't.

like image 50
Thomas Jones-Low Avatar answered Sep 30 '22 20:09

Thomas Jones-Low