Analytics

Sunday, March 24, 2013

Temporary Tables

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. In Oracle 8i, the maintenance and management of temporary tables can be delegated to the server by using Global Temporary Tables.

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. The ON COMMIT DELETE ROWS clause indicates that the data should be deleted at the end of the transaction.

1. Create global temporary table

SQL> CREATE GLOBAL TEMPORARY TABLE supplier
  2  (  supplier_id     numeric(10)     not null,
  3     supplier_name   varchar2(50)    not null,
  4     contact_name    varchar2(50)
  5  )
  6  /

2. Create global temporary table from existing table

SQL> create global temporary table employee_tab
  2        on commit preserve rows
  3        as select * from employee;

Table created.

3. Create global temporary table with 'on commit delete rows' option

SQL> create global temporary table transaction_tab
  2        on commit delete rows
  3        as select *
  4             from employee;

Table created.

4. Temporary tables cannot be forced into logging.

SQL> ALTER TABLE temp_emp LOGGING;
ALTER TABLE temp_emp LOGGING
*
ERROR at line 1:
ORA-14451: unsupported feature with temporary table

5. Temporary tables support primary keys.

SQL> CREATE GLOBAL TEMPORARY TABLE temp_emp
  2    AS SELECT * FROM employee;

Table created.

SQL>
SQL> ALTER TABLE temp_emp
  2    ADD CONSTRAINT pk_temp_emp
  3    PRIMARY KEY (emp_no);

Table altered.

6. Temporary tables do not support foreign keys

SQL> CREATE GLOBAL TEMPORARY TABLE temp_emp
  2    AS SELECT * FROM employee;

Table created.

SQL>
SQL> ALTER TABLE temp_emp
  2    ADD CONSTRAINT fk_temp_emp_dept
  3    FOREIGN KEY (dept_no)
  4    REFERENCES department;
ALTER TABLE temp_emp
*
ERROR at line 1:
ORA-14455: attempt to create referential integrity constraint on temporary table

7. You cannot alter a temporary table to change its data duration.
(drop and recreate)

SQL> CREATE GLOBAL TEMPORARY TABLE temp_emp
  2    AS SELECT * FROM employee;

Table created.

SQL>
SQL> ALTER TABLE temp_emp ON COMMIT PRESERVE ROWS;
ALTER TABLE temp_emp ON COMMIT PRESERVE ROWS
                     *
ERROR at line 1:
ORA-01735: invalid ALTER TABLE option

8. Update a TEMPORARY TABLE and check the table it based on

SQL> UPDATE temp_emp SET salary = 99
  2  WHERE title = 'Designer';

3 rows updated.

SQL>
SQL> select * from employee where title = 'Designer'
  2
SQL> SELECT salary FROM temp_emp
  2  WHERE title = 'Designer';
    SALARY
----------
        99
        99
        99

3 rows selected.

9. Truncate and Drop global temporary table

SQL> create global temporary table session_tab
  2  on commit preserve rows
  3  as select * from employees;

SQL> truncate table session_tab;

Table truncated.

SQL> drop table session_tab;

Table dropped.

1 comment: