To create a table named test with column col1 type varchar2 length 10, col2 type number. col3 type clob we can use CREATE TABLE statement as,
CREATE TABLE TEST(col1 VARCHAR2(10),
col2 NUMBER,
col3 CLOB);
Now if I insert data into the table the data is visible and accessible to all users. In many cases it is needed the data inside a table will be reside temporarily. In that case we can use temporary tables. Temporary tables are useful in applications where a result set is to be buffered. To create temporary table we have to issue CREATE GLOBAL TEMPORARY clause.
Temporary table can be of two types based on ON COMMIT clause settings.
1)ON COMMIT DELETE ROWS specifies temporary table would be transaction specific. Data persist within table up to transaction ending time. If you end the transaction the database truncates the table (delete all rows). Suppose if you issue commit or run ddl then data inside the temporary table will be lost. It is by default option.
2)ON COMMIT PRESERVE ROWS specifies temporary table would be session specific. Data persist within table up to session ending time. If you end the session the database truncates the table (delete all rows). Suppose you type exit in SQL*Plus then data inside the temporary table will be lost.
1)This statement creates a temporary table that is transaction specific:
SQL>CREATE GLOBAL TEMPORARY TABLE test_temp
(col1 number,
col2 number)
ON COMMIT DELETE ROWS;
Table created.
2)Insert row in to the temporary table.
SQL> insert into test_temp values(1,2);
1 row created.
3)Look at the data in the table.
SQL> select * from test_temp;
COL1 COL2
---------- ----------
1 2
4)Issue Commit.
SQL> commit;
Commit complete.
5)Now look at the data in the temporary table. As I created transaction specific temporary table(on commit delete rows) so data is lost after commit.
SQL> select * from test_temp;
no rows selected
Example of Session Specific Temporary Tables.
----------------------------------------------------
1)Create Session Specific Temporary Table test_temp2.
CREATE GLOBAL TEMPORARY TABLE test_temp2
(col1 number,
col2 number)
ON COMMIT PRESERVE ROWS;
2)Insert data into it and look at data both before commit and after commit.
SQL> insert into test_temp2 values(3,4);
1 row created.
SQL> select * from test_temp2;
COL1 COL2
---------- ----------
3 4
SQL> commit;
Commit complete.
SQL> select * from test_temp2;
COL1 COL2
---------- ----------
3 4
3)End the Session.
SQL> exit;
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
4)Connect in a new session and look at data again.
-bash-3.00$ sqlplus arju/a
SQL*Plus: Release 10.2.0.1.0 - Production on Tue Jun 10 00:06:27 2008
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select * from test_temp2;
no rows selected
Note: Never confuse with the GLOBAL keyword. After seeing GLOBAL keyword you may thing LOCAL keyword may have. But it is not true. To create temporary table we have to specify GLOBAL keyword. No LOCAL keyword exist there.
Related Documents:
---------------------------
Feature and Restriction of Temporary Table
Monday, June 9, 2008
Subscribe to:
Post Comments (Atom)
Tag Cloud
10.2g
10g
11g
11gR2
Abasa
About Oracle
Administration
Adsense
Alerts
Archival
ASM
ASP.Net
Audit
Audit Vault
Backup
Bangladesh
Block Corruption
Blogger
Browser
Bug
Business
Clone
Clusterware
Comments
Concepts
Connection
Controlfiles
Crime
CSS
Data Block
Data Dictionary
Data Guard
Data Mining
Data Pump
Data Type
Database Administration
Database Vault
DBConsole
Developer
Economics
EM
Excel
Exercise
Explain plan
Export
External Table
Facebook
Firefox
Firmware
Flashback
Forum
Functions
Games
Globalization Support
Grid Control
Hardware
History
HTML
IE
Import
Indexes
initializaion parameter
initialization parameter
Installation
Internals
Internet
Interview
isql*plus
Java
JavaScript
Job
Joins
Joke
Limitation
Linux
Listener
Logminer
Magento
Mail
Materialized View
Medical
Memory
Mobile
Money
Multimedia
MySQL
Net Services
Network
OCP
Operators
Oracle
Oracle Concepts
Oracle Recovery
OS
Others
OUI
Package
Packages
Parameters
Partitioning
Patchset
Performance
Perl
Pfile
Photos
PHP
PL/SQL
Profile
Pseudocolumns
Puzzle
Quiz
Quota
RAC
RAC Installation
Recovery
Recovery Problems
Redo Log
Reports
RMAN
Scripts
Security
SEO
Server Administration
SGA
Shell Script
Smarty
Social Marketing
Solaris
Spfile
SQL
SQL Tuning
SQL*Loader
Sql*Plus
Startup Problem
Streams
SwingBench
System Analysis
Tablespaces
Technology
Temp
TNS Error
Tools
Troubleshooting
Tuning
Undo
UNIX
Upgradation
Utilities
Version
Views
Vmware
Windows
Wordpress
XML
7 comments:
When I do this:
1. I create a global temp table with COMMIT PRESERVE ROWS with no indexes
2. insert a row
3. attempt to drop above table
I get this message:
ORA-14452: attempt to create, alter or drop an index on temporary table already in use
How do I get around this?
Truncate the table stupid, then drop it.
thanks, anon
don't call people stupid on this forum please... you are stupid to leave stupid comments like this. You should really look at a mirror everyday and see how stupid your stupid face is stupid.
Hi, dont quarrel here. It is very good forum and we everyday read comments.
guess he wanted so say: truncate it a couple of times, then drop it
Hi don't quarrel.
This post help me a lot.
Thank you Arju.
Post a Comment