1. 创建一个触发器,表中的行在任何时候被插入或更新时,当前用户名和时间也会被标记在该行中。并且它会检查雇员的姓名以及薪水。

--创建测试表
CREATE TABLE emp (
empname text,
salary integer,
last_date timestamp,
last_user text
); --创建触发器函数
CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
-- 检查 empname 以及 salary
IF NEW.empname IS NULL THEN
RAISE EXCEPTION 'empname cannot be null';
END IF;
IF NEW.salary IS NULL THEN
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
END IF; -- 谁会倒贴钱为我们工作?
IF NEW.salary < 0 THEN
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
END IF; -- 记住谁在什么时候改变了工资单
NEW.last_date := current_timestamp;
NEW.last_user := current_user;
RETURN NEW;
END;
$emp_stamp$ LANGUAGE plpgsql; --创建触发器
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW EXECUTE PROCEDURE emp_stamp(); --测试触发器
test=# insert into emp values ('John'); --salary为空,触发器报错
ERROR: John cannot have null salary
CONTEXT: PL/pgSQL function emp_stamp() line 7 at RAISE test=# insert into emp values (null,1200); --empname为空,触发器报错
ERROR: empname cannot be null
CONTEXT: PL/pgSQL function emp_stamp() line 4 at RAISE test=# insert into emp values ('John',-200); --salary为负数,触发器报错
ERROR: John cannot have a negative salary
CONTEXT: PL/pgSQL function emp_stamp() line 10 at RAISE test=# insert into emp values ('Bob',1200); --成功插入正常数据,并记录了最后操作时间和操作用户
INSERT 0 1
test=# select * from emp;
empname | salary | last_date | last_user
---------+--------+----------------------------+-----------
Bob | 1200 | 2017-08-09 17:39:23.671957 | postgres
(1 row)

2. 用于审计的触发器过程
这个例子触发器保证了在emp表上的任何插入、更新或删除一行的动作都被记录(即审计)在emp_audit表中。当前时间和用户名以及在其上执行的操作类型都会被记录到行中。

--创建测试表
create table emp (
empname text not null,
salary integer
); --创建审计表
create table emp_audit(
operation char(1) not null,
stamp timestamp not null,
userid text not null,
empname text not null,
salary integer
); --创建触发器函数
create or replace function process_emp_audit() returns trigger as $emp_audit$
begin
if (TG_OP = 'DELETE') then
insert into emp_audit select 'D',now(),user,old.*;
return old;
elsif (TG_OP = 'UPDATE') then
insert into emp_audit select 'U',now(),user,new.*;
return new;
elsif (TG_OP = 'INSERT') then
insert into emp_audit select 'I',now(),user,new.*;
return new;
end if;
return null;
end;
$emp_audit$ language plpgsql; --创建触发器
create trigger emp_audit
after insert or update or delete on emp
for each row execute procedure process_emp_audit(); --测试触发器
test=# insert into emp values ('John',1200);
INSERT 0 1
test=# select * from emp_audit;
operation | stamp | userid | empname | salary
-----------+----------------------------+----------+---------+--------
I | 2017-08-09 18:18:10.189772 | postgres | John | 1200
(1 row)

The End!

2017-08-17

【PostgreSQL-9.6.3】触发器实例的更多相关文章

  1. SQL触发器实例讲解

    SQL触发器实例1 定义: 何为触发器?在SQL Server里面也就是对某一个表的一定的操作,触发某种条件,从而执行的一段程序.触发器是一个特殊的存储过程. 常见的触发器有三种:分别应用于Inser ...

  2. SQL触发器实例

    SQL触发器实例讲解(本文是来自百度文库) 备注:本人建了一个站特价汇,我想记录每个商品的点击量,然后按照点击量来牌名商品,想要提高效率,所以必须得用触发器,下面是本人在百度文库中的找到的学习资料,分 ...

  3. [SQL SERVER系列]存储过程,游标和触发器实例[原创]

    自己写的存储过程与游标结合使用的实例,与大家分享,也供自己查阅,仅供参考: --使用游标循环处理,删除重复的记录 declare @UserID int ) ) declare @UnitFlag i ...

  4. SQL触发器实例(下)

    基本语法: Create Trigger [TriggerName] ON [TableName] FOR [Insert][,Delete][,Update] AS --触发器要执行的操作语句. G ...

  5. PL/SQL之存储过程和触发器实例

    1.Oracle存储过程实例 /*不带任何参数存储过程(输出系统日期)*/ CREATE OR REPLACE PROCEDURE output_date IS BEGIN DBMS_OUTPUT.P ...

  6. sqlserver 触发器实例代码

    定义: 何为触发器?在SQL Server里面也就是对某一个表的一定的操作,触发某种条件,从而执行的一段程序.触发器是一个特殊的存储过程. 常见的触发器有三种:分别应用于Insert , Update ...

  7. oracle触发器实例

    8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2.3 创建替代(INS ...

  8. Mysql触发器实例分析

    所谓触发器,就是在定义在表对象上.当触发器所在的表出现指定的事件时,会触发对应表的delete update insert的操作.说的有点绕口,其实就是到监视某种情况,然后去触发某种操作. 触发器是如 ...

  9. sqlserver 触发器实例

    实例1:update USE [数据库名称]GO/****** Object: Trigger [dbo].[触发器名称] Script Date: 05/08/2014 12:40:25 ***** ...

  10. Oracle触发器实例(网搜)

    触发器使用教程和命名规范 目  录触发器使用教程和命名规范 11,触发器简介 12,触发器示例 23,触发器语法和功能 34,例一:行级触发器之一 45,例二:行级触发器之二 46,例三:INSTEA ...

随机推荐

  1. js 发布订阅模式

    //发布订阅模式 class EventEmiter{ constructor(){ //维护一个对象 this._events={ } } on(eventName,callback){ if( t ...

  2. SpringBoot @ConditionalOnBean、@ConditionalOnMissingBean注解源码分析与示例

    前言: Spring4推出了@Conditional注解,方便程序根据当前环境或者容器情况来动态注入bean,对@Conditional注解不熟悉的朋友可移步至 Spring @Conditional ...

  3. 总结for循环及for循环增强遍历数组,list,set和map

    一.对于集合 (1)普通for循环 int[] arr = { 2, 1, 2 }; for(int i=0;i<arr.length;i++){ System.out.println(arr[ ...

  4. Oracle怎么用(常用工具)

    ​ Oracle数据库管理系统装好了!那要怎么用呢? 将介绍的工具:①Database Configuration Assistant ②SQL Plus ③SQL Developer ​ 一.Dat ...

  5. one troubleshooting case about em access issue

    Today when trying to open my Oracle EM url, I can not open it. So I thought may be the network is ha ...

  6. scp: useful commands

    Examples Copy the file "foobar.txt" from a remote host to the local host $ scp your_userna ...

  7. 【CV知识学习】【转】beyond Bags of features for rec scenen categories。基于词袋模型改进的自然场景识别方法

    原博文地址:http://www.cnblogs.com/nobadfish/articles/5244637.html 原论文名叫Byeond bags of features:Spatial Py ...

  8. JConsole使用手冊具体解释

    一篇Sun项目主页上介绍JConsole使用的文章,前段时间性能測试的时候大概翻译了一下以便学习,今天整理一下发上来.有些地方也不知道怎么翻,就保留了原文,可能还好理解点.呵呵,水平有限,翻的不好,大 ...

  9. python supervisor进程监控工具的使用

    supervisor —— a process control system 另外一个类似 supervisor的工具,因为supervisor 不兼容python3, !!! Circus Proc ...

  10. hdu5396 Expression 区间dp +排列组合

    #include<stdio.h> #include<string> #include<map> #include<vector> #include&l ...