KingbaseES 临时表

临时表在数据库管理和数据处理中有着广泛的应用,主要用于存储临时数据或进行中间计算。临时表中的数据对会话是私有的,每个会话只能看到和修改自己会话的数据。
KingbaseES支持本地临时表和全局临时表。创建临时表时若不指定GLOBAL或LOCAL,则默认值指定为LOCAL。

本地临时表和全局临时表有如下四点区别:
1.本地临时表在临时模式下,用户不可以指定;但是全局临时表创建在用户指定模式下;
2.本地临时表对象本身并不是一直存在,在会话退出后,本地临时表会被删除;全局临时表创建之后,一直存在,除非显示去删除它。
3.删除本地临时表,不受其他会话影响;但是删除全局临时表时,所有会话都不能持有全部临时表的数据。
4.全局临时表不支持外键约束也不支持其他表引用全局临时表作为外键约束,而本地临时表没有这个要求。

会话1 创建本地临时表

test=# create temp table temp1(id integer);
CREATE TABLE
test=# insert into temp1 values(1);
INSERT 0 1
test=# select * from temp1;
 id
----
  1
(1 行记录)

test=# \d temp1
         数据表 "pg_temp_5.temp1"
 栏位 |  类型   | 校对规则 | 可空的 | 预设
------+---------+----------+--------+------
 id   | integer |          |        |

test=# \q
[kbc7@singlekbdb ~]$  ksql -Usystem -d test -p 7788
ksql (V8.0)
输入 "help" 来获取帮助信息.

test=# \d temp1
没有找到任何名称为 "temp1" 的关联.

可以看到本地临时表是创建在临时的用户模式下(pg_temp_5),会话退出后本地临时表被删除了。

会话1 创建全局临时表

test=# create global temp table temp_g(id integer) on commit preserve rows;
CREATE TABLE
test=# insert into temp_g values(1);
INSERT 0 1
test=# select * from temp_g;
 id
----
  1
(1 行记录)

test=# \d temp_g
          数据表 "public.temp_g"
 栏位 |  类型   | 校对规则 | 可空的 | 预设
------+---------+----------+--------+------
 id   | integer |          |        |

test=# \q
[kbc7@singlekbdb ~]$  ksql -Usystem -d test -p 7788
ksql (V8.0)
输入 "help" 来获取帮助信息.

test=# select * from temp_g;
 id
----
(0 行记录)

可以看到全局临时表是创建在默认的public模式下,会话退出后全局临时表依然存在。有public用户权限的用户都可以操作这张临时表

posted @ 2024-03-28 15:33  KINGBASE研究院  阅读(55)  评论(0编辑  收藏  举报