Postgresql 函式 觸發器寫法

2021-08-26 07:02:28 字數 1299 閱讀 8959

1、資料庫環境

-- table: 學生分數表

create table stu_score

( stuno serial not null, --學生編號

major character varying(16), --專業課程

score integer --分數

)with (

oids=false

);alter table stu_score owner to postgres;

-- table: 專業狀態表,儲存哪個專業有多少學生報名

create table major_stats

( major character varying(16), --專業課程

total_score integer, --總分

total_students integer --學生總數

)with (

oids=false

);alter table major_stats owner to postgres;

2、函式、儲存過程

create or replace function fun_stu_major()

returns trigger as

$body$

declare

rec record;

begin

delete from major_stats;--將統計表裡面的舊資料清空

for rec in (select major,sum(score) as total_score,count(*) as total_students

from stu_score group by major) loop

insert into major_stats values(rec.major,rec.total_score,rec.total_students);

end loop;

return new;

end;

$body$

language 'plpgsql' volatile

3、正式建立觸發器trigger

create trigger tri_stu_major 

after insert or update or delete

on stu_score

for each row

execute procedure fun_stu_major()

postgresql 觸發器寫法

create or replace function delete fdr returns trigger as delete fdr begin delete from object classes where object classes dataroom id old dataroom id ...

postgresql 觸發器 觸發函式 動態建表

postgresql 建立分割槽表,sql優化之postgresql table partitioning postgresql中使用動態sql 實現自動按時間建立表分割槽 declare createon varchar 32 createyy varchar 4 tablename varcha...

函式 觸發器

函式 觸發器2009 05 26 13 39 分類 oracle語句 函式 create or replace function sal tax v sal number return number is begin if v sal 2000 then return 0.10 elsif v sa...