1. pgsql删除字段,存在该字段才删除

alter table 【表名】 drop column if exists 【字段名】;

2.pgsql批量添加/删除一张表的字段

a.增加

ALTER TABLE 【表名】
 ADD COLUMN 【字段名】【数据类型】 【限制】, 
 ADD COLUMN 【字段名】【数据类型】 【限制】;

ALTER TABLE user
	ADD COLUMN user_name character varying not null,
 	ADD COLUMN age smallint not null;

b.删除

ALTER TABLE 【表名】 
	DROP 【字段】,
	DROP 【字段】;

ALTER TABLE user
	DROP 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 user
alter update_time type timestamp(0)

6.修改字段备注

comment on column 【表名.字段名】 is 【注释】;
comment on column t_user.isValid  is '是否有效(1:是、2:否)';
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐