zoukankan      html  css  js  c++  java
  • postgresql change table

    --if cloumn exist
    SELECT  EXISTS (SELECT 1 
    FROM information_schema.columns 
    WHERE table_schema='ent' AND table_name='AdsPlatform' AND column_name='Name');
    
    
    --add cloumn
    ALTER TABLE finance."MappingInfo" ADD COLUMN IF NOT EXISTS  "Active" boolean;
    UPDATE finance."MappingInfo" SET "Active"=false where "Active" is null  ;
    ALTER TABLE finance."MappingInfo" ALTER COLUMN "Active" set NOT NULL;
    --or
    ALTER TABLE finance."MappingInfo" ALTER COLUMN "Active" Drop NOT NULL;
    --remove column
    ALTER TABLE finance."MappingInfo" DROP COLUMN IF EXISTS "WarnMsg";
    
    --add column & set column value from another
    
    ALTER TABLE ent."AdsPlatform" ADD COLUMN IF NOT EXISTS "PlatfromSettlementParty" character varying(100);
    
    DO
    $do$
    BEGIN
    IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='ent' AND table_name='AdsPlatform' AND column_name='Name') THEN
       UPDATE ent."AdsPlatform" SET "PlatfromSettlementParty"="Name" WHERE "PlatfromSettlementParty" is null;
    ELSE 
        UPDATE ent."AdsPlatform" SET "PlatfromSettlementParty"= '' where "PlatfromSettlementParty" is null ;
    END IF;
    END
    $do$
    
    ALTER TABLE ent."AdsPlatform" ALTER COLUMN "PlatfromSettlementParty" set NOT NULL;
    

      

      

  • 相关阅读:
    BZOJ1841 : 蚂蚁搬家
    BZOJ3068 : 小白树
    BZOJ4449 : [Neerc2015]Distance on Triangulation
    BZOJ3692 : 愚蠢的算法
    BZOJ3145 : [Feyat cup 1.5]Str
    BZOJ4684 : Company Organization
    BZOJ2934 : [Poi1999]祭坛问题
    ML(2)——感知器
    ML(附录1)——梯度下降
    微服务架构
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/9207917.html
Copyright © 2011-2022 走看看