Greenplum表的管理实践-1

Greenplum表的管理实践-1

本文章主要介绍和实践如何创建、修改、删除表,包括临时表的管理,同时针对表的约束,包括非空,唯一,主外键,默认等,另外还会简单进行数据的插入和修改,删除的实验操作。


首先需要申明的是Greenplum数据库的表与任何一种关系型数据库中的表类似,不过其表中的行被分布在系统中的不同Segment上。 当用户创建一个表时,用户会指定该表的分布策略。

Greenplum的表分布策略,存储特殊属性,已经大表的分区,我会在表实践-2上进行总结整理。

Greenplum创建的表的策略或者注意事项:

1 创建表

create table department(deptid int not null,
                       deptname varchar(20),
                       createtime timestamp)
                       DISTRIBUTED BY(deptid);
                       

archdata=# create table department(deptid int not null,
archdata(#                        deptname varchar(20),
archdata(#                        createtime timestamp)
archdata-#                        DISTRIBUTED BY(deptid);
CREATE TABLE
archdata=# 
archdata=# 
archdata=# \dt department
                List of relations
 Schema |    Name    | Type  |  Owner  | Storage 
--------+------------+-------+---------+---------
 public | department | table | gpadmin | heap
(1 row)

archdata=# \dt+ department
                       List of relations
 Schema |    Name    | Type  |  Owner  | Storage | Description 
--------+------------+-------+---------+---------+-------------
 public | department | table | gpadmin | heap    | 
(1 row)  

 查看表大小
         
 select pg_size_pretty(pg_relation_size('department'));
         
archdata=#  select pg_size_pretty(pg_relation_size('department'));
 pg_size_pretty 
----------------
 0 bytes
(1 row)

archdata=# 

2 创建临时表

PostgreSQL支持两类临时表,会话级和事务级临时表。在会话级别的临时表中,在整个会话的生命周期中,数据一直保存。事务级临时表,数据只存在于这个事务的生命周期中。不指定临时表的属性,

PostgreSQL中,不管是事务级还是会话级临时表,当会话结束时,临时表就会消失。这与oracle数据库不同,在oracle数据库中,只是临时表中的数据消失,而临时表还存在。

PostgreSQL临时表是schema下所生成的一个特殊的表,这个schema的名称为“pg_temp_n”,其中n代表数字,不同的session数字不同。

一个会话创建的临时表不能被其他会话访问。

默认情况下,创建的临时表是会话级的,如果需要创建事务。需要添加“on commit delete rows”子句。(注:“on commit”子句形式有三种:“on commit preserve rows”,默认值,会话级;“on commit delete rows”,事务级,事务结束,删除数据;“on commit drop”,事务级,事务结束,删除临时表)

创建临时表的关键字“temporary”可以缩写为“temp”。

PostgreSQL为了与其他数据库创建临时表的语句保持兼容,还没有“GLOBAL”和“LOCAL”关键字,但两个关键字没有用处。

2.1 会话级临时表

创建临时表

create temporary table temp_t as select * from pg_class;

archdata=# create temporary table temp_t as select * from pg_class;
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'relname' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
SELECT 411

archdata=# \dt pg_class;
                 List of relations
   Schema   |   Name   | Type  |  Owner  | Storage 
------------+----------+-------+---------+---------
 pg_catalog | pg_class | table | gpadmin | heap
(1 row)

archdata=# select count(*) from pg_class;
 count 
-------
   411
(1 row)

archdata=# 

在本session中是可以看到表的
archdata-# \dt temp_t
                 List of relations
    Schema    |  Name  | Type  |  Owner  | Storage 
--------------+--------+-------+---------+---------
 pg_temp_1428 | temp_t | table | gpadmin | heap
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值