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;

  

  

上一篇:Greenplum 调优--VACUUM系统表


下一篇:C# Firefox Session Manager 文件的导出与管理