pgsql 的表相关的操作

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:否)';

 


————————————————

原文链接:https://blog.csdn.net/weixin_41908066/article/details/111614266

posted @ 2021-09-24 12:10  曹丽是我女朋友。  阅读(418)  评论(0编辑  收藏  举报