索引是对数据库表中一列或多列值进行排序的一种结构,使用
索引可提高数据库中特定数据的查询速度
索引是一种单独的、存储在磁盘上的数据库结构,他们包含着对
数据表里所有记录的引用指针.索引用于快速找出在某个
或多个列中有一特定值的行,所有PostgreSQL列类型都可
以被索引,对相关列使用索引是提高查询操作时间的最佳
途径
索引是在存储引擎中实现,因此每种存储引擎的索引都不一定完
全相同,并且每种存储引擎也不一定支持所有索引类型.根据
存储引擎定义每个表的最大索引数和最大索引长度.所有存
储引擎支持每个表至少16个索引,总索引长度至少256字节.
大多数存储引擎有更高的限制.
Hash索引
只能处理简单的等于比较.当一个索引的列涉及使用 = 操作符
进行比较的时候,查询规划器会考虑使用Hash索引
GiST索引
不是单独一种索引类型,而是一种架构,可以在这种架构上实现很
多不同的索引类型策略,因此,可以使用GiST索引的特定操作
符类型高度依赖于索引策略(操作符等)
GIN索引
反转索引,可以处理包含多个键的值(比于数组).和GiST类似,GIN
支持用户定义的索引策略,可以使用GIN索引的特定操作符类型
根据索引策略的不同而不同
create [unique | fulltext | spatial] index 索引名
on 表名 (字段名 [长度],...) [ASC | DESC];
先建表book:
create table book(
bookid int not null,
bookname varchar(255) not null,
authors varchar(255) not null,
info varchar(255) null,
comment varchar(255) null,
year_publication date not null
);
create index bknameidx on book(bookname);
create unique index uniqididx on book(bookid);
create index bkcmtidx on book(comment);
create index bkauandinfoidx on book(authors,info);
alter index 旧索引名 rename to 新索引名;
alter index bkauandinfoidx rename to publicbk;
alter table 表名 rename index 旧索引名 to 新索引名;
alter table book rename index bkauandinfoidx to publicbk;
drop index 索引名;
drop index publicbk;
alter table 表名 drop index 索引名;
视图是一个虚拟表,是从数据库中一个或多个表中导出来的表,还可以
从已经存在的视图的基础上定义
先创建表student和stu_info并插入数据
create table student(
s_id int,
name varchar(40)
);
insert into student
values(1,‘孙悟空‘),(2,‘猪悟能‘),(3,‘沙悟净‘);
create table stu_info(
s_id int,
glass varchar(40),
addr varchar(90)
);
insert into stu_info
values(1,‘五班‘,‘花果山‘),(2,‘六班‘,‘高老庄‘),
(3,‘七班‘,‘流沙河‘)
create [or replace] [algorithm = {undefined | merge | temptable}]
view 视图名 [(字段名)]
as 查询语句
[with [cascaded | local] check option]
在数据表t上创建名为view_t的视图:
create table t(quantity int,price int);
insert into t values(3,50);
create view view_t
as select quantity,price,quantity * price from t;
`select * from view_t;`
create view stu_glass(id,name,glass)
as select student.s_id,student.name,stu_info.glass
from student,stu_info where student.s_id = stu_info.s_id;
select * from stu_glass;
查看数据库中所有视图的详细信息
select * from information_schema.views;
drop view [if exists] 视图名[,视图名...] [restrict | cascade]
删除视图必须拥有drop权限
drop view if exists view_t;
PostgreSQL中视图和表的区别:
PostgreSQL中视图和表的联系:
一个触发器是一种声明,告诉数据库应该在执行特定操作的时候执行特定的函数.
触发器的执行不需要call语句来调用,也不需要手工启动,只要当一个预定以
的时间发生的时候,就会被PostgreSQL自动调用
触发器函数,是指一个没有参数并且返回trigger类型的函数,在创建触发器之前,
首先需要创建触发器函数
create function fun_name() returns trigger as $fun_name$
begin
函数执行代码
end;
$ fun_name $ language plpgsql;
create trigger 触发器名 before | after 触发事件
on 表名 for each row execute procedure 触发器函数;
创建一个触发器,使得每次有新数据插入时,其中的时间字段uptime自动变更为
当前时间
create table timedb(
uid int,
gid int,
uptime timestamp with time zone
);
create function func_timedb () returns trigger as $func_timedb$
begin
if (tg_op = ‘update‘) then
if new.uptime = old.uptime then
return null;
end if;
end if;
update timedb set uptime = now() where uid = new.uid and gid = new.gid;
return null;
end;
$func_timedb$ language plpgsql;
create trigger timedb_update after insert on timedb
for each row execute procedure func_timedb ();
insert into timedb values(1,3);
select * from timedb;
drop trigger 触发器名 on 表名;
事务的属性:
原子性 一致性 隔离性 持久性
begin;
SQL 语句1;
...
commit;
start transaction
开始一个新的事务块
begin
表示初始化一个事务块.在begin命令后的语句都将在一个事务里面
执行,知道出现commit或rollback.此命令和start transaction等价
commit
表示提交事务
rollback
事务失败时执行回滚操作
set transaction
设置当前事务的特性,对后面的事务没有影响
创建角色:
create role 角色名;
create role post1;
查看系统中的角色
select rolname from pg_roles;
修改角色名称
alter role 组角色名称 rename to 新角色名;
删除角色
drop role 组角色名称;
create role post2 createdb;
口令
create role name 口令认证方式 具体口令;
create role post3 password ‘123456‘;
创建用户
create user 用户名;
删除用户
drop user 用户名;
修改密码
alter user 用户名 口令认证方式 新密码;
对组角色授权
alter role 角色名 权限1,权限2...;
对用户授权
alter user 角色名 权限1,权限2...;
收回组角色权限
alter user 角色名 no权限1,no权限2...;
`alter user post nocreatedb;`
收回用户权限
alter role 角色名 no权限1,no权限2...;
修改数据库的拥有者
alter database 数据库名 owner to 拥有者名称;
增加用户的数据表权限
grant 权限 on 数据表 to 用户名称;
原文:https://www.cnblogs.com/wangbaby/p/10289862.html