在一次建表时,使用了错误的用户,把表建在了gpadmin用户下,虽然GreenPlum数据库在对表操作的时候主要用的是SCHEMA而不是用户,但是涉及到权限的问题,还是绝对修改表的OWNER。在ORACLE数据库中,是不支持直接修改表的OWNER的,但是GreenPlum数据库是支持的。GreenPlum数据库中SCHEMA和OWNER的关系,通过下面的信息即可很直观的看出。
3 |
Schema | Name | Type | Owner | Storage |
4 |
--------+---------------+-------+---------+--------- |
5 |
public | med_ord_pgm_d | table | gpadmin | heap |
6 |
public | ord_pay | table | gpadmin | heap |
7 |
public | t_ctas | table | dbdream | heap |
GreenPlum数据库的SCHEMA和用户和ORACLE的概念完全不同,从上面的信息可以看出,一个SCHEMA可以包含多个用户的对象,对表操作时,使用的是SCHEMA.TABLENAME而不是OWNER.TABLENAME,请看如下演示。
02 |
You are now connected to database "dbdream" as user "dbdream". |
03 |
dbdream=> select * from dbdream.t_ctas; |
04 |
ERROR: schema "dbdream" does not exist |
05 |
LINE 1: select * from dbdream.t_ctas; |
07 |
dbdream=> select * from public.t_ctas; |
在同意SCHEMA里面,不同的用户不能创建相同名字的对象。
2 |
You are now connected to database "dbdream" as user "dbdream". |
3 |
dbdream=> create table med_ord_pgm_d(id int); |
4 |
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table. |
5 |
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. |
6 |
ERROR: relation "med_ord_pgm_d" already exists |
而不同的用户直接访问对方表需要有对方的授权才可以,当然超级用户除外。
1 |
dbdream=> select * from public.med_ord_pgm_d; |
2 |
ERROR: permission denied for relation med_ord_pgm_d |
要更改表的OWNER,可以通过alter table命令直接修改。
06 |
Schema | Name | Type | Owner | Storage |
07 |
--------+---------------+-------+---------+--------- |
08 |
public | med_ord_pgm_d | table | gpadmin | heap |
09 |
public | ord_pay | table | dbdream | heap |
10 |
public | t_ctas | table | dbdream | heap |
GreenPlum数据库在插入空值方面,字符类型的字段可以直接用‘’代替,这和ORACLE数据库一样。
03 |
Column | Type | Modifiers |
04 |
--------+-----------------------+----------- |
05 |
name | character varying(10) | |
07 |
Distributed by: (name) |
10 |
dbdream=> insert into t_ctas values ('',1); |
但是对数字和日期类型来说,这种方式就不行了,’’在GreenPlum数据库中表示的是字符类型。
1 |
dbdream=> insert into t_ctas values('a',''); |
2 |
ERROR: invalid input syntax for integer: "" |
对于数字或日期类型的数据,如果要插入控制,要使用null明确指定。
1 |
dbdream=> insert into t_ctas values('a',null); |
下面顺便介绍下GreenPlum数据库常用的数据类型,常用的整型数字类型有int、bigint、smallint,常用的小数类型有numeric和decimal,常用的自增类型有serial和bigserial。下面创建一张数字类型的表。
01 |
dbdream=> create table t_int(autoinc serial,small smallint,standard int,big bigint,acc numeric) Distributed by(autoinc); |
02 |
NOTICE: CREATE TABLE will create implicit sequence "t_int_autoinc_seq" for serial column "t_int.autoinc" |
06 |
Column | Type | Modifiers |
07 |
----------+----------+--------------------------------------------------------- |
08 |
autoinc | integer | not null default nextval('t_int_autoinc_seq'::regclass) |
13 |
Distributed by: (autoinc) |
smallint是短整型数子类型,支持范围是负32K(32768)到正32K-1(32767)。
int是标准的整型,支持范围是正负2G(-2147483648到2147483647)。
bigint是长整型数字类型,支持正负9E(-9223372036854775808到9223372036854775807)。
numeric是小数类型,没有限制。
serial是自增长数字类型,支持范围是1到2G(最大数值2147483647)。
bigserial是长类型自增长数字类型,最大支持9E长度(最大数值9223372036854775807)。
再看看字符类型,字符类型和其他数据库基本一样,也是分定长和变长类型,下面创建一张包含这些类型的表。
01 |
dbdream=> create table t_char(c char(10),v varchar(10),t text) DISTRIBUTED BY(c); |
05 |
Column | Type | Modifiers |
06 |
--------+-----------------------+----------- |
08 |
v | character varying(10) | |
char类型是定长字符类型,最大支持10485760字节,不足以空格补齐。
varchar类型是变长字符类型,最大支持10485760字节。
text字符类型,没有长度限制。
在看下时间类型的字段,主要能用到的时间类型包括date、time、timestamp类型,下面创建一张时间类型字段的表。
01 |
dbdream=> create table t_time(d date,t time,ts timestamp) DISTRIBUTED BY(d); |
05 |
Column | Type | Modifiers |
06 |
--------+-----------------------------+----------- |
08 |
t | time without time zone | |
09 |
ts | timestamp without time zone | |
下面插入一条数据,看看数据存储格式。
1 |
dbdream=> insert into t_time values(current_date,current_time,current_timestamp); |
3 |
dbdream=> select * from t_time; |
5 |
------------+-----------------+---------------------------- |
6 |
2016-01-29 | 17:29:49.197889 | 2016-01-29 17:29:49.197889 |
data类型存的仅是日期类型的数据(年月日),这和ORACLE数据库的date类型不一样。
time类型存放的是一天以内的时间(时分秒),精确到毫秒。
timestamp类型存放的是日期和时间(data+time)类型的数据,精确到毫秒。
本文固定链接: http://www.dbdream.com.cn/2016/01/29/greenplum%e6%95%b0%e6%8d%ae%e5%ba%93%e4%bf%ae%e6%94%b9%e8%a1%a8%e7%9a%84owner%e3%80%81%e6%8f%92%e5%85%a5%e7%a9%ba%e5%80%bc%e6%95%b0%e6%8d%ae%e5%8f%8a%e5%b8%b8%e7%94%a8%e6%95%b0%e6%8d%ae%e7%b1%bb/ | 信春哥,系统稳,闭眼上线不回滚!