暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

视图,索引,用户权限学习

原创 郭鑫 2020-03-06
797

create table users (id int,password varchar(256));
create view view_users as select id from users;
create temp view view_users as select id from users;
create view view_users (no) as select id from users;

9.3版本以前无法更新视图,需要定义更新,删除,插入规则才行

创建索引不能增删改

gin反转索引(支持包含@>,被包含@<,相等=,重叠&&)
create table contacts (id int primary key,name varchar(40),phone varchar(32)[],address text);
create index idx_phone on contacts using gin(phone);
select * from contacts where phone @> array [‘12345’::varchar(32)];

降序建索引
create index a_idx on contacts on contacts(name desc);

如果name字段存在空值可以指定空值排在非空前面,或后面
create index a_idx on contacts on contacts(name desc nulls first/last);

大表并发创建索引create index concurrently idx_test on test (note);
重建索引不支持concurrently,但支持在一个字段建立两个索引,可以建一个新的索引,然后把旧的删掉。

并发创建索引时中断invalid状态的索引必须要删除,因为它会影响查询效率。

修改索引:
alter index idx_a rename to xxx
alter index idx_a set tablespace xxx

pg使用角色的概念管理数据库访问权限,角色可以是用户
create role(无login) create use(有login)

只有superuser可以创建superuser

createdb权限,createrole权限,createuser权限

in role name(指定用户成为哪些角色成员)

如果给用户赋权创建数据库是alter role,给用户创建模式则是grant ,比如grant xxx on xxx

all privileges=grant 所有权限

权限层次
超级用户,创建数据库,用户,login权限
在模式中创建数据库对象(表,索引)
dml权限
操作表字段权限

只读权限:
grant select on all tables in schema public to xxx;
alter default privileges in schema public grant select on tables to xxx;

grant select on all tables in schema xxx to xxx;
alter default privileges in schema xxx grant select on tables to xxx;

pg可以在事务中回滚ddl!

savepoint可以在大事务中把操作拆分,这样出错不必回滚整个事务
rollback to savepoint mypoint;

事务持久化参数(max_prepared_transactions=10)开始一个事务,即使重启库,这个事务也可以提交

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论