温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在PostgreSQL数据库中对序列进行增删改操作

发布时间:2021-01-04 16:22:54 来源:亿速云 阅读:302 作者:Leah 栏目:开发技术

如何在PostgreSQL数据库中对序列进行增删改操作?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创建序列

CREATE SEQUENCE if not exists test_mergetable_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 999999999
START 1
CACHE 1;
//或者: 
create sequence if not exists test_mergetable_id_seq increment by 1 minvalue 1 no maxvalue start with 1;

指定序列(给表的主键指定创建好的序列)

alter table test_mergetable alter column "i_id" set default nextval('test_mergetable_id_seq');

设置序列自增长从当前最大值开始

SELECT setval('test_mergetable_id_seq', (SELECT MAX(i_id) FROM test_mergetable));
alter sequence test_mergetable_id_seq start with 12;

删除序列

drop sequence IF EXISTS test_mergetable_id_seq

查看序列

SELECT nextval('test_mergetable_id_seq')

补充:pgsql的schema对用户授权,单个用户跨schema增删改查操作

--创建用户

create user user1;

--修改密码

alter user report with password 'password';

--授权查询权限

grant usage on schema schema1 to user1;
grant usage on schema schema2 to user1;

修改search_path可跨schema操作

set search_path = "$user",user1,user2

--授权schema:schema1给user1权限 这个权限太大需要慎用

grant all on schema schema1 to user1;

--授权schema的表权限给user1 用户权限太多需慎用

grant all on all tables in schema schema1 to user1;

--授权schema的表权限给user1 用户权限太多需慎用

grant all on all tables in schema schema1 to user1;

--授权某个schema的单个表查权限

grant select on schema2.table1           to user1;

--收回所有授权

revoke all on all tables in schema schema1 from user1;

--为某个特定用户设置search_path

alter user user1 set search_path="$user",user1,user2;

关于如何在PostgreSQL数据库中对序列进行增删改操作问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI