Greenplum表的管理实践-1
本文章主要介绍和实践如何创建、修改、删除表,包括临时表的管理,同时针对表的约束,包括非空,唯一,主外键,默认等,另外还会简单进行数据的插入和修改,删除的实验操作。
文章目录
首先需要申明的是Greenplum数据库的表与任何一种关系型数据库中的表类似,不过其表中的行被分布在系统中的不同Segment上。 当用户创建一个表时,用户会指定该表的分布策略。
Greenplum的表分布策略,存储特殊属性,已经大表的分区,我会在表实践-2上进行总结整理。
Greenplum创建的表的策略或者注意事项:
- 该表的列以及它们的数据类型。参见选择列的数据类型。
- 任何用于限制列或者表中能包含的数据的表或者列约束。参见设置表和列约束。
- 表的分布策略,这决定了Greenplum数据库如何在Segment之间划分数据。参见选择表分布策略。
- 表存储在磁盘上的方式。参见选择表存储模型。
- 大型表的表分区策略。参见创建和管理数据库。
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

最低0.47元/天 解锁文章
1897

被折叠的 条评论
为什么被折叠?



