💻
QMMMS的笔记
博客
  • QMMMS的笔记
  • agent
    • MCP的背景、原理和开发
    • Agent 历史与背景
    • Agentic Workflows
    • 环境检查与基础工具
    • Tool Call
    • 工具与运行时的值
    • temp
    • 处理 Tool Call error
    • trick
  • algorithm
    • 线性结构
    • 二叉树
    • 图
    • 查找
    • 排序
    • 动态规划
    • 优化方法
    • 数学
    • 迁移至Java
  • computer_composition
    • 系统总线
    • 存储器
    • 输入输出系统
    • 计算机的运算方法
    • 指令系统
    • 补充
  • computer_network
    • 引入
    • 应用层
    • 传输层
    • 网络层(数据平面)
    • 网络层(控制平面)
    • 链路层
    • 常见问答
    • 实验
  • database
    • SQL实战
    • 关系代数
    • 数据库设计
    • 规范化
    • 数据库基本概念
    • 查询原理
    • 数据库恢复技术
    • 并发控制
  • dev_tools
    • Git
    • Nginx
    • Spring
    • LangChain
    • PyTorch Cheat Sheet
    • MyBatis
    • MySQL Cheat Sheet
    • MySQL 补充
    • Redis
    • Docker
    • RocketMQ
    • Chrome
  • linux
    • Linux基础命令与使用
    • 文件与权限
    • 文件与目录操作
    • 权限属性高级
    • 命令与文件的查找
    • 文件压缩和打包
    • vim编辑器
    • shell变量
    • 命令补充
    • 数据流重定向
    • 管道命令
    • shell脚本
    • 用户管理
    • 用户间交流
    • 计划任务
    • 进程管理
    • 软件管理
    • 认识系统服务
    • 运维常用命令
    • 常用命令
  • llm
    • 大规模语言模型概述
    • 分布式训练概述
    • 有监督微调概述
    • 强化学习与LLM
    • LLM评估概述
    • 大模型应用
    • 理解大模型
    • 量化
    • 预训练
    • 上下文学习
  • machine_learning
    • 引入
    • 大致正确学习
    • 一致收敛
    • 偏差还是过拟合?
    • 可学习的充要条件
    • 非均匀可学习性
    • 计算复杂性
  • mathematics
    • 概率与统计基础
    • 线性代数基础
  • operating_system
    • 操作系统基本概念
    • 进程和线程
    • 同步,互斥与死锁
    • 内存管理
    • 文件系统
    • I/O系统
    • 保护与安全
    • 《现代操作系统》
  • statistical_learning
    • 统计学习引入
    • 线性回归
    • 分类
    • 重抽样方法
    • 线性模型选择与正则化
    • 非线性模型
    • 基于树的方法
    • 支持向量机
    • 无指导学习
    • 马尔科夫链和蒙托卡罗方法简明理解
    • R语言速查
  • deep_learning
    • basic_concepts
      • 逻辑回归与损失函数
      • 神经网络
      • 正则化、预处理、权重初始化
      • 优化算法
      • 机器学习策略
      • 复习:从计算机视觉的角度
      • 卷积神经网络
      • 深度卷积网络示例
      • 计算机视觉任务
      • 循环神经网络
      • 自然语言处理任务
      • 注意力
      • Transformers 家族
      • 显卡扫盲
      • 强化学习概述
    • semi-supervise
      • 半监督学习简介
      • Consistency Regularization
      • Proxy-label Methods
      • Holistic Methods
      • Generative Models
      • Graph-Based SSL
      • Self-Supervision for SSL
      • Other SSL methods
  • programming
    • cpp
      • STL
      • C++基础
      • 内存管理
      • 面向对象
    • java
      • 环境和介绍
      • 注释
      • String
      • 面向对象思想
      • Object
      • 包
      • 访问权限修饰符
      • 初始化块
      • 接口
      • 内部类
      • 注解
      • 枚举
      • 集合框架
      • List
      • Map
      • 泛型
      • 迭代
      • IO与流
      • 序列化
      • 异常
      • Lambda
      • Stream流
      • Socket
      • 缓冲
      • 命名规范
      • 拆箱装箱
      • 值传递
      • 深拷贝
      • 反射
      • JVM
      • 并发编程基础
    • python
      • 并发编程
      • 环境管理
  • software_engineering
    • basic_concepts
      • 系统分析与设计概述
      • 规划
      • 需求分析与原型设计
      • 项目管理
      • 建模
      • 数据库设计
      • 架构
      • 配置管理
      • 测试管理
      • 安全
      • 编码原则
      • 微服务
      • 补充内容
    • software_testing
      • CMMI基础
      • PPQA与SQA
      • 软件测试基础
      • 黑盒测试
      • 白盒测试
      • 集成测试
      • 系统测试
      • 测开面试补充
由 GitBook 提供支持
在本页
  • Select Operation
  • Project Operation
  • Positional Notation(位置标记)
  • Union Operation
  • Set-Difference Operation
  • Cartesian-product operation
  • Rename Operation
  • Set-Intersection Operation
  • Natural-Join Operation
  • Theta Join
  • Division
  • Assignment Operation
  • Outer Join
  • Generalized Projection
  • Aggregation functions
在GitHub上编辑
  1. database

关系代数

上一页SQL实战下一页数据库设计

最后更新于9个月前

关系代数(Relational Algebra)与SQL查询语言类似,不过多用于理论,SQL查询语言用于现实的数据库查询。

Three types of operations/operators on relations:

  • fundamental operations:基本运算

  • additive operations:由基本运算组合而成的附加运算

  • extended operations:扩展运算

Fundamental operations 部分

Select Operation

相当于SQL查询语言中的where

Select operation is defined as:

σp(r)={t∣t∈r∧p(t)}\Large \sigma_p(r)=\{t|t \in r \land p(t) \}σp​(r)={t∣t∈r∧p(t)}

ppp is called the selection predicate, connected by : ∧\land∧ (and), ∨\lor∨ (or), egegeg (not) , op is one of: ===, eee, >>>, <<<, ≤\le≤, ≥\ge≥

Project Operation

相当于SQL查询语言中的select

Notation:

例子1:

SELECT distinct ID, name, salary
FROM instructor;

例子2:

SELECT distinct name
FROM instructor
WHERE dept_name='Physics';

Positional Notation(位置标记)

Use $1, $2, . . . refer to the first attribute, the second attribute, and so on.

例子:

如果表结构为instructor(ID, name, dept_name, salary),那么:

SELECT distinct i1.salary
FROM instructor i1 JOIN instructor i2
WHERE i1.salary < i2.salary;

即选出所有不是最高工资的工资。

Union Operation

Union operation on relation r and s is defined as:

  • r, s must have the same arity (same number of attributes)

  • The attribute domains must be compatible(兼容的)

例子:

Based on “emp-dept” schema, find all employees working as 'CLERK' or having salary higher than 2000, or fulfill both conditions.

Set-Difference Operation

Set difference operation on relation r and s is defined as:

  • r, s must have the same arity

  • The attribute domains must be compatible

例子:

Based on “emp-dept” schema, find all employees working as 'CLERK' ,but not having salary higher than 2000.

Cartesian-product operation

Cartesian-product operation on relation r and s is defined as:

例子1:

Give the relational algebra expression for the operations shown:

例子2:

Based on “emp-dept” schema, find all employees’ name and their departments' name .

Rename Operation

returns the result of expression E under the name x:

If a relational-algebra expression E has arity n then:

Additive operations 部分

We define additional operations that do not add any power to the relational algebra, but that simplify common queries. An additional operation can be replaced/re-represented by basic operations.

Set-Intersection Operation

For relation r and s, set-intersection operation on r and s is defined as:

例子:

Based on “emp-dept” schema, find all employees working as 'CLERK' and having salary higher than 2000.

Natural-Join Operation

  • Natural join is associative(结合律)

  • Natural join is commutative (交换律)

Theta Join

The theta join operation is defined as:

Division

记不住公式没问题,看例子理解。

例子1:

Find all students who have taken all courses offered in the Biology department.

select distinct S.name
from student as S
where not exists (
    (
        select course_id
        from course
        where dept_name = 'Biology'
	)
    except
    (
        select T.course_id
        from takes as T
        where S.ID = T.ID
    ) 
);

例子2:

Based on “emp-dept” schema, find the jobs provided by all the departments having employees.

首先,确定目标的是job,条件是deptno

例子3:

找到提供了工资在3000到4000之间所有职位的部门,给出部门名称。

首先确定目标是deptno,条件是job

\Large \Pi_{dname}(dept \bowtie \Pi_{deptno,job}(emp)\div\Pi_{job}(\sigma_{sal\ge 3000 \and sal <4000}(emp)))

Assignment Operation

Outer Join

Outer join is an extension of the join operation that avoids loss of information.

left outer join operation is defined as:

Extended operations部分

Generalized Projection

Generalized project(广义投影) extends the projection operation by allowing arithmetic functions to be used in the projection list.

Aggregation functions

Aggregate operation(聚集运算) in relational algebra is defined as:

例子1:

Based on “emp-dept” schema,Find the average salary in each department

例子2:

获得工资比所在部门平均工资高的员工姓名、工资以及所在部门的平均工资。

ΠA1,A2,…,Ak(r)\Large \Pi_{A_1,A_2,\dots,A_k}(r)ΠA1​,A2​,…,Ak​​(r)

where AiA_iAi​ are attribute names and rrr is a relation name

ΠID,name,salary(instructor)\Large \Pi_{ID,name,salary}(instructor)ΠID,name,salary​(instructor)
Πname(σdept_name=′Physics′(instructor))\Large \Pi_{name}(\sigma_{dept\_name='Physics'}(instructor))Πname​(σdept_name=′Physics′​(instructor))
Π$4(σ$4<$8(instructor×instructor))\Large \Pi_{\$4} (\sigma_{\$4 < \$8} (instructor \times instructor ))Π$4​(σ$4<$8​(instructor×instructor))
r∪s={t∣t∈r∨t∈s}\Large r \cup s=\{t|t \in r \lor t \in s \}r∪s={t∣t∈r∨t∈s}
Πname(σjob=′CLERK′(emp))∪Πname(σsalary>2000(emp))\Large \Pi_{name}(\sigma_{job='CLERK'}(emp)) \cup \Pi_{name}(\sigma_{salary>2000}(emp))Πname​(σjob=′CLERK′​(emp))∪Πname​(σsalary>2000​(emp))
r−s={t∣t∈r∧t∉s}\Large r - s=\{t|t \in r \land t \notin s \}r−s={t∣t∈r∧t∈/s}
Πname(σjob=′CLERK′(emp))−Πname(σsalary>2000(emp))\Large \Pi_{name}(\sigma_{job='CLERK'}(emp)) - \Pi_{name}(\sigma_{salary>2000}(emp))Πname​(σjob=′CLERK′​(emp))−Πname​(σsalary>2000​(emp))
r×s={t∣t=t1t2∧t1∈r∧t2∈s}\Large r \times s=\{t|t=t_1t_2 \land t_1 \in r \land t_2 \in s \}r×s={t∣t=t1​t2​∧t1​∈r∧t2​∈s}
ΠA,B,C,D,E(σA=C(r×s))\Large \Pi_{A,B,C,D,E}(\sigma_{A=C}(r \times s))ΠA,B,C,D,E​(σA=C​(r×s))
Πemp.name,dept.name(σemp.deptno=dept.deptno(emp×dept))\Large \Pi_{emp.name, dept.name}(\sigma_{emp.deptno=dept.deptno}(emp \times dept))Πemp.name,dept.name​(σemp.deptno=dept.deptno​(emp×dept))
ρx(E)\Large \rho_{\text{x}}(E)ρx​(E)
ρx(A1,A2,…,An)(E)\Large \rho_{\text{x}(A_1,A_2,\dots,A_n)}(E)ρx(A1​,A2​,…,An​)​(E)
r∩s={t∣t∈r∧t∈s}=r−(r−s)\Large r \cap s=\{t|t \in r \land t \in s \}=r-(r-s)r∩s={t∣t∈r∧t∈s}=r−(r−s)
Πname(σjob=′CLERK′(emp))∩Πname(σsalary>2000(emp))\Large \Pi_{name}(\sigma_{job='CLERK'}(emp)) \cap \Pi_{name}(\sigma_{salary>2000}(emp))Πname​(σjob=′CLERK′​(emp))∩Πname​(σsalary>2000​(emp))

Let r and s be relations on schemas R and S respectively, and assuming R∩S=A1,A2,…,AnR\cap S = {A_1, A_2, \dots, A_n }R∩S=A1​,A2​,…,An​, natural-join operation is defined as:

r⋈s=σr.A1=s.A1∧⋯∧r.An=s.An(r×s)\Large r \bowtie s = \sigma_{r.A_1=s.A_1 \land \dots \land r.A_n=s.A_n}(r \times s)r⋈s=σr.A1​=s.A1​∧⋯∧r.An​=s.An​​(r×s)
(A⋈B)⋈C=A⋈(B⋈C)\Large (A \bowtie B) \bowtie C = A \bowtie (B \bowtie C)(A⋈B)⋈C=A⋈(B⋈C)
A⋈B=B⋈A\Large A \bowtie B = B \bowtie AA⋈B=B⋈A
r⋈θs=σθ(r×s)\Large r \bowtie_\theta s = \sigma_\theta(r \times s)r⋈θ​s=σθ​(r×s)

where θ\thetaθ is a predicate on attributes in R∪SR \cup SR∪S

R÷S=ΠR−S(r)−ΠR−S((ΠR−S(r)×S)−ΠR−S,S(r))\Large R \div S = \Pi_{R-S}(r) - \Pi_{R-S}((\Pi_{R-S}(r) \times S) - \Pi_{R-S,S}(r))R÷S=ΠR−S​(r)−ΠR−S​((ΠR−S​(r)×S)−ΠR−S,S​(r))
Πname,course_id(ρS(student)⋈S.ID=T.IDρT(takes))÷Πcourse_id(σdept_name=′Biology′(course))\Large \begin{align} &\Pi_{name,course\_id}(\rho_{S}(student) \bowtie_{S.ID=T.ID} \rho_{T}(takes)) \\ \div &\Pi_{course\_id}(\sigma_{dept\_name = 'Biology'}(course)) \end{align}÷​Πname,course_id​(ρS​(student)⋈S.ID=T.ID​ρT​(takes))Πcourse_id​(σdept_name=′Biology′​(course))​​
Πjob,deptno(emp)÷Πdeptno(dept)\Large \Pi_{job,deptno}(emp) \div \Pi_{deptno}(dept)Πjob,deptno​(emp)÷Πdeptno​(dept)

The assignment operation (←\leftarrow←) provides a convenient way to express complex queries. 相当于with ... as...

(r⋈s)∪(r−ΠR(r⋈s)×{(null,…,null)})\Large (r \bowtie s) \cup (r- \Pi_R(r \bowtie s) \times \{ (\text{null},\dots, \text{null})\})(r⋈s)∪(r−ΠR​(r⋈s)×{(null,…,null)})
ΠF1,F2,…,Fn(E)\Large \Pi_{F_1,F_2,\dots,F_n}(E)ΠF1​,F2​,…,Fn​​(E)

each of F1,F2,…,FnF_1,F_2,\dots,F_nF1​,F2​,…,Fn​ are arithmetic expressions involving constants and attributes in the schema of E

G1,G2,…,GnGF1(A1),F2(A2),…,Fn(An)(E)\Large _{G_1,G_2,\dots,G_n} \mathcal{G}_{F_1(A_1),F_2(A_2),\dots,F_n(A_n)}(E)G1​,G2​,…,Gn​​GF1​(A1​),F2​(A2​),…,Fn​(An​)​(E)

EEE is any relational-algebra expression

G1,G2,…,GnG_1,G_2,\dots,G_nG1​,G2​,…,Gn​is a list of attributes on which to group (can be empty)

Each FiF_iFi​ is an aggregate function

Each AiA_iAi​ is an attribute name

deptnoGavg(sal)(emp)\Large _{deptno} \mathcal{G}_{avg(sal)}(emp)deptno​Gavg(sal)​(emp)
Πename,sal,avgsal(σsal>avgsal(emp⋈(deptnoGavg(sal) as avgsal(emp))))\Large \Pi_{ename,sal,avgsal}(\sigma_{sal>avgsal}(emp \bowtie(_{deptno} \mathcal{G}_{avg(sal) \, as\, avgsal}(emp))))Πename,sal,avgsal​(σsal>avgsal​(emp⋈(deptno​Gavg(sal)asavgsal​(emp))))