GreenPlum删除主键约束

最近使用GreenPlum(简称GP)时遇到了一个小问题,删除主键删不掉,一直报主键不存在,例子如下:

zxy=> \d tmp_cst_pcp_avy_sttn_hw
     Table "a0.tmp_cst_pcp_avy_sttn_hw"
        Column         |         Type          | Modifiers 
-----------------------+-----------------------+-----------
 dccp_avy_id           | character varying(40) | not null
 avy_aw_cnd_id         | character varying(20) | not null
 cst_id                | character varying(18) | not null
 multi_tenancy_id      | character varying(5)  | not null
 stat_dt               | date                  | not null
 cstpcpavyfit_cnd_dnum | numeric(5,0)          | 
 cstpcpavyfitcnd_acamt | numeric(15,2)         | 
 avy_aw_rslt_stcd      | character varying(2)  | 
 ccycd                 | character varying(3)  | 
 cardno                | character varying(19) | 
 txn_dt                | date                  | 
 prz_tpcd              | character varying(2)  | 
 aspd_id               | character varying(8)  | 
Indexes:
    "tmp_cst_pcp_avy_sttn_hw_pkey" PRIMARY KEY, btree (dccp_avy_id, avy_aw_cnd_id, cst_id, stat_dt, multi_tenancy_id)
Distributed by: (dccp_avy_id, avy_aw_cnd_id, cst_id, stat_dt, multi_tenancy_id)
zxy=> alter table tmp_cst_pcp_avy_sttn_hw drop constraint tmp_cst_pcp_avy_sttn_hw_pkey cascade;
ERROR:  constraint "tmp_cst_pcp_avy_sttn_hw_pkey" does not exist
不加模式名和加模式名都尝试了,都删不掉
zxy=> alter table a0.tmp_cst_pcp_avy_sttn_hw drop constraint tmp_cst_pcp_avy_sttn_hw_pkey cascade;
ERROR:  constraint "tmp_cst_pcp_avy_sttn_hw_pkey" does not exist

想了很多招,最后翻数据字典,找到解决的办法,发现\d 查看表结构的和实际存在数据字典上的不太一样

例如:

zxy=> \d tmp_cst_pcp_avy_sttn_hw
     Table "a0.tmp_cst_pcp_avy_sttn_hw"
        Column         |         Type          | Modifiers 
-----------------------+-----------------------+-----------
 dccp_avy_id           | character varying(40) | not null
 avy_aw_cnd_id         | character varying(20) | not null
 cst_id                | character varying(18) | not null
 multi_tenancy_id      | character varying(5)  | not null
 stat_dt               | date                  | not null
 cstpcpavyfit_cnd_dnum | numeric(5,0)          | 
 cstpcpavyfitcnd_acamt | numeric(15,2)         | 
 avy_aw_rslt_stcd      | character varying(2)  | 
 ccycd                 | character varying(3)  | 
 cardno                | character varying(19) | 
 txn_dt                | date                  | 
 prz_tpcd              | character varying(2)  | 
 aspd_id               | character varying(8)  | 
Indexes:
    "tmp_cst_pcp_avy_sttn_hw_pkey" PRIMARY KEY, btree (dccp_avy_id, avy_aw_cnd_id, cst_id, stat_dt, multi_tenancy_id)
Distributed by: (dccp_avy_id, avy_aw_cnd_id, cst_id, stat_dt, multi_tenancy_id)

这个时候以为该表的主键是tmp_cst_pcp_avy_sttn_hw_pkey
实际上
zxy=> select oid from pg_class where relname='tmp_cst_pcp_avy_sttn_hw';
  oid  
-------
 54934
(1 row)

zxy=> \d pg_constraint
    Table "pg_catalog.pg_constraint"
    Column     |    Type    | Modifiers 
---------------+------------+-----------
 conname       | name       | not null
 connamespace  | oid        | not null
 contype       | "char"     | not null
 condeferrable | boolean    | not null
 condeferred   | boolean    | not null
 conrelid      | oid        | not null
 contypid      | oid        | not null
 confrelid     | oid        | not null
 confupdtype   | "char"     | not null
 confdeltype   | "char"     | not null
 confmatchtype | "char"     | not null
 conkey        | smallint[] | 
 confkey       | smallint[] | 
 conbin        | text       | 
 consrc        | text       | 
Indexes:
    "pg_constraint_oid_index" UNIQUE, btree (oid)
    "pg_constraint_conname_nsp_index" btree (conname, connamespace)
    "pg_constraint_conrelid_index" btree (conrelid)
    "pg_constraint_contypid_index" btree (contypid)


zxy=> select conname from pg_constraint where conrelid=54934;
       conname       
---------------------
 pk_cst_pcp_avy_sttn
(1 row)

实际上是pk_cst_pcp_avy_sttn
发现区别了吧,这个时候再去删除主键
zxy=> alter table tmp_cst_pcp_avy_sttn_hw drop constraint pk_cst_pcp_avy_sttn cascade;
ALTER TABLE
zxy=> \d tmp_cst_pcp_avy_sttn_hw
     Table "a0.tmp_cst_pcp_avy_sttn_hw"
        Column         |         Type          | Modifiers 
-----------------------+-----------------------+-----------
 dccp_avy_id           | character varying(40) | not null
 avy_aw_cnd_id         | character varying(20) | not null
 cst_id                | character varying(18) | not null
 multi_tenancy_id      | character varying(5)  | not null
 stat_dt               | date                  | not null
 cstpcpavyfit_cnd_dnum | numeric(5,0)          | 
 cstpcpavyfitcnd_acamt | numeric(15,2)         | 
 avy_aw_rslt_stcd      | character varying(2)  | 
 ccycd                 | character varying(3)  | 
 cardno                | character varying(19) | 
 txn_dt                | date                  | 
 prz_tpcd              | character varying(2)  | 
 aspd_id               | character varying(8)  | 
Distributed by: (dccp_avy_id, avy_aw_cnd_id, cst_id, stat_dt, multi_tenancy_id)

zxy=> 

完全删除了

总结:GP删除主键的语句:ALTER TABLE 表名 DROP CONSTRAINT 约束名 [RESTRICT | CASCADE]

如果报约束名不存在时,去数据字典(pg_class和pg_constraint)中查找,去查实际的约束名

 

你可能感兴趣的:(GreeenPlum删除主键,Postgresql删除主键,GreenPlum)