【 PostgreSQL】工作中常用语句干货

接触gp数据库近一年的时间,语法上和其他数据库还是有些许不同,工作中常用的操作语句分享给大家!

-- 建表语句

create table ods.ods_b_bill_m (

acct_month text,

user_id text,

city_code text

)

WITH (

appendonly=true,

orientation=column,--列存 行存为 row

compresstype=zlib,--压缩格式 --QUICKLZ 

COMPRESSLEVEL=5, --压缩等级 0--9 --1 压缩低查询快

OIDS=FALSE

)

DISTRIBUTED BY (user_id) --分布键

PARTITION BY LIST("acct_month") --分区键

(

PARTITION p_201801 VALUES ('201801'),

PARTITION p_201802 VALUES ('201802'),

default partition other --容错没有分区键在此,不推荐使用 

);

-- 修改分布键

alter table tab01 set distributed by(name);

-- 增加分区

alter table ods.ods_b_bill_m partition p_201803 values('201803')

WITH (appendonly=true,orientation=column,compresstype=zlib,COMPRESSLEVEL=5);

-- 删除分区

alter table ods.ods_b_bill_m drop partition p_201803 ;

-- 修改表名

ALTER TABLE ods.ods_b_bill_m RENAME TO ods_cb_bill_m ; 

-- 增加字段 

alter table ods.ods_b_bill_m add gprs_flow text default null ;

-- 删除字段

alter table ods.ods_b_bill_m drop column gprs_flow ;

-- 修改字段名称 

alter table ods.ods_b_bill_m rename column "账期" to acct_month;

-- 更改字段类型 

alter table ods.ods_b_bill_m alter column  user_age type character  varying ;

--强制类型转换 转换成数值类型需要强转 

alter table ods.ods_b_bill_m alter column  user_age type numeric  using user_age::numeric ;

-- 创建序列

CREATE SEQUENCE rptinfo_id_seq START 100;

-- 授权

alter schema rpt owner to user01;    

grant select on table ods.ods_b_bill_m to user01; --将表查询权限给用户

grant all on schema dw to user01;

猜你喜欢

转载自blog.csdn.net/sinat_35630008/article/details/81175097