关于postgresql触发器的总结(lab作业系列)
上题:
In this tutorial you will create a stored procedure and triggers to check a complex constraint. Consider the table definition below:

While the unique constraints defined here are sensible, they are not sufficient to express the constraint that a car (identified by its plate) cannot be rented more than once on any particular day.
1. Explain briefly why this constraint cannot be expressed using a CHECK constraint.
2. Write a stored procedure (function in postgres) which takes as input a plate, start date and end date and throws an error if table car rental contains any row with the same plate where the rental period is different from but overlaps with the given period. Note: Throwing errors in postgres is documented here: https://www.postgresql.org/docs/9.6/static/plpgsql-errors-and-messages.html Note: By checking only for different rental periods we don’t need to worry about the newly inserted or updated tuple using the procedure in a trigger (see next question). Duplicate periods are already prevented by the uniqueness constraints specified.
3.Create triggers which invoke the stored procedure created to enforce the “no duplicate rentals” constraint. Note: In postgres, trigger functions have no arguments and return type trigger: https://www.postgresql.org/docs/9.6/static/plpgsql-trigger.html Create a separate trigger function that meets these requirements and invokes the function defined earlier.
Note: You can test that your triggers work by inserting and updating tuples in car rentals:
-- should work
INSERT INTO car_rentals VALUES ('2-F4ST', '2015-02-02', '2015-02-11', 'DI123');
UPDATE car_rentals SET start_date = '2015-02-01', end_date = '2015-02-10';
INSERT INTO car_rentals VALUES ('SP33DY', '2015-01-20', '2015-02-05', 'DI234');
-- should fail (test individually)
UPDATE car_rentals SET plate = '2-F4ST' WHERE plate = 'SP33DY';
INSERT INTO car_rentals VALUES ('2-F4ST', '2015-02-10', '2015-02-15', 'DI234');
INSERT INTO car_rentals VALUES ('2-F4ST', '2015-01-20', '2015-02-15', 'DI234');
INSERT INTO car_rentals VALUES ('2-F4ST', '2015-02-02', '2015-02-09', 'DI234');
-- should work
INSERT INTO car_rentals VALUES ('2-F4ST', '2015-03-01', '2015-03-10', 'DI234');
-- additional test
update car_rentals set end_date = '2015-03-02' where plate = '2-F4ST' and license_nr='DI234'
第一问:check只能检测固定数值(给出来的值)进行约束,所以布恩能够用check,用触发器
首先是触发器,基本概念和用法:https://www.yiibai.com/postgresql/postgresql-trigger.html
建立一个触发器,首先先创造出对应函数,即你想让触发器完成什么样的工作,里面必须要有对条件的正确判断,begin开始,end结尾,外层套create的大套子,二三问和在一起写个触发器:
create or replace function chk_car_rentals()
returns trigger as $$
begin
if TG_OP='INSERT' then
if (select count(*) from car_rentals
where new.plate=plate and new.start_date <= end_date and new.end_date >= start_date
group by plate) >0 then
raise exception 'aaaaa';
return null;
end if;
elsif TG_OP='UPDATE' then
if new.plate <> old.plate then
if (select count(*) from car_rentals
where new.plate=plate and new.start_date <= end_date and new.end_date >= start_date
group by plate) >0 then
raise exception 'aaaaa';
return null;
end if;
elsif new.start_date < old.start_date or new.end_date > old.end_date then
if (select count(*)
from (select * from car_rentals
except
select *from car_rentals where old.plate=plate and old.start_date=start_date and old.end_date=end_date and old.license_nr=license_nr) as new_car_rentals
where new.plate=plate and new.start_date <= end_date and new.end_date >= start_date
group by plate) >0 then
raise exception 'bbbb';
return null;
end if;
end if;
end if;
return new;
end;
$$ language plpgsql; create trigger trg_car_rental
before insert or update on car_rentals
for each row execute procedure chk_car_rentals();
--如果是Insert 语句,语句应该是插入的新日期在原日期之内且车牌号相等,导致插入的时间段重复,引发异常
--如果是update 语句,当车牌号发生改变后,触发触发器的条件和上面 insert的条件相同,重复将异常
当车牌号不变,利用except除去表中旧记录的部分,select新的在所剩的表里面看是否还存在着“撞车”的情况,把异常条件写出来就好了 接下来就是测试这几条insert,update语句,应和预期结果相同
关于postgresql触发器的总结(lab作业系列)的更多相关文章
- PostgreSQL触发器的使用
原文: https://www.yiibai.com/postgresql/postgresql-trigger.html -------------------------------------- ...
- BCNF范式及其分解方法(对一次Lab作业的总结)
BCNF是比第三范式更严格一个范式.它要求关系模型中所有的属性(包括主属性和非主属性)都不传递依赖于任何候选关键字.也就是说,当关系型表中功能上互相依赖的那些列的每一列都是一个候选关键字时候,该满足B ...
- CentOS7下载配置PostgreSQL的pgAgent运行代理作业
1.安装PostgreSQL 参考官方文档https://www.postgresql.org/download/linux/redhat/,运行如下命令 yum install https://do ...
- postgresql 触发器
一.创建事件触发器 1.ddl_command_start - 一个DDL开始执行前被触发: 2.ddl_command_end - 一个DLL 执行完成后被触发: 3.sql_drop -- 删除一 ...
- postgresql 触发器 更新操作
1 前言 功能需求:当一张表格某个字段变化,另一张表某个字段写入该值 2 代码 CREATE OR REPLACE FUNCTION "public"."synStatu ...
- java数据结构和算法编程作业系列篇-数组
/** * 编程作业 2.1 向highArray.java程序(清单2.3)的HighArray类添加一个名为getMax()的方法,它返回 数组中最大关键字的值,当数组为空时返回-1.向main( ...
- 触发器-- 肖敏_入门系列_数据库进阶 60、触发器(三) --youku
二 https://v.youku.com/v_show/id_XMzkxOTc5NDY0OA==.html?spm=a2h0k.11417342.soresults.dtitle 三 https:/ ...
- SQL Server 创建作业系列问题
一.从IClassFactory为CLSID为{AA40D1D6-CAEF-4A56-B9BB-D0D3DC976BA2}的COM组件创建实例失败. 尊重原著作:本文转载自http://www.hao ...
- Xquery的初步学习(一次Lab作业的总结)
Task 1: Open countries.xml, compose the following XQueries: 1. Return the area of Mongolia. 2. Retur ...
随机推荐
- Django From组件 fields widgets
一.Form组件之字段 Field required=True, 是否允许为空 widget=None, HTML插件 label=None, 用于生成Label标签或显示内容 initial=Non ...
- 如何忽略.gitignore文件的提交
1.默认的.gitignore文件无法忽略,如果想要忽略可以把.gitignore里面的文件转移到项目下面的 .git/info/exclude 里面, 2..gitignore可以直接使用插件,参照 ...
- apache软件no_ssl和openssl两种类型的区别
apache软件同一版本有两种类型:no_ssl和openssl: openssl多了个ssl安全认证模式,它的协议是HTTPS而不是HTTP,这就是带有SSL的服务器与一般网页服务器的区别了. 一般 ...
- PLSQL中使用二维存储结构---二维数组
代码如下: --PLSQL中没有数组的概念 用TYPE 和Table of Record来代替多维数组--create by jenrry 20171028-- 1.0 建立存放列的测试表creat ...
- 便利的操作plist文件
便利的操作plist文件 升级iOS9了,网络被强制切换成https了,你需要更新你的plist的文件才能够支持http,正常的做法是这样子的: 过程是不是挺繁琐的呢?如果你新建的另外一个工程,里面还 ...
- 基于scrapyd爬虫发布总结
一.版本情况 python以丰富的三方类库取得了众多程序员的认可,但也因此带来了众多的类库版本问题,本文总结的内容是基于最新的类库版本. 1.scrapy版本:1.1.0 D:\python\Spid ...
- [2018HN省队集训D1T1] Tree
[2018HN省队集训D1T1] Tree 题意 给定一棵带点权树, 要求支持下面三种操作: 1 root 将 root 设为根. 2 u v d 将以 \(\operatorname{LCA} (u ...
- Subversion、TortoiseSVN、Ankhsvn+VS使用
Subversion为版本控制软件的服务器端.VisualSVN Server 可以在Windows平台安装和管理全功能的Subversion server. TortoiseSVN为Subversi ...
- 【Alpha】Daily Scrum Meeting——blog2
团队成员 吴吉键 201421122007(组长) 魏修祺 201421122008 孙劲林 201421122022 1. 会议当天照片 忘记拍了!(没有拍照片的习惯,没有第四人拍照) 2. 每个人 ...
- CI 2.2 + smarty 3.1.18 完美整合配置成功
CI 2.2 + smarty 3.1.18 配置成功 一.准备文档下载 (CI 框架和smarty) 二.将Smarty-3.1.18 源码包里面的libs文件夹copy到ci的项目目录appli ...