天天看點

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觸發器

文法規則如下: