欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PostgreSQL、pgsql为表添加列或判断列存不存在再添加列的方法

程序员文章站 2022-03-23 14:21:02
1.通用方法: eg: do $$ begin begin alter table dingtalk_corp_info add colum...

1.通用方法:

eg:

do $$
    begin
        begin
            alter table dingtalk_corp_info add column admin_id text;
        exception
            when duplicate_column then raise notice 'column admin_id already exists in dingtalk_corp_info.';
        end;
    end;
$$;

2.pgsql 9.6+:

eg:

alter table dingtalk_corp_info add column if not exists admin_id text;

3.直接添加

eg:

alter table dingtalk_corp_info
add column admin_id text;