触发器(tigger)是一种由事物自动触发执行的特殊存储过程,这些事件可以是对一个表进行INSERT、UPDATE、DELETE等操作。

一、创建触发器

create   [ constraint ]   trigger name {  before |  after | instead of} { event [ or....] }  on table_name [from referenced_table_name]

{not deferrable | [deferrable] {intially immediate |  initially deferred }}

[for [each]  {row  |  statement}]

[when (condition)]

execute procedure function_name (arguments)

1  实例:删除学生表student的一条记录是,同事删掉score的成绩记录也删除。

1.1 创建表

postgres=# create table student(
student_no int primary key,
sutdent_name varchar(40),
age int);
CREATE TABLE postgres=# create table score(
postgres(# student_no int,
postgres(# chinese_score int,
postgres(# test_date date);
CREATE TABLE
postgres=#

  

1.2 创建执行函数

postgres=# create or replace function student_delete_trigger()
postgres-# returns trigger as $$
postgres$# begin
postgres$# delete from score where student_no = OLD.student_no;
postgres$# return OLD;
postgres$# end;
postgres$# $$
postgres-# language plpgsql;
CREATE FUNCTION

  

1.3 在创建触发器

postgres=# create trigger delete_student_trigger
postgres-# after delete on student
postgres-# for each row execute procedure student_delete_trigger();
CREATE TRIGGER
postgres=#

  

1.4 插入数据

postgres=# insert into score values(1,85,date '2018-05-16');
INSERT 0 1
postgres=# insert into score values(2,56,date '2018-05-16');
INSERT 0 1
postgres=# insert into score values(1,88,date '2018-05-16');
INSERT 0 1
postgres=# insert into score values(2,33,date '2018-05-16');
INSERT 0 1
postgres=# insert into score values(3,43,date '2018-05-16');
INSERT 0 1
postgres=# insert into score values(3,53,date '2018-05-16');
INSERT 0 1

  

1.5 删除学号为3的学生

postgres=# delete from student where student_no = 3;
DELETE 1
postgres=# select * from score;
student_no | chinese_score | test_date
------------+---------------+------------
1 | 85 | 2018-05-16
2 | 56 | 2018-05-16
1 | 88 | 2018-05-16
2 | 33 | 2018-05-16
(4 rows) postgres=#

  

未完待续。。。

postgresql逻辑结构--触发器(三)的更多相关文章

  1. postgresql逻辑结构(一)

    一.数据库逻辑结构介绍 数据库:应用连接到一个数据库时,一般不能访问其它数据库,除非使用dblink等其他手段. 表.索引:postgresql中标的术语为relation,其它数据库中成为table ...

  2. PostgreSQL 创建触发器 Trigger

    触发器的知识点: PostgreSQL在以下情况下执行/调用触发器:在尝试操作之前(在检查约束并尝试INSERT,UPDATE或DELETE之前).或者在操作完成后(在检查约束并且INSERT,UPD ...

  3. [转]PostgreSQL 逻辑结构 和 权限体系 介绍

    摘要: 本文旨在帮助用户理解PostgreSQL的逻辑结构和权限体系,帮助用户快速的理解和管理数据库的权限. 逻辑结构 最上层是实例,实例中允许创建多个数据库,每个数据库中可以创建多个schema,每 ...

  4. 触发器三(行级DML触发器)(学习笔记)

    行级DML触发器 每当一条记录出现更新操作时进行触发操作定义时要定义FOR EACH ROW 使用":old.字段"和":new.字段"标识符 No. 触发语句 ...

  5. 跟我一起读postgresql源码(三)——Rewrite(查询重写模块)

    上一篇博文我们阅读了postgresql中查询分析模块的源码.查询分析模块对前台送来的命令进行词法分析.语法分析和语义分析后获得对应的查询树(Query).在获得查询树之后,程序开始对查询树进行查询重 ...

  6. MySql、PostgreSql、SqlServer三种数据库的造数存储过程实例

    主要实例:把临时表tmp_table数据插入到目标表target_table 一.MySql造数存储过程实例 mysql造数 -- 第一步,创建临时表 CREATE TEMPORARY TABLE I ...

  7. 触发器-- 肖敏_入门系列_数据库进阶 60、触发器(三) --youku

    二 https://v.youku.com/v_show/id_XMzkxOTc5NDY0OA==.html?spm=a2h0k.11417342.soresults.dtitle 三 https:/ ...

  8. postgresql 的触发器

    今天编写了一个触发器 功能: 有两个表,当一个表的字段有所改动的时候,另一个表跟着改动 CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS T ...

  9. postgresql 表触发器

    1.先建一个函数,用来执行触发器启动后要执行的脚本 CREATE OR REPLACE FUNCTION "public"."trigger_day_aqi"( ...

随机推荐

  1. Write Markdown Syntax Online Document with Sphinx and Pandoc

    There is no doubt that we have to write doc while we are developing software. But How do you write d ...

  2. C++指针一

    指针也是变量,占有内存空间,用来保存内存地址. 指针也是一种数据类型,指针是一种数据类型,是指它所致内存空间的数据类型. 指针变量和它指向的内存块是两个不同的概念 *p操作内存 在指针声明时,*号表示 ...

  3. 广搜 迷宫(zznu 1962)

    http://acm.zznu.edu.cn/problem.php?id=1962 题目描述 在很多 RPG (Role-playing Games) 游戏中,迷宫往往是非常复杂的游戏环节.通常来说 ...

  4. 2017-2018-1 201552326《信息安全技术》实验二——Windows口令破解

    2017-2018-1 201552326<信息安全技术>实验二--Windows口令破解 姓名:刘美岑 学号:20155326 班级:1553班 日期:10.24 一.实验环境 操作系统 ...

  5. OS基础:动态链接库(二)

    1.vc6.0新建工程MFC AppWizard[dll]工程 命名LptMfcDll1 2.在lptMfcDll1.h添加函数名声明 添加的代码: //lptAddBegin void lptMfc ...

  6. 省市区三级联动picker-view-微信小程序

    这个是微信小程序的picker-view组件,开发的省市区3级联动,体验还不错,就分享给大家了, 此版本的省市区数据为本地JS数据,免去请求加载数据了,大家直接看源码,先看下面的动图:   项目地址: ...

  7. 索引视图DEMO1

    --use tempdb ----------------------在创建视图和所有底层表时,必须打开ANSI_NULLS以及QUOTED_IDENTIFIER选项 --SET ANSI_NULLS ...

  8. html5 Ajax 访问.net WebApi获取视频流

    http://localhost//api/Test/GetVideo?filename=/GoodVideo/e36a144b-52cd-4174-93d2-cfc41aea6c1d.mp4 是AP ...

  9. C++数组初始化方法

    定义: ]; // array of 10 uninitialized ints 此 new 表达式分配了一个含有 10 个 int 型元素的数组,并返回指向该数组第一个元素的指针,此返回值初始化了指 ...

  10. 常用Java集合类总结

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 7.1.List(允许重复元素) ArrayList: 底层数据结构:Object[] 在查询(get).遍 ...