top of page

Craft, activity and play ideas

Public·31 members
Hermann Romanov
Hermann Romanov

Oracle Sql Create Temporary Table As Select


Applications often use some form of temporary data store for processes that are to complicated to complete in a single pass. Often, these temporary stores are defined as database tables or PL/SQL tables. From Oracle 8i onward, the maintenance and management of temporary tables can be delegated to the server by using Global Temporary Tables.




oracle sql create temporary table as select



The data in a global temporary table is private, such that data inserted by a session can only be accessed by that session. The session-specific rows in a global temporary table can be preserved for the whole session, or just for the current transaction.


Although the data in a GTT is written to the temporary tablespace, the associated undo is still written to the normal undo tablespace, which is itself protected by redo, so using a GTT does not reduce undo and the redo associated with protecting the undo tablespace.


If you've read the previous section, you will already know the relationship between global temporary tables and redo. The data in a GTT is written to the temporary tablespace, which is not directly protected by redo, so using a GTT improves performance by reducing redo generation. Unfortunately, prior to Oracle 12c, all undo associated with DML against a GTT is written to the normal undo tablespace, which is itself protected by redo. As a result, using a GTT reduces the amount of redo generation, but does not eliminate it. Another why of describing this is, using a GTT removes direct redo generation, but not indirect redo generation cause by undo.


A new variation of temporary tables has been introduced in Oracle 18c. A private temporary table is a memory-based temporary table that is dropped at the end of the session or transaction depending on the setup. You can read more about them here.


You can use the TEMPORARY keyword when creating a table. 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. (The existing table is hidden until the temporary table is dropped.)


InnoDB does not support compressed temporary tables. When innodb_strict_mode is enabled (the default), CREATE TEMPORARY TABLE returns an error if ROW_FORMAT=COMPRESSED or KEY_BLOCK_SIZE is specified. If innodb_strict_mode is disabled, warnings are issued and the temporary table is created using a non-compressed row format. The innodb_file_per-table option does not affect the creation of InnoDB temporary tables.


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.


One implication of this behavior is that a session can manipulate its temporary tables even if the current user has no privilege to create them. Suppose that the current user does not have the CREATE TEMPORARY TABLES privilege but is able to execute a definer-context stored procedure that executes with the privileges of a user who does have CREATE TEMPORARY TABLES and that creates a temporary table. While the procedure executes, the session uses the privileges of the defining user. After the procedure returns, the effective privileges revert to those of the current user, which can still see the temporary table and perform any operation on it.


You cannot use CREATE TEMPORARY TABLE ... LIKE to create an empty table based on the definition of a table that resides in the mysql tablespace, InnoDB system tablespace (innodb_system), or a general tablespace. The tablespace definition for such a table includes a TABLESPACE attribute that defines the tablespace where the table resides, and the aforementioned tablespaces do not support temporary tables. To create a temporary table based on the definition of such a table, use this syntax instead:


Support for TABLESPACE = innodb_file_per_table and TABLESPACE = innodb_temporary clauses with CREATE TEMPORARY TABLE is deprecated as of MySQL 8.0.13; expect it be removed in a future version of MySQL.


Oracle SQL query rewriting with temporary tablesOracle Database Tips by Donald BurlesonEnd users often don't know how difficult a SQL statement might be to code, and a great example is a simple query 'show me all stores with above average sales?. This would involve only two tables (a STORE and a SALES table), but it's a simple question with a complex answer. To answer the question "Show me all stores with above average sales?", we must know: - How many stores are there? - What are the total sales for each store?


  • (Note: You may find a faster execution plan by using Global Temporary tables, depending on your release of Oracle):WITHsum_sales AS ( select /*+ materialize */ sum(quantity) all_sales from sales ),number_stores AS ( select /*+ materialize */ count(*) nbr_stores from store ),sales_by_store AS ( select /*+ materialize */ store_name, sum(quantity) store_sales from store natural join sales group by store_name)SELECT store_name, store_sales, all_sales / nbr_stores avg_salesFROM sum_sales, number_stores, sales_by_storewhere store_sales > (all_sales / nbr_stores); For more details, see my notes:Oracle SQL-99 WITH clause

  • Oracle SQL materialize Hint

If you like Oracle tuning, you may enjoy the new book "Oracle Tuning: The Definitive Reference", over 900 pages of BC's favorite tuning tips & scripts. You can buy it direct from the publisher for 30%-off and get instant access to the code depot of Oracle tuning scripts.


Viewed 10K+ times! This question is You Asked Hi,Please, help me to understand - whats happenging?:I've GTT with ON COMMIT PRESERVE ROWSand after inserting values with procedure:"insert into gtt ... select ...;commit;"next sql"select * from gtt" returns nothing!!!but, without commit there are rowsSQL for creating table:CREATE GLOBAL TEMPORARY TABLE GTT( K010 NUMBER(9, 0) , K020 NUMBER(10, 0) , CLI_BIZN NUMBER(1, 0) NOT NULL , CLI_SEGM VARCHAR2(3 BYTE) NOT NULL ) ON COMMIT PRESERVE ROWS; and Connor said...Sorry I can't reproduce (I'm on 12.1.0.2)SQL> CREATE GLOBAL TEMPORARY TABLE GTT 2 ( 3 K010 NUMBER(9, 0) 4 , K020 NUMBER(10, 0) 5 , CLI_BIZN NUMBER(1, 0) NOT NULL 6 , CLI_SEGM VARCHAR2(3 BYTE) NOT NULL 7 ) 8 ON COMMIT PRESERVE ROWS;Table created.SQL>SQL> insert into gtt 2 select 1,2,3,4 from dual;1 row created.SQL>SQL> select * from gtt; K010 K020 CLI_BIZN CLI---------- ---------- ---------- --- 1 2 3 41 row selected.SQL>SQL> commit;Commit complete.SQL>SQL> select * from gtt; K010 K020 CLI_BIZN CLI---------- ---------- ---------- --- 1 2 3 41 row selected.Can you send us an end-to-end test case to use ?Also, put a trace on the session to make sure nothing 'hidden' is going on (triggers etc), ie,exec dbms_monitor.session_trace_enableinsert into gtt...select * from gtt...exec dbms_monitor.session_trace_disableand have a look in the trace file, or send it to us: asktom_us@oracle.com Rating (3 ratings)Is this answer out of date? If it is, please let us know via a Comment Comments Comment Problem solvedA reader, January 12, 2018 - 3:00 pm UTC


The table name can be specified as db_name.tbl_name to create the table in a specific database. This works regardless of whether there is a default database, assuming that the database exists. If you use quoted identifiers, quote the database and table names separately. For example, write `mydb`.`mytbl`, not `mydb.mytbl`.


In the created table, a PRIMARY KEY is placed first, followed by all UNIQUE indexes, and then the nonunique indexes. This helps the MySQL optimizer to prioritize which index to use and also more quickly to detect duplicated UNIQUE keys.


Setting NDB_TABLE options. The table comment in a CREATE TABLE that creates an NDB table or an ALTER TABLE statement which alters one can also be used to specify one to four of the NDB_TABLE options NOLOGGING, READ_BACKUP, PARTITION_BALANCE, or FULLY_REPLICATED as a set of name-value pairs, separated by commas if need be, immediately following the string NDB_TABLE= that begins the quoted comment text. An example statement using this syntax is shown here (emphasized text):


When creating MyISAM tables, you can use the DATA DIRECTORY='directory' clause, the INDEX DIRECTORY='directory' clause, or both. They specify where to put a MyISAM table's data file and index file, respectively. Unlike InnoDB tables, MySQL does not create subdirectories that correspond to the database name when creating a MyISAM table with a DATA DIRECTORY or INDEX DIRECTORY option. Files are created in the directory that is specified.


If a MyISAM table is created with no DATA DIRECTORY option, the .MYD file is created in the database directory. By default, if MyISAM finds an existing .MYD file in this case, it overwrites it. The same applies to .MYI files for tables created with no INDEX DIRECTORY option. To suppress this behavior, start the server with the --keep_files_on_create option, in which case MyISAM does not overwrite existing files and returns an error instead.


If a MyISAM table is created with a DATA DIRECTORY or INDEX DIRECTORY option and an existing .MYD or .MYI file is found, MyISAM always returns an error, and does not overwrite a file in the specified directory.


As of MySQL 8.0.16, a table inherits the default schema encryption if an ENCRYPTION clause is not specified. If the table_encryption_privilege_check variable is enabled, the TABLE_ENCRYPTION_ADMIN privilege is required to create a table with an ENCRYPTION clause setting that differs from the default schema encryption. When creating a table in a general tablespace, table and tablespace encryption must match.


About

Welcome to the group! You can connect with other members, ge...

Members

  • Melissa Willis
  • Catherine Barrett
    Catherine Barrett
  • Adam Broost
    Adam Broost
  • Karakurt Monreal
    Karakurt Monreal
  • stas malets
    stas malets
bottom of page