1. pgsql删除字段,存在该字段才删除
alter table 【表名】 drop column if exists 【字段名】;
2.pgsql批量添加/删除一张表的字段
a.增加
ALTER TABLE 【表名】ADD COLUMN 【字段名】【数据类型】 【限制】, ADD COLUMN 【字段名】【数据类型】 【限制】;ALTER TABLE userADD COLUMN user_name character varying not null,ADD COLUMN age smallint not null;
b.删除
ALTER TABLE 【表名】 DROP 【字段】,DROP 【字段】;ALTER TABLE userDROP user_name,DROP age
3. .修改表名
alter table 【表名】 rename to 【新表名】
4 .修改字段名
alter table 【表名】 rename 【字段名】 to 【新字段名】
5 .修改字段类型
如:ID 字段 原类型为 character varying(50) 新类型为integer其中,ID中原有数据为1,2,3等数字用如下语句更改alter table 【表名】 alter column 【字段名】 type 【字段类型】 using 【字段新类型】;alter table dbo.titemtype alter column id type integer using to_number(id,'9');
或者
--将 表 user的 update_time 字段更改为timestamp类型。ALTER TABLE 【表名】ALTER 【字段名】 TYPE 【字段新类型】alter table useralter update_time type timestamp(0)
6.修改字段备注
comment on column 【表名.字段名】 is 【注释】;comment on column t_user.isValid is '是否有效(1:是、2:否)';