EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle Temporary Table
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Oracle Temporary Table

By Priya PedamkarPriya Pedamkar

Oracle Temporary Table

Definition of Oracle Temporary Table

Oracle temporary table which was introduced in Oracle 8i can be defined as a permanent object where the data is stored in a disk and that particular data is immediately deleted after that particular session or transaction has come to an end and for Oracle private temporary tables are stored in the Oracle database memory and each of these tables are visible only to the session which created that particular table and these tables are generally SQL programs embedded in Oracle sessions.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Temporary tables are of two types: Global Temporary Table and Private Temporary Table introduced in Oracle 18c. When we create any temporary table in the Oracle database it is automatically global. So we need to add Global Keyword.

The syntax of the Global Oracle Temporary table is shown below:

CREATE GLOBAL TEMPORARY TABLE table_name
(column1 datatype [ constraint ],
column2 datatype [ constraint ],
...);

Parameters:

  • table_name: It refers to the name of the temporary table that the user is creating.
  • Column1, column2: It refers to the name of the columns
  • Constraint: It refers to the column constraint which we may or may not add.

How Temporary Table Works in Oracle?

Temporary Table in Oracle as defined earlier is used to store data for some specific task as the temporary table data is deleted as soon as the transaction or session ends or finishes. Suppose we want to extract some data which is not stored in a specific table and is not present in the database. In that case, we can use a procedure to first extract the data from different tables and insert that data into a temporary table during the start of the session or transaction. The data from a temporary table that is created during the starting of the session or transaction can be used throughout the session or throughout the transaction depending upon the permission provided while creating the table. If we give ON COMMIT DELETE ROWS then the table is transaction-specific but if we give ON COMMIT PRESERVE ROWS then the table is session-specific.

How to Create Oracle Temporary Table?

In the previous section of this article, we have learned about how the temporary table works in Oracle. In this section, we will see how we could create a temporary table. The temporary table is just like another table but we have to use the keyword ‘GLOBAL TEMPORARY’ in the create table query. For this example, we will create a temporary table named temp_table having columns named id and description. The id column will be the Primary Key. Let us now look at the query for creating a temporary table.

CREATE GLOBAL TEMPORARY TABLE temp_table (
id NUMBER PRIMARY KEY,
description VARCHAR2(20)
)
ON COMMIT DELETE ROWS;

We can see in the above query that the create table statement ends with ON COMMIT DELETE ROWS which means that the table will be deleted after every transaction. So it is transaction-based. If we want the table created to be session-based which means the data will be present in the temporary table for the whole session and not only the current transaction. Then we need to modify the SELECT query by adding ON COMMIT PRESERVE ROWS clause at the end of the statement.

CREATE GLOBAL TEMPORARY TABLE temp_table (
id NUMBER PRIMARY KEY,
description VARCHAR2(20)
)
ON COMMIT PRESERVE ROWS;

Let us now run the query in SQL developer.

SQL developer-1.1

As we can see in the above screenshot that the temporary table has been created.

How to Insert Data Into Oracle Temporary Table?

Since in the earlier section we can see that the temporary table temp_table has been already created, let us now insert data into the temporary table. In this example, we will insert data into the two columns present in the temp_table. We will not commit the insert statement as we know that the table data contents are transaction specific, so as soon as we sent to commit the data will get deleted. Let us look at the query for the same.

INSERT INTO temp_table VALUES (1, 'First entry');

Let us now run the query in the SQL developer.

SQL developer-1.2

As we can see in the above screenshot the row has been inserted into the table.

Let us now use a SELECT query to check the contents of the table. One point to keep in my mind that since this table is transaction specific we will lose the data as soon as we commit the insert statement. So, let us check the contents of the data before we commit the statement.

BEFORE COMMIT

SELECT * FROM temp_table;

Let us run the query in SQL developer.

SQL developer-1.3

In the above screenshot, we can see the contents of the table. Let us now commit the transaction and check the rows present in the table.

AFTER COMMIT

SELECT COUNT(*) FROM temp_table;

Let us see the output in the SQL developer.

Oracle Temporary Table-1.4

As per the screenshot, the data got successfully deleted as the count is Zero.

How to Delete?

If the table is created with ON COMMIT DELETE ROWS claws then if we simply commit the data will get deleted as already shown in the previous section. In case the table is created with ON DELETE PRESERVE ROWS then we will use the DELETE statement to delete the rows of the temporary table. Let us look at the query for the same.

DELETE from temp_table2 WHERE id =1;

Let us run the query in SQL developer.

Oracle Temporary Table-1.5

As we can see in the screenshot the row has been deleted successfully.

SELECT COUNT(*) FROM TEMP_TABLE2;

Oracle Temporary Table-1.6

As we can in the screenshot the count present is zero. Hence the delete statement successfully deleted the contents of the table.

Recommended Articles

This is a guide to Oracle Temporary Table. Here we also discuss the Introduction and how the temporary table works in oracle? along with how to delete and insert data into an oracle temporary table. You may also have a look at the following articles to learn more –

  1. Oracle Self Join
  2. Oracle While Loop
  3. Oracle Window Functions
  4. Oracle MD5
Popular Course in this category
Oracle Training (17 Courses, 8+ Projects)
  17 Online Courses |  8 Hands-on Projects |  140+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
Financial Analyst Masters Training Program4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more