GreenPlum数据库修改表的OWNER、插入空值数据及常用数据类型

在一次建表时,使用了错误的用户,把表建在了gpadmin用户下,虽然GreenPlum数据库在对表操作的时候主要用的是SCHEMA而不是用户,但是涉及到权限的问题,还是绝对修改表的OWNER。在ORACLE数据库中,是不支持直接修改表的OWNER的,但是GreenPlum数据库是支持的。GreenPlum数据库中SCHEMA和OWNER的关系,通过下面的信息即可很直观的看出。

1 dbdream=# \d
2                  List of relations
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
8  (3 rows)

GreenPlum数据库的SCHEMA和用户和ORACLE的概念完全不同,从上面的信息可以看出,一个SCHEMA可以包含多个用户的对象,对表操作时,使用的是SCHEMA.TABLENAME而不是OWNER.TABLENAME,请看如下演示。

01 dbdream=> \c
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;
06  
07 dbdream=> select * from public.t_ctas;
08  name | id
09 ------+----
10 (0 rows)

在同意SCHEMA里面,不同的用户不能创建相同名字的对象。

1 dbdream=> \c
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命令直接修改。

01 dbdream=# ALTER TABLE ORD_PAY owner to dbdream;
02 ALTER TABLE
03  
04 dbdream=# \d
05                  List of relations
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
11  (3 rows)

GreenPlum数据库在插入空值方面,字符类型的字段可以直接用‘’代替,这和ORACLE数据库一样。

01 dbdream=> \d t_ctas
02            Table "public.t_ctas"
03  Column |         Type          | Modifiers
04 --------+-----------------------+-----------
05  name   | character varying(10) |
06  id     | integer               |
07 Distributed by: (name)
08 Tablespace: "tbs1"
09  
10 dbdream=> insert into t_ctas values ('',1);
11 INSERT 0 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);
2 INSERT 0 1

下面顺便介绍下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"
03 CREATE TABLE
04 dbdream=> \d t_int
05                              Table "public.t_int"
06   Column  |   Type   |                        Modifiers                       
07 ----------+----------+---------------------------------------------------------
08  autoinc  | integer  | not null default nextval('t_int_autoinc_seq'::regclass)
09  small    | smallint |
10  standard | integer  |
11  big      | bigint   |
12  acc      | numeric  |
13 Distributed by: (autoinc)
14 Tablespace: "tbs1"

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);
02 CREATE TABLE
03 dbdream=> \d t_char
04            Table "public.t_char"
05  Column |         Type          | Modifiers
06 --------+-----------------------+-----------
07  c      | character(10)         |
08  v      | character varying(10) |
09  t      | text                  |
10 Distributed by: (c)
11 Tablespace: "tbs1"

char类型是定长字符类型,最大支持10485760字节,不足以空格补齐。

varchar类型是变长字符类型,最大支持10485760字节。

text字符类型,没有长度限制。

在看下时间类型的字段,主要能用到的时间类型包括date、time、timestamp类型,下面创建一张时间类型字段的表。

01 dbdream=> create table t_time(d date,t time,ts timestamp) DISTRIBUTED BY(d);
02 CREATE TABLE
03 dbdream=> \d t_time
04               Table "public.t_time"
05  Column |            Type             | Modifiers
06 --------+-----------------------------+-----------
07  d      | date                        |
08  t      | time without time zone      |
09  ts     | timestamp without time zone |
10 Distributed by: (d)
11 Tablespace: "tbs1"

下面插入一条数据,看看数据存储格式。

1 dbdream=> insert into t_time values(current_date,current_time,current_timestamp);
2 INSERT 0 1
3 dbdream=> select * from t_time;
4      d      |        t        |             ts            
5 ------------+-----------------+----------------------------
6  2016-01-29 | 17:29:49.197889 | 2016-01-29 17:29:49.197889
7 (1 row)

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/ | 信春哥,系统稳,闭眼上线不回滚!

该日志由 dbdream 于2016年01月29日发表在 GreenPlum 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: GreenPlum数据库修改表的OWNER、插入空值数据及常用数据类型 | 信春哥,系统稳,闭眼上线不回滚!
关键字: greenplum

猜你喜欢

转载自weitao1026.iteye.com/blog/2384900