Constraints and Triggers

article/2025/6/19 6:00:03

目录

Kinds of Constraints

Single-Attribute Keys

Multiattribute Key

Foreign Keys

Expressing Foreign Keys

Enforcing Foreign-Key Constraints

Actions Taken

Attribute-Based Checks

Timing of Checks

Tuple-Based Checks

Assertions

Timing of Assertion Checks

Triggers: Motivation

Event-Condition-Action Rules

Preliminary Example: A Trigger


  • A constraint is a relationship among data elements that the DBMS is required to enforce.(约束是数据库系统强制加在数据元素之间的关系)
  • Example: key constraints.

Triggers are only executed when a specified condition occurs(e.g., insertion of a tuple.)

  • Easier to implement than complex constraints.

Kinds of Constraints

  • Keys.

  • Foreign-key, or referential-integrity.

  • Value-based constraints.

  • Constrain values of a particular attribute.

  • Tuple-based constraints.

  • Relationship among components.

  • Assertions: any SQL boolean expression.

Single-Attribute Keys

  • Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute.

  • Example:

CREATE TABLE Beers (name CHAR(20) UNIQUE,manf CHAR(20)
);

Multiattribute Key

The bar and beer together are the key for Sells:
CREATE TABLE Sells (bar CHAR(20),beer VARCHAR(20),price REAL,PRIMARY KEY (bar, beer)
);

多属性键,使用锁哥属性来标志唯一元组,适用于单属性无法标识的场景

Foreign Keys

  • Values appearing in attributes of one relation must appear together in certain attributes of another relation.(一个关系中的属性必定会出现在其他关系中)

  • Example: in Sells(bar, beer, price), we might expect that a beer value also appears in Beers.name .

Expressing Foreign Keys

Use keyword REFERENCES, either:

  • After an attribute (for one-attribute keys).

  • As an element of the schema:

FOREIGN KEY (<list of attributes>) REFERENCES <relation> (<attributes>)

Referenced attributes must be declared

PRIMARY KEY or UNIQUE

Enforcing Foreign-Key Constraints

If there is a foreign-key constraint from relation R to relation S, two violations are possible:

  • An insert or update to R introduces values not found in S.

  • A deletion or update to S causes some tuples of R to “dangle.”

Actions Taken

  • Default : Reject the modification.(默认情况是拒绝数据修改操作)

  • Cascade : Make the same changes in Sells.(级联检查)

Deleted beer: delete Sells tuple.

Updated beer: change value in Sells.

  • Set NULL : Change the beer to NULL.

Attribute-Based Checks

  • Constraints on the value of a particular attribute.
  • Add CHECK(<condition>) to the declaration for the attribute.
  • The condition may use the name of the attribute, but any other relation or attribute name must be in a subquery.(Checks允许使用多属性,但是如果出现跨表的多属性,其他表中的属性必须使用子查询)

Timing of Checks

Attribute-based checks are performed only when a value for that attribute is inserted or updated.(基于属性的检查只有在数据插入和更新时才触发)

  • Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5.

  • Example: CHECK (beer IN (SELECT name FROM Beers)) not checked if a beer is deleted from Beers (unlike foreign-keys).(这里可以看出与外键约束的不同,在Beers表中数据被删除时并不会检查这个Checks约束)

Tuple-Based Checks

CHECK (<condition>) may be added as a relation-schema element.

The condition may refer to any attribute of the relation.

But other attributes or relations require a subquery.

Checked on insert or update only

对比维度基于属性的检查(列级约束)基于元组的检查(表级约束)
能否使用本表中的其他属性?❌ 不能直接引用
约束表达式必须仅依赖当前列的值。
✅ 可以直接引用
可在约束条件中使用同一行的其他列(如 CHECK (end_date > start_date))。
能否使用跨表属性?✅ 可以间接使用(需通过子查询)。✅ 可以直接使用(通过子查询)。
如何使用跨表属性?1. 子查询必须独立于当前行
子查询不能引用当前表的其他列,只能依赖固定条件或其他表的数据。
示例
sql<br> CHECK (category_id IN (SELECT category_id FROM Categories))<br>
1. 子查询可结合当前行的属性
子查询可通过 NEW.column(如 MySQL)或直接引用当前行的列。
示例
sql<br> CHECK (salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = NEW.dept_id))<br>
何时触发 CHECK 检查?仅在该列的值被插入或更新时触发
- 若其他列更新,即使整行数据变化,该约束也不会触发。
- 示例:CHECK (price > 0) 仅在 price 列被修改时验证。
仅在整行数据被插入或更新时触发
- 无论哪一列变化,只要行数据发生修改,约束都会验证。
- 示例:CHECK (end_date > start_date) 在插入或更新任意列时均会验证。

Assertions

  • These are database-schema elements, like relations or views.(是一种数据库模式元素)

  • Defined by:

CREATE ASSERTION <name>

CHECK (<condition>);

  • Condition may refer to any relation or attribute in the database schema.(约束条件可以调用数据模式中的所有属性和关系)

Timing of Assertion Checks

  • In principle, we must check every assertion after every modification to any relation of the database.
  • A clever system can observe that only certain changes could cause a given assertion to be violated.

Example: No change to Beers can affect FewBar. Neither can an insertion to Drinkers.

Triggers: Motivation

  • Assertions are powerful, but the DBMS often can’t tell when they need to be checked.

  • Attribute- and tuple-based checks are checked at known times, but are not powerful.

  • Triggers let the user decide when to check for any condition.

断言虽然功能比较强大但是触发的时机不明确,基于属性和元组的检查虽然触发时机比较明确但是逻辑功能比较有限,而触发器就比较好的解决了这两者的缺陷,将优点结合了起来。

Event-Condition-Action Rules

Another name for “trigger” is ECA rule, or event-condition-action rule.

  • Event : typically a type of database modification, e.g., “insert on Sells. ”(一般来说是指数据的修改类型)

  • Condition : Any SQL boolean-valued expression.(SQL语句是触发器被触发的条件)

  • Action : Any SQL statements.(动作,也就是触发器被触发后需要执行的SQL语句)

Preliminary Example: A Trigger

Instead of using a foreign-key constraint and rejecting insertions into Sells(bar, beer, price) with unknown beers, a trigger can add that beer to Beers, with a NULL manufacturer.

Example: Trigger Definition

CREATE TRIGGER BeerTrig
AFTER INSERT ON Sells
REFERENCING NEW ROW AS NewTuple
FOR EACH ROW
WHEN (NewTuple.beer NOT IN
(SELECT name FROM Beers))
INSERT INTO Beers(name)
VALUES(NewTuple.beer);

Options: FOR EACH ROW

Triggers are either “row-level” or “statement-level. ”

  • Row level triggers : execute once for each modified tuple.(行级触发就是变化了几行数据就执行几次触发器)

  • Statement-level triggers : execute once for a SQL statement, regardless of how many tuples are modified.(语句级触发就是一行语句不管多少行数据发生变化,都只执行一次触发器)

Options: REFERENCING

  • INSERT statements imply a new tuple (for row-level) or new table (for statement-level). The “table” is the set of inserted tuples.

  • DELETE implies an old tuple or table.

  • UPDATE implies both.

Refer to these by [NEW OLD][TUPLE TABLE] AS <name>

维度行级触发器(FOR EACH ROW)语句级触发器(FOR EACH STATEMENT)
触发频率每插入 / 更新 / 删除 一行数据 触发一次每执行 一条 SQL 语句 触发一次(无论影响多少行)
数据范围单次触发处理 单行数据单次触发处理 全表数据(语句影响的所有行)
数据形态以 行(元组) 为单位以 临时表(多行集合) 为单位

Options: The Condition

  • Any boolean-valued condition.

  • Evaluated on the database as it would exist before or after the triggering event, depending on whether BEFORE or AFTER is used.

But always before the changes take effect.(不管是after还是before,所有数据库的状态都没有真正的发生变化)

Access the new/old tuple/table through the names in the REFERENCING clause 


http://www.hkcw.cn/article/tjJlYJkmRL.shtml

相关文章

免费且好用的PDF水印添加工具

软件介绍 今天要给大家推荐一款超实用的PDF添加水印工具&#xff0c;它能够满足用户给PDF文件添加水印的需求&#xff0c;而且完全免费。 这款PDF添加水印的软件有着简洁的界面&#xff0c;操作简便&#xff0c;无需安装&#xff0c;解压后即可使用。 在使用前&#xff0c;先…

设计模式——面向对象设计六大原则

摘要 本文详细介绍了设计模式中的六大基本原则&#xff0c;包括单一职责原则、开放封闭原则、里氏替换原则、接口隔离原则、依赖倒置原则和合成复用原则。每个原则都通过定义、理解、示例三个部分进行阐述&#xff0c;旨在帮助开发者提高代码的可维护性和灵活性。通过具体代码…

DO指数GPU版本

大指数下DO指数模型计算优化 DO指数模型概述 DO指数&#xff08;Duranton-Overman Index&#xff09;是由Duranton和Overman于2005年提出的产业空间集聚测度方法&#xff0c;它通过分析企业间的精确地理距离分布来识别产业集聚模式。与传统集聚指标相比&#xff0c;DO指数具有…

工业物联网中的事件驱动采样架构及优化

论文标题 Event-Based Sampling Architecture and Optimization for Industrial Internet of Things 工业物联网中的事件驱动采样架构及优化 作者信息 Tejas Thosani Process Control Systems, Micron Technology Inc., Manassas, USA tthosanimicron.com Andres Prado Esp…

Windows | 总误按Num Lock?修改注册表永久禁用Numlk键使小键盘一直输入数字

先说需修改注册表的位置与键值 路径&#xff1a;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout\ 二进制键&#xff1a;Scancode Map 键值&#xff1a; 00 00 00 00 00 00 00 00 01 00 00 00 00 00 45 00 00 00 00 00 00 00 00 00如下图&#xff1a; …

c#与halcon环境配置,导出算法库,使用halcon环境编程

目录 1. C#配置halcon运行环境 2.导出halcon算法库 3.使用方法 记录下C#配置halcon环境的方法&#xff0c;以及halcon导出库的使用方法。 1. C#配置halcon运行环境 VS版本&#xff1a; vs2019 halcon版本: 20.11 创建c#工程&#xff0c;点击“创建新项目”&#xff0c;…

tomcat yum安装

使用yum安装 yum install -y java-1.7.0-openjdk* tomcat* --disablerepoepel## java-1.7.0-openjdk* 注意&#xff1a;最终安装的是java-1.8.0版本## --disablerepoepel 禁用&#xff1a;EPEL源&#xff0c;防止版本冲突 java -version (2) 启停&#xff1a;Tomcat 7 s…

时间的基本概念与相关技术三

1.5 守时技术 所谓守时&#xff08;time keeping&#xff09;是指一个时频系统&#xff08;包括频标和分频钟&#xff09;对时间信号和时间信息的保持。频率标准&#xff08;简称频标&#xff09;的频率准确度、频率稳定度和守时系统的环境条件是决定守时能力的三个关键因素。…

云原生安全基石:Kubernetes 核心概念与安全实践指南

&#x1f525;「炎码工坊」技术弹药已装填&#xff01; 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、基础概念 1. Kubernetes 架构全景 Kubernetes&#xff08;简称 K8s&#xff09;采用主从架构&#xff0c;由控制平面&#xff08;Control Plane&…

【python】uv管理器

uv是一个速度极快的 Python 包和项目管理器&#xff0c;用 Rust 编写。 安装 安装uv之前&#xff0c;确保你的电脑不需要安装了python 在Windows下&#xff0c;可以使用官方的脚本直接安装 powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.…

2021 年 12 月大学英语四级考试真题(第 1 2 3 套)——解析版——篇章题

&#x1f3e0;个人主页&#xff1a;fo安方的博客✨ &#x1f482;个人简历&#xff1a;大家好&#xff0c;我是fo安方&#xff0c;目前中南大学MBA在读&#xff0c;也考取过HCIE Cloud Computing、CCIE Security、PMP、CISP、RHCE、CCNP RS、PEST 3等证书。&#x1f433; &…

【Linux】mmap文件内存映射

&#x1f4dd;前言&#xff1a; 这篇文章我们来讲讲Linux——mmap mmap介绍mmap接口介绍mmap使用示例 &#x1f3ac;个人简介&#xff1a;努力学习ing &#x1f4cb;个人专栏&#xff1a;Linux &#x1f380;CSDN主页 愚润求学 &#x1f304;其他专栏&#xff1a;C学习笔记&a…

深度学习驱动的超高清图修复技术——综述

Deep Learning-Driven Ultra-High-Definition Image Restoration: A Survey Liyan Wang, Weixiang Zhou, Cong Wang, Kin-Man Lam, Zhixun Su, Jinshan Pan Abstract Ultra-high-definition (UHD) image restoration​​ aims to specifically solve the problem of ​​quali…

【Docker系列】Docker 容器内安装`ps`命令

博客目录 一、为什么需要在 Docker 容器中安装ps命令二、不同 Linux 发行版的安装方法1. Alpine Linux 镜像的安装方法2. Debian/Ubuntu 镜像的安装方法3. CentOS/RHEL 镜像的安装方法 三、验证安装与基本使用四、永久解决方案&#xff1a;修改 Dockerfile1. Alpine 基础镜像的…

【KWDB 创作者计划】_再热垃圾发电汽轮机仿真与监控系统:KaiwuDB 批量插入10万条数据性能优化实践

再热垃圾发电汽轮机仿真与监控系统&#xff1a;KaiwuDB 批量插入10万条数据性能优化实践 我是一台N25-3.82/390型汽轮机&#xff0c;心脏在5500转/分的轰鸣中跳动。垃圾焚烧炉是我的胃&#xff0c;将人类遗弃的残渣转化为金色蒸汽&#xff0c;沿管道涌入我的胸腔。 清晨&#x…

对蚁群算法的理解和实例详解

目录 一、算法概述 二、实例详解 1&#xff09;问题分析 2&#xff09;初始化参数 2&#xff09;设置蚂蚁初始位置 3&#xff09;选择路径 4&#xff09;记录本次最佳路径 5&#xff09;更新信息素 6&#xff09;清空禁忌表 三、计算结果 四、总结 一、算法概述 一群…

【PowerPoint专栏】PowerPoint的保存选项

在PowerPoint的保存选项中有非常多的可用选项,保存的类型也非常多。 在PowerPoint中的工具选项中同样有一些相关的菜单操作帮助用户完成一些特殊操作。 在

直击2025粤港澳大湾区车展 科技引领未来车展

5月31日,第二十九届粤港澳大湾区车展在深圳国际会展中心(宝安)拉开帷幕。本届车展延续“面向科技、面向未来、面向市场”的主题,以“科技Alpha车展”为核心方向,探索汽车前沿科技。展会规模超过26万平方米,有超8万平方米的户外活动体验区及试驾专区。车展期间,全球近百家…

新王加冕!巴黎5比0国米首夺欧冠 年轻风暴席卷欧洲

北京时间6月1日凌晨,2024至2025赛季欧冠联赛决赛在德国慕尼黑安联球场进行。经过90分钟的激战,法甲巴黎圣日耳曼队以5比0大胜意甲国际米兰队,夺得队史首座“大耳朵杯”。此前的淘汰赛中,“大巴黎”先后淘汰了利物浦队、阿斯顿维拉队和阿森纳队三支英超劲旅。而国米则在半决…

定制一款国密浏览器(13):预置国密根证书到浏览器

由于国密算法没有得到国外的认可,所以 Chromium、Firefox 等浏览器均不支持国密算法。即使我们修改了 Chromium 的源码,增加了国密算法的支持,但还不能在浏览器中正常使用。因为这涉及到证书的信任问题,国密证书都是国内厂商签发的,国密根证书并没有集成到系统和浏览器中。…