postgresql 删除、增加、修改字段;修改表名,字段类型

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:否)';
  • 21
    点赞
  • 148
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

unix_sky

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值