Create a global temporary table
Use the following commands to create and identify global temporary tables:
To create a global temporary table:
create global temp table <table name> as select * from <table name>
The preceding query only copies schema of table and did not include the data. For inserting data,
you need to run the insert
command.
Example:
SYSTEM.ADMIN(ADMIN)=> select * from t1;
N
---
5
(1 row)SYSTEM.ADMIN(ADMIN)=> create global temp table GT2 as select * from t1;
INSERT 0 0
SYSTEM.ADMIN(ADMIN)=> select * from gt2;
N
---
(0 rows)SYSTEM.ADMIN(ADMIN)=> insert into gt2 select * from t1;
INSERT 0 1
SYSTEM.ADMIN(ADMIN)=> select * from gt2;
N
---
5
(1 row)
To identify a global temporary table in the database, use \d, and verify the
value for
Type
:- If
GLOBAL
is specified, then a global temporary table is defined. - If
LOCAL
is specified, then a local temporary table is defined. - If
TEMP
is specified, then a local temporary table is defined.
GTT.ADMIN(ADMIN)=> \d
List of relations
Schema | Name | Type | Owner
-------+------+------------------+-------
ADMIN | GT1 | GLOBAL TEMP TABLE| ADMIN
(1 row)
Restrictions:
- Similarly to local temporary tables, ADD and DROP of columns are not allowed on global temporary tables.
- User can't load data in a global temporary table using nzload.
- Global Temporary Tables (GTT) cannot be altered once created.
- GTT with constraints can not be restored and may result
nzrestore
failure with following error:Operation not allowed on a Global Temp Table Template.