当前位置: 首页 > GreenPlum > 正文

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

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

dbdream=# \d
                 List of relations
 Schema |     Name      | Type  |  Owner  | Storage 
--------+---------------+-------+---------+---------
 public | med_ord_pgm_d | table | gpadmin | heap
 public | ord_pay       | table | gpadmin | heap
 public | t_ctas        | table | dbdream | heap
 (3 rows)

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

dbdream=> \c
You are now connected to database "dbdream" as user "dbdream".
dbdream=> select * from dbdream.t_ctas;
ERROR:  schema "dbdream" does not exist
LINE 1: select * from dbdream.t_ctas;

dbdream=> select * from public.t_ctas;
 name | id 
------+----
(0 rows)

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

dbdream=> \c
You are now connected to database "dbdream" as user "dbdream".
dbdream=> create table med_ord_pgm_d(id int);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' 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.
ERROR:  relation "med_ord_pgm_d" already exists

而不同的用户直接访问对方表需要有对方的授权才可以,当然超级用户除外。

dbdream=> select * from public.med_ord_pgm_d;
ERROR:  permission denied for relation med_ord_pgm_d

要更改表的OWNER,可以通过alter table命令直接修改。

dbdream=# ALTER TABLE ORD_PAY owner to dbdream;
ALTER TABLE

dbdream=# \d
                 List of relations
 Schema |     Name      | Type  |  Owner  | Storage 
--------+---------------+-------+---------+---------
 public | med_ord_pgm_d | table | gpadmin | heap
 public | ord_pay       | table | dbdream | heap
 public | t_ctas        | table | dbdream | heap
 (3 rows)

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

dbdream=> \d t_ctas
           Table "public.t_ctas"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 name   | character varying(10) | 
 id     | integer               | 
Distributed by: (name)
Tablespace: "tbs1"

dbdream=> insert into t_ctas values ('',1);
INSERT 0 1

但是对数字和日期类型来说,这种方式就不行了,’’在GreenPlum数据库中表示的是字符类型。

dbdream=> insert into t_ctas values('a','');
ERROR:  invalid input syntax for integer: ""

对于数字或日期类型的数据,如果要插入控制,要使用null明确指定。

dbdream=> insert into t_ctas values('a',null);
INSERT 0 1

下面顺便介绍下GreenPlum数据库常用的数据类型,常用的整型数字类型有int、bigint、smallint,常用的小数类型有numeric和decimal,常用的自增类型有serial和bigserial。下面创建一张数字类型的表。

dbdream=> create table t_int(autoinc serial,small smallint,standard int,big bigint,acc numeric) Distributed by(autoinc);
NOTICE:  CREATE TABLE will create implicit sequence "t_int_autoinc_seq" for serial column "t_int.autoinc"
CREATE TABLE
dbdream=> \d t_int
                             Table "public.t_int"
  Column  |   Type   |                        Modifiers                        
----------+----------+---------------------------------------------------------
 autoinc  | integer  | not null default nextval('t_int_autoinc_seq'::regclass)
 small    | smallint | 
 standard | integer  | 
 big      | bigint   | 
 acc      | numeric  | 
Distributed by: (autoinc)
Tablespace: "tbs1"

smallint是短整型数子类型,支持范围是负32K(32768)到正32K-1(32767)。

int是标准的整型,支持范围是正负2G(-2147483648到2147483647)。

bigint是长整型数字类型,支持正负9E(-9223372036854775808到9223372036854775807)。

numeric是小数类型,没有限制。

serial是自增长数字类型,支持范围是1到2G(最大数值2147483647)。

bigserial是长类型自增长数字类型,最大支持9E长度(最大数值9223372036854775807)。

再看看字符类型,字符类型和其他数据库基本一样,也是分定长和变长类型,下面创建一张包含这些类型的表。

dbdream=> create table t_char(c char(10),v varchar(10),t text) DISTRIBUTED BY(c);
CREATE TABLE
dbdream=> \d t_char
           Table "public.t_char"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 c      | character(10)         | 
 v      | character varying(10) | 
 t      | text                  | 
Distributed by: (c)
Tablespace: "tbs1"

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

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

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

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

dbdream=> create table t_time(d date,t time,ts timestamp) DISTRIBUTED BY(d);
CREATE TABLE
dbdream=> \d t_time
              Table "public.t_time"
 Column |            Type             | Modifiers 
--------+-----------------------------+-----------
 d      | date                        | 
 t      | time without time zone      | 
 ts     | timestamp without time zone | 
Distributed by: (d)
Tablespace: "tbs1"

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

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

data类型存的仅是日期类型的数据(年月日),这和ORACLE数据库的date类型不一样。

time类型存放的是一天以内的时间(时分秒),精确到毫秒。

timestamp类型存放的是日期和时间(data+time)类型的数据,精确到毫秒。

本文固定链接: http://www.dbdream.com.cn/2016/01/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数据库修改表的OWNER、插入空值数据及常用数据类型:等您坐沙发呢!

发表评论

快捷键:Ctrl+Enter