SQL Relational Algebra(数据库关系代数)

news2025/6/6 16:22:34

目录

What is an “Algebra”

What is Relational Algebra?

Core Relational Algebra

Selection

Projection

Extended Projection

Product(笛卡尔积)

Theta-Join

Natural Join

Renaming

Building Complex Expressions

Sequences of Assignments

Expressions in a Single Assignment

Expression Trees

Example: Tree for a Query

Example: Self-Join

Operations on Bags


What is an “Algebra”

Mathematical system consisting of:

  • Operands --- variables or values from which new values can be constructed.(操作数,用于构建新值的变量或者值)

  • Operators --- symbols denoting procedures that construct new values from given values.(运算符,标志着从给定值创建新值的过程)

What is Relational Algebra?

  • An algebra whose operands are relations or variables that represent relations.(关系代数是操作数是关系或者是表示关系的变量)

  • Operators are designed to do the most common things that we need to do with relations in a database.

  • The result is an algebra that can be used as a query language for relations.(关系代数语言将会是数据库语言的基础)

Core Relational Algebra

  • Union, intersection, and difference.(并、交、差)

Usual set operations, but both operands must have the same relation schema.(两个操作数之间必须要有一样的关系模式)

  • Selection: picking certain rows.(也就是SQL中的WHERE)

  • Projection: picking certain columns.

  • Products and joins: compositions of relations.(笛卡尔积和连接:笛卡尔积是全组合、连接是条件组合)

  • Renaming of relations and attributes.

Selection

R_{1}:=\sigma _{C}(R_{2})
  • C is a condition (as in “if” statements) that refers to attributes of R2.

  • R1 is all those tuples of R2 that satisfy C.

这两个关系之间没有任何关系

Projection

R_{1}:=\pi _{L}(R_{2})
  • L is a list of attributes from the schema of R2.(L是R2关系模式中的一串属性)

  • R1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1.(查看R2的属性列表,然后提取出L属性列表中的属性,然后按照特定顺序创建R1的元组)

  • Eliminate duplicate tuples, if any.(消除重复项)

Extended Projection

  • Using the same \pi _{L} operator, we allow the list L to contain arbitrary expressions(任意表达式) involving attributes:

  • Arithmetic on attributes, e.g., A+B->C.

  • Duplicate occurrences of the same attribute.

Product(笛卡尔积)

R_{3}:=R_{1}\times R_{2}

  • Pair each tuple t1 of R1 with each tuple t2 of R2.

  • Concatenation t1t2 is a tuple of R3.

  • Schema of R3 is the attributes of R1 and then R2, in order.

But beware attribute A of the same name in R1 and R2: use R1.A and R2.A.(如果R1、R2中有相同的属性使用R1.A和R2.A来进行区分)

Theta-Join

R_{3}:=R_{1}\bowtie _{C}R_{2}
  • Take the product R1 Χ R2.

  • Then apply \bowtie _{C} to the result.

  • As for σ, C can be any boolean-valued condition.(对于C来说,可以是任何布尔值的表达式)

Historic versions of this operator allowed only A \theta B, where \theta is = , <, etc.; hence the name “theta-join.”

Natural Join

  • A useful join variant (natural join) connects two relations by:
  • Equating(等值比较) attributes of the same name, and Projecting out one copy of each pair of equated attributes.(将等值属性的一组副本投影掉)
  • Denoted R3 := R1 R2.

Renaming

  • The ρ operator gives a new schema to a relation.
  • R_{1}:=\rho _{R1(A_{1}A_{2}...A_{n})}(R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2.
  • Simplified notation: R1(A_{1}A_{2}...A_{n}):=R2

Building Complex Expressions

Combine operators with parentheses and precedence rules.(通过括号或者优先运算规则对操作符进行组合)

Three notations, just as in arithmetic:

  • Sequences of assignment statements.

  • Expressions with several operators.

  • Expression trees.

Sequences of Assignments

  • Create temporary relation names.
  • Renaming can be implied by giving relations a list of attributes.
  • Example:
R3 := R1 C R2
can be written:
R4 := R1 Χ R2
R3 := σ C (R4)

Expressions in a Single Assignment

  • Example:

the theta-join R3 := R1 C R2

can be written:

R3 := σC (R1 Χ R2)

  • Precedence of relational operators:

  1. [σ, π, ρ] (highest).

  2. [Χ, ].

  3. .

  4. [, ]

Expression Trees

  • Leaves are operands --- either variables standing for relations or particular constant relations.(叶子结点是操作数,可以是标识关系的变量也可以是常量)

  • Interior nodes are operators, applied to their child or children.(内部结点是操作符,作用于子结点)

Example: Tree for a Query

Using the relations Bars(name, addr) and Sells(bar, beer, price), find the names of all the bars that are either on Maple St. or sell Bud for less than $3.

Example: Self-Join

Using Sells(bar, beer, price) , find the bars that sell two different beers at the same price.
Strategy :
  • by renaming, define a copy of Sells, called S(bar, beer1, price).
  • The natural join of Sells and S consists of quadruples (bar, beer, beer1, price)
  • such that the bar sells both beers at this price.

先通过重命名得到一个Sells的副本,然后将原表与副本进行自连接,自连接的条件是price相同,然后进行选择,最后投影出name

Operations on Bags

  • Selection applies to each tuple, so its effect on bags is like its effect on sets.

  • Projection also applies to each tuple, but as a bag operator, we do not eliminate duplicates.

  • Products and joins are done on each pair of tuples, so duplicates in bags have no effect on how we operate.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2399081.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

智能工业时代:工业场景下的 AI 大模型体系架构与应用探索

自工业革命以来&#xff0c;工业生产先后经历了机械化、电气化、自动化、信息化的演进&#xff0c;正从数字化向智能化迈进&#xff0c;人工智能技术是新一轮科技革命和产业变革的重要驱动力量&#xff0c;AI 大模型以其强大的学习计算能力掀开了人工智能通用化的序幕&#xff…

易语言使用OCR

易语言使用OCR 用易语言写个脚本&#xff0c;需要用到OCR&#xff0c;因此我自己封装了一个OCR到DLL。 http://lkinfer.1it.top/ 视频演示&#xff1a;https://www.bilibili.com/video/BV1Zg7az2Eq3/ 支持易语言、c、c#使用&#xff0c;平台限制&#xff1a;window 10 介绍…

C++和C#界面开发方式的全面对比

文章目录 C界面开发方式1. **MFC&#xff08;Microsoft Foundation Classes&#xff09;**2. **Qt**3. **WTL&#xff08;Windows Template Library&#xff09;**4. **wxWidgets**5. **DirectUI** C#界面开发方式1. **WPF&#xff08;Windows Presentation Foundation&#xf…

算法-集合的使用

1、set常用操作 set<int> q; //以int型为例 默认按键值升序 set<int,greater<int>> p; //降序排列 int x; q.insert(x); //将x插入q中 q.erase(x); //删除q中的x元素,返回0或1,0表示set中不存在x q.clear(); //清空q q.empty(); //判断q是否为空&a…

性能优化 - 理论篇:CPU、内存、I/O诊断手段

文章目录 Pre引言1. CPU 性能瓶颈1.1 top 命令 —— 多维度 CPU 使用率指标1.2 负载&#xff08;load&#xff09;——任务排队情况1.3 vmstat 命令 —— CPU 繁忙与等待 2. 内存性能瓶颈2.1 操作系统层面的内存分布2.2 top 命令 —— VIRT / RES / SHR 三个关键列2.3 CPU 缓存…

算法:二分查找

1.二分查找 704. 二分查找 - 力扣&#xff08;LeetCode&#xff09; 二分查找算法要确定“二段性”&#xff0c;时间复杂度为O(lonN)。为了防止数据溢出&#xff0c;所以求mid时要用防溢出的方式。 class Solution { public:int search(vector<int>& nums, int tar…

Ubuntu 22.04 上安装 PostgreSQL(使用官方 APT 源)

Ubuntu 22.04 上安装 PostgreSQL&#xff08;使用官方 APT 源&#xff09; 步骤 1&#xff1a;更新系统 sudo apt update sudo apt upgrade -y步骤 2&#xff1a;添加 PostgreSQL 官方仓库 # 安装仓库管理工具 sudo apt install wget ca-certificates gnupg lsb-release -y#…

Linux随记(十八)

一、k8s的node节点磁盘 /data已使用率超过 85% , 出现disk pressure &#xff0c;驱逐pod现象 evicted &#xff0c; the node had condition:[DiskPressure] #修改/var/lib/kubelet/config.yaml ]# cat /var/lib/kubelet/config.yaml apiVersion: kubelet.config.k8s.io/v1…

Windows MongoDB C++驱动安装

MongoDB驱动下载 MongoDB 官网MongoDB C驱动程序入门MongoDB C驱动程序入门 安装环境 安装CMAKE安装Visual Studio 编译MongoDB C驱动 C驱动依赖C驱动&#xff0c;需要先编译C驱动 下载MongoDB C驱动源码 打开CMAKE(cmake-gui) 选择源码及输出路径,然后点击configure …

MS1023/MS1224——10MHz 到 80MHz、10:1 LVDS 并串转换器(串化器)/串并转换器(解串器)

产品简述 MS1023 串化器和 MS1224 解串器是一对 10bit 并串 / 串并转 换芯片&#xff0c;用于在 LVDS 差分底板上传输和接收 10MHz 至 80MHz 的并行字速率的串行数据。起始 / 停止位加载后&#xff0c;转换为负载编 码输出&#xff0c;串行数据速率介于 120Mbps…

线性调频波形测距测速信号处理——全代码+注释

clear all close all clc %% 参数设置 fs600e6;%采样率 fc10.45e9;% 波形发射载频 t10e-6;%脉宽 f050e6;%波形中频频率 B10e6;%带宽 uB/(2*t);%调频斜率 Tv100e-6;% 脉冲重复周期 Num64;% 测速脉冲数 lamdfs/B;% 抽取带宽 Nsround(fs*t); NTvround(fs*Tv); tt0:1/fs:t-1/fs; ff…

WPS word 已有多级列表序号

wps的word中&#xff0c;原来已生成的文档里&#xff0c;已存在序号。比如&#xff0c;存在2、2.1、2.1.1、2.1.1.1、2.1.1.1.1 5层序号&#xff0c;而且已分为5级。但增加内容的时候&#xff0c;并不会自动增加序号&#xff0c;应该如何解决&#xff1f; 原来长这样&#xff…

【科研绘图系列】R语言绘制论文组合图形(multiple plots)

禁止商业或二改转载,仅供自学使用,侵权必究,如需截取部分内容请后台联系作者! 文章目录 介绍加载R包数据下载导入数据数据预处理画图1画图2画图3画图4画图5系统信息介绍 这篇文章详细介绍了如何使用R语言进行科研绘图,特别是绘制论文组合图形(multiple plots)。文章从数…

springMVC-9数据格式化

数据格式化 学习目标&#xff1a; 理解在我们提交数据(比如表单时)&#xff0c;SpringMVC怎样对提交的数据进行转换和处理的 Spring MVC 上下文中内建了很多转换器&#xff0c;可完成大多数 Java 类型的转换工作。 基本数据类型可以和字符串之间自动完成转换 应用实例-页面…

ISBN书号查询接口如何用PHP实现调用?

一、什么是ISBN书号查询接口 ISBN数据查询接口是一项图书信息查询服务。它基于全球通用的ISBN编码系统&#xff0c;帮助用户快速获取图书的详细信息&#xff0c;包括书名、作者、出版社、出版时间、价格、封面等关键字段。 该接口广泛应用于电商平台、图书馆管理系统、二手书…

Aop + 注解实现数据字典类型转换 EasyExcel导出

Aop 注解 实现数据字典类型转换 文章目录 Aop 注解 实现数据字典类型转换一、基础方式✅字典转换简介&#x1f449;实现步骤✅ 1. 定义自定义注解Dict ✅ 2. 定义查询字典项的两个方法✅ 3. 定义Aop拦截我们查询的方法✅ 4. VO映射类✅ 5. Controller层✅ 6. serviceImpl✅ 7. …

Xilinx超过256m bit flash固件跳转失败问题

问题描述 按照 链接: Xilinx 7系列fpga在线升级和跳转 这个方式跳转失败 问题排查 进一步排查现象如下 上面这个现象呈现出明显的以16m为周期的规律。感觉很大概率是因为flash超过了16m&#xff08;256bit&#xff09;导致的地址越界问题。另外我在CSDN上也找到类似的问题…

SpringCloud 分布式锁Redisson锁的重入性与看门狗机制 高并发 可重入

可重入 Redisson 的锁支持 可重入性&#xff0c;这意味着同一个线程在获取锁后&#xff0c;如果再次尝试获取该锁&#xff0c;它可以成功地获得锁&#xff0c;而不会被阻塞。 每次一个线程成功获取锁后&#xff0c;它的持有次数会增加。当线程再次获取该锁时&#xff0c;Redi…

02 APP 自动化-Appium 运行原理详解

环境搭建见 01 APP 自动化-环境搭建 文章目录 一、Appium及Appium自动化测试原理二、Appium 自动化配置项三、常见 ADB 命令四、第一个 app 自动化脚本 一、Appium及Appium自动化测试原理 Appium 跨平台、开源的 app 自动化测试框架&#xff0c;用来测试 app 应用程序&#x…

由docker引入架构简单展开说说技术栈学习之路

想象一下&#xff0c;你开了一家线上小卖部&#xff08;单机版&#xff09;&#xff0c;突然爆单了怎么办&#xff1f;别急&#xff0c;技术架构的升级打怪之路&#xff0c;可比哆啦A梦的口袋还神奇&#xff01; 第1关&#xff1a;单枪匹马的创业初期&#xff08;单机架构&…