본문 바로가기

Oracle/etc

global temporary table



create global temporary table 문
임시 테이블은 사용자가 DML(update, insert, delete) 문을 실행한 후, 트랜젝션을 종료(commit)하면 변경된 데이터들이 테이블에 저장되지 않는 테이블이다.
어떤 응용을 개발할 때 데이터를 잠시 저장하는 변수와 같은 유형이다.
임시 테이블에 데이터를 유지시키는 방법은 2가지가 있다.
on commit delete rows 트랜젝션을 발생시킨 후, commit문을 실행할 때 없어지는 방법
한 transaction 동안만 data를 저장(default임)
on commit preserve rows 트랜젝션을 종료하면(commit) 테이블 내에 데이터가 저장되었다가 세션을 종료하면 임시 테이블에 저장되었던 데이터들이 모두 없어지는 방법
한 session 동안만 data를 저장

temporary table은 특정 session 또는 transaction 동안만 data를 저장한다.

• TABLE의 definition 정보는 data dictionary에 영구적으로 저장되지만, data는 일시적으로 저장된다.• CREATE로 TABLE 생성시 SEGMENT가 할당되는 것이 아니라 첫 번째 INSERT시 할당된다.• DML 작업시 해당 data에 대해 LOCK을 걸지 않는다.• data 변경시 redo log에 기록이 남지 않는다.【형식】CREATE GLOBAL TEMPORARY TABLE table명        (column명 datatype...)        [ON COMMIT DELETE ROWS | ON COMMIT PRESERVE ROWS]; 일반적으로 생성하는 table에 GLOBAL TEMPORARY라는 키워드를 사용한다. 다음에 컬럼과 데이터타입을 지정하며 또는 subquery를 이용하여 생성할 수도 있다.【예제】SQL> connect scott/tigerSQL> CREATE GLOBAL TEMPORARY TABLE test_temp  2  (id number(3), name varchar2(10));Table created.SQL> INSERT INTO test_temp VALUES(10,'Corea');1 row created.SQL> SELECT * FROM test_temp;        ID NAME---------- ----------        10 CoreaSQL> COMMIT;`Commit complete.SQL> SELECT * FROM test_temp;no rows selectedSQL>SQL> CREATE GLOBAL TEMPORARY TABLE temp_emp  2  AS  3  SELECT * FROM emp;Table created.SQL> select * from temp_emp;no rows selectedSQL> 【예제】
on commit delete rowson commit preserve rows
SQL> create global temporary table temp_test  2  (id        number(6),  3  salary     number(8,2))  4  on commit delete rows;테이블이 생성되었습니다.SQL> insert into temp_test values(  2  11111,12345.33);1 개의 행이 만들어졌습니다.SQL> select * from temp_test;        ID     SALARY---------- ----------     11111   12345.33SQL> commit;커밋이 완료되었습니다.SQL> select * from temp_test;선택된 레코드가 없습니다.SQL> 
SQL> create global temporary table temp_test2  2  (id        number(6),  3  salary     number(8,2))  4  on commit preserve rows;테이블이 생성되었습니다.SQL> insert into temp_test2 values  2  (222222, 33333.44);1 개의 행이 만들어졌습니다.SQL> select * from temp_test2;        ID     SALARY---------- ----------    222222   33333.44SQL> connect jijoe/jijoe_passwdConnected.세션이 변경되었습니다.SQL> select * from temp_test2;선택된 레코드가 없습니다.SQL> 
참조 : http://radiocom.kunsan.ac.kr/lecture/oracle/statement_create/create_temporary_table.html