天天看点

PostgreSql的基本语法总结对表的操作其他小结

PostgreSql的基本语法总结

  • 对表的操作
    • 1、创建表
      • 1、创建表的语句如下
      • 2、为表增加一列
      • 3、删除表的其中一列
      • 3、修改表的其中一列
  • 其他小结
    • 1、DISTINCT合并重复行
    • 2、having的用法
    • 3、模糊查询
    • 4、in与not in
    • 5、时间类型增加或减少
    • 6、pg索引
    • 7、主键修改
    • 8、pg触发器
      • 1、创建pg触发器
      • 2、删除pg触发器

对表的操作

1、创建表

1、创建表的语句如下

create table books(
   bno 			int primary key     	not null,
   bname      	text 					not null,
   author     	varchar(50) 			not null,
   price		int						not null,
   quantity		int						not null
);  
create table card(
   cno 		  	int primary key     	not null,
   name       	varchar(50) 			not null,
   class      	varchar(50) 			not null
);
create table borrow(
   id 			int 			     	not null,
   cno 			int references card(cno),
   bno        	int references books(bno),
   rdate      	date,
   primary key(id,cno,bno)     	    			
);  

           

在这里顺便解释一下text,char,varchar的区别

数据类型 描述
text 可变无限长度
char 相当于char(1),只存一个字符,不够用空白填充
char(n) 存n一个字符,不够用空白填充
varchar 相当于text
varchar(n) 最多可存储n个字符,长度可变

注:在PostgreSql里面,三种数据类型没有性能差异,一般推荐试用text和varchar,有限制的情况下给varchar加个参数即可。

2、为表增加一列

当为学生表增加一列课程(cn)时,可以如下操作(前提是表必须存在):

3、删除表的其中一列

删除学生表得列为课程(cn)时,可以如下操作(前提是表必须存在):

3、修改表的其中一列

其他小结

1、DISTINCT合并重复行

对于括号里面sno属性进行合并。

2、having的用法

在查询出来的数据中,检索符合某个变量范围的数据。

PostgreSql的基本语法总结对表的操作其他小结

根据上图,通过cno分组并统计打印出总数大于等于5的数据

统计如下

select cno,count(*) from borrow
	group by cno
           
PostgreSql的基本语法总结对表的操作其他小结
select cno,count(*) from borrow
	group by cno
	having count(*) >= 5
           
PostgreSql的基本语法总结对表的操作其他小结

3、模糊查询

此处比较简单,举个例子就OK

查询姓名中第2个字为“明”字的学生学号、姓名

4、in与not in

字面意思,存在与不存在

栗子:

select *from books
	where bno in (1,3,4)
           

5、时间类型增加或减少

update borrow set rdate = rdate + INTERVAL '1 D'
update borrow set rdate = rdate - INTERVAL '1 D'
           
缩写 意义
Y Years
M Months
W Weeks
D Days
H Hours
M Minutes
S Seconds

注:注意Month和Minute,如果用M,数据库默认是分钟,尽量用全称,区分一下。

6、pg索引

pg里面有多种索引,我这里介绍四个。

1. B-Tree

语法规则:

create index 索引名 on 表名(要创建索引的列);

适用场景:B-tree可以处理对可以排序成某些顺序的数据的等式和范围查询。特别地,当索引列参与使用以下运算符之一的比较时, PostgreSQL查询计划器将考虑使用B-tree索引(<、<=、=、>、>=)。

2. Hash

语法规则:

create index 索引名 on 表名 using hash (要创建索引的列);

适用场景:Hash索引只能处理简单的等式比较。当使用=运算符进行比较时,查询计划器将考虑使用Hash索引。

3. GIST

4. SP-GIST

5. GIN

6. BRIN

7、主键修改

因为主键必须唯一不能重复,所以,要先删除主键,在建立。

删除旧的主键,语法规则如下:

alter table 表名 drop constraint 主键名;

主键名一般为表名加下划线pkey

建立新主键,语法规则如下:

alter table 表名 add primary key(新列名);

8、pg触发器

1、创建pg触发器

创建触发器需要创建两个东西,一个是触发器,一个是触发器调用的函数:

函数语法规则如下:

栗子:

create or replace function borrow_save_trigger()
returns trigger as $do$
begin	
	if (select bname from books where bno = new.bno) = '数据库技术及应用' 
	then
		insert into borrow_save values(new.id,new.cno,new.bno,new.rdate);
	else
		--此处没有代码
	end if;
	return new;
end;
$do$
language plpgsql;
           

参数解释:

语法名词 解释
create or replace function 创建或更换(修改)函数,基本不变
borrow_save_trigger 自己命名的函数名
returns trigger as 返回触发器,以’$do$ begin’开始,以’end; $do$ ’结束
language plpgsql; 表示语言为pgSql语言
return new; 返回一个结果

其余部分则写自己的逻辑代码。

触发器语法规则如下:

create trigger trigger_name [before|after] [update|delete|insert|select] on table_name (of column_name可无)
	for each row execute procedure funcation_name();
           

栗子:

create trigger save_trigger 
    after insert ON borrow
    for each row execute procedure borrow_save_trigger();
           

参数解释:

语法名词 解释
trigger_name 触发器名
table_name 表名
column_name 列名
funcation_name 函数名

2、删除pg触发器

语法规则如下: