Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Oracle can you create a table that only exists while the database is running?

Tags:

sql

oracle

Is there a way in Oracle to create a table that only exists while the database is running and is only stored in memory? So if the database is restarted I will have to recreate the table?

Edit: I want the data to persist across sessions. The reason being that the data is expensive to recreate but is also highly sensitive.

Using a temporary table would probably help performance compared to what happens today, but its still not a great solution.

like image 697
chotchki Avatar asked Jan 30 '12 15:01

chotchki


People also ask

How do you create a table only if it does not exist in Oracle?

You can do it using PL/SQL Block. Check for table name in user_tables and if table does not exists then create it using Dynamic Query.

What is a temporary table in Oracle?

A TEMPORARY table is visible only within the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non- TEMPORARY table of the same name.

How do you create a temp table?

To create a temporary table, you must have the CREATE TEMPORARY TABLES privilege. After a session has created a temporary table, the server performs no further privilege checks on the table. The creating session can perform any operation on the table, such as DROP TABLE , INSERT , UPDATE , or SELECT .

Does CREATE TABLE need commit in Oracle?

CREATE TABLE and DROP TABLE statements do not commit a transaction if the TEMPORARY keyword is used. (This does not apply to other operations on temporary tables such as ALTER TABLE and CREATE INDEX , which do cause a commit.)

Is it possible to check if a table exists in Oracle?

No! It wasn't working on Oracle. Normally, it doesn’t make a lot of sense to check whether a table exists or not because objects shouldn’t be created at runtime and the application should know what objects were created at install time.

What is a a table in Oracle?

A table is an object of the database. It is used to store entity-related information like employees, departments, etc. In an Oracle database, the CREATE TABLE statement is used to create a table. The primary key is used to identify each record uniquely. This is a guide to Create Table in Oracle.

What is organization clause in CreateTable in Oracle Database?

Create table in Oracle Database has an organization clause. This defines how it physically stores rows in the table. The options for this are: By default, tables are heap-organized.

What happens when you create a table in Oracle without overflow?

When you create an index-organized table, Oracle Database evaluates the maximum size of each column to estimate the largest possible row. If an overflow segment is needed but you have not specified OVERFLOW, then the database raises an error and does not execute the CREATE TABLE statement.


3 Answers

You can create a 100% ephemeral table that is usable for the duration of a session (typically shorter than the duration than the database run time) called a TEMPORARY table. The entire purpose of a table in memory is to make it faster for reading from. You will have to re-populate the table for each session as the table will be forgotten (both structure and data) once the session completes.

like image 69
Hasteur Avatar answered Nov 15 '22 07:11

Hasteur


No exactly, no. Oracle has the concept of a "global temporary table". With a global temporary table, you create the table once, as with any other table. The table definition will persist permanently, as with any other table.

The contents of the table, however, will will not be permanent. Depending on how you define it, the contents will persist for either the life of the session (on commit perserve rows) or the life of the transaction (on commit delete rows).

See the documentation for all the details: http://docs.oracle.com/cd/E11882_01/server.112/e25494/tables003.htm#ADMIN11633

Hope that helps.

like image 43
Mark J. Bobak Avatar answered Nov 15 '22 05:11

Mark J. Bobak


You can use Oracle's trigger mechanism to invoke a stored procedure when the database starts up or shuts down.

That way you could have the startup trigger create the table, and the shutdown trigger drop it.

You'd probably also want the startup trigger to handle cases where the table exists and truncate it just in case the server stopped suddenly and the shutdown trigger wasn't called.

Oracle trigger documentation

like image 36
Gary Avatar answered Nov 15 '22 06:11

Gary