GP数据库 常用SQL语句

GP数据库 常用SQL语句

--1,查看列名以及类型
select upper(column_name) ,data_type from information_schema.columns 
where table_schema='ods_hs08' and lower(table_name)=lower('T_ods_hs08_secumreal');

--2,查看表名剔除分区表名
select a.tablename from pg_tables a where a.schemaname='ods_hs08'
and a.tablename not in (select b.partitiontablename from pg_partitions b where b.schemaname='ods_hs08' ) ;
--3,查分区表名
select parentpartitiontablename,partitiontablename,* from pg_partitions ;
--4,查看gpload错误记录表
select * from gp_read_error_log('ext_gpload_reusable_2117d97e_5cf0_11e9_b169_2880239d54e8') 
where cmdtime > to_timestamp('1554971595.06');

select gp_read_error_log('ext_gpload_4a33e352_5f20_11e9_8223_2880239d54e8 ');
--5,修改主键
alter table ods_htgs.ttrd_acc_balance_cash
  add constraint ttrd_acc_balance_cash_pkey primary key (cash_ext_accid, accid, currency);
  --或者在建表语句列后面加
CONSTRAINT ttrd_acc_balance_cash_pkey PRIMARY KEY (cash_ext_accid, accid, currency)

--6,外键约束
alter table ods_htgs.ttrd_acc_secu
  add constraint fk_ttrd_acc_secu foreign key (cash_accid)
  references ods_htgs.ttrd_acc_cash (accid);
  --或者在建表语句列后面加
  CONSTRAINT fk_ttrd_acc_secu FOREIGN KEY (cash_accid)
      REFERENCES ods_htgs.ttrd_acc_cash (accid) MATCH SIMPLE
      ON UPDATE NO ACTION
      ON DELETE NO ACTION
     
--7,字符转日期转数字
select * from  ods_hs08.t_ods_hs08_his_entrust
where to_number(to_char(to_date(init_date || '','yyyyMMdd'),'yyyyMMdd'),'99999999') between 20010101 and 20191201

--8,table_constraint语法:
table_constraint is:

[ CONSTRAINT constraint_name ]
{ UNIQUE ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |
  PRIMARY KEY ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |
  CHECK ( expression ) |
  FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
    [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]

猜你喜欢

转载自www.cnblogs.com/lizm166/p/10736979.html