数据库复习2. Relational Algebra 关系代数
Relational Algebra 关系代数
Relational Algebra is the language to describe operations in relational DBMS helps understanding of Query execution,and particularly Query optimisation.
关系代数是描述关系DBMS中操作的语言,有助于理解查询执行,特别是查询优化。
It is basic set of relational operations to retrieve, filter and restructure relations.
它是一组用于检索、筛选和重组关系的基本关系操作。
Result of operations is always a new relation *.
原表经关系代数操作后的结果是一个新表
给定下列几张表作为操作实例用表:
STUDENT
FName | LName | ReaNum | BDate | Address | Gender | DName |
---|---|---|---|---|---|---|
Bart | Simpson | 1111 | 1991-10-10 | 10 Evergreen Terrace, Springfield | M | Computer Science |
Lisa | Simpson | 2222 | 1993-06- 14 | 10 Evergreen Terrace, Springfield | F | Physics |
Milhouse | Van Houten | 3333 | 1991-09-07 | Mill House, Springfield | M | History |
Ralph | Wiggum | 4444 | 1993- 12-22 | Wiggum House, Springfield | M | History |
Todd | Flanders | 5555 | 1992-06-28 | 9 Evergreen Terrace, Springfield | M | Religious Studies |
Rod | Flanders | 6666 | 1994-02-27 | 9 Evergreen Terrace, Springfield | M | Religious Studies |
A pu | Naha… | 7777 | 1968-03-17 | Kwik E Mart, Springfield | M | Computer Science |
Monty | Burns | 8888 | 1898-11-11 | Nuclear Power Plant, Springfield | M | Physics |
Department
DName | HoD | NoO/Emps |
---|---|---|
Physics | Homer Simpson | 22 |
Computer Science | Edna Krabappel | 34 |
Religious Studies | Rev. Lovejoy | 12 |
History | Abe Simpson | 18 |
DEPT_LOCATIONS
DName | DLocation |
---|---|
Physics | Science Building |
Computer Sc ience | Science Building |
Computer Sc ience | Computer Centre |
Religious Studies | Main Building |
History | Main Building |
The SELECT operation 选择操作
Definition: Selects subset of tuples from relation on basis of a selection condition. 定义:根据选择条件从表中选出元组的子集的操作。
Denoted by
σ
(sigma) as the formσ<selection condition>(R)
: 用σ
符号表示成σ<selection condition>(R)
的形式:<Selection condition>
: 选择条件:Selection condition is formed by arbitrarily connecting clauses using boolean operators
AND
,OR
andNOT
选择条件由使用布尔运算符“AND”、“OR”和“NOT”的任意连接子句构成。
Selection condition is a boolean expression made up of a number of clauses of form:
选择条件可以是一个布尔表达式,以下列形式表示:
<attribute name> <comparison op> <constant value> or <attribute name> <comparison op> <attribute name><comparison op> `is normally one of { =,≠,<,>,≤ ,≥ }
(R)
: R is just name of a database relation R是被操作表的表名
Note:
Result of SELECT operation is always a relation (as with all other relational algebra operations).
SELECT操作后的结果依然是一张表(这一点和其他关系操作符一样)
Relation resulting from SELECT has same attributes as R
SELECT操作后得到的新表与原表R有相同的表头
Number of tuples in resulting relation always less than or equal to number of tuples in R
新表的行数小于等于原表
SELECT operation is commutative 交换律
σ<cond1>(σ<cond2>(R)) = σ<cond2>(σ<cond1>(R) )
A sequence of
SELECT
s can be applied in any order, or can be converted to a single SELECT operation asσ<condl> AND <cond2> (R)
多个
SELECT
语句可以由任意顺序应用,或者可以由布尔操作符连接成一个SELECT
语句,例如:σ<condl> AND <cond2> (R)
SELECT Examples: 下面来看SELECT语句的实例:
σ(LName =’Simpson’AND Gender = ’M’)OR(DName = ’Religious Studies’)(STUDENT)
FName | LName | ReaNum | BDate | Address | Gender | DName |
---|---|---|---|---|---|---|
Bart | Simpson | 1111 | 1991-10-10 | 10 Evergreen Terrace, Springfield | M | Computer Science |
Todd | Flanders | 5555 | 1992-06-28 | 9 Evergreen Terrace, Springfield | M | Religious Studies |
Rod | Flanders | 6666 | 1994-02-27 | 9 Evergreen Terrace, Springfield | M | Religious Studies |
The PROJECT operation. OPRATION操作
Definition: PROJECT selects some of columns on basis of an attribute list from attributes of a relation
R
PROJECT操作按照属性列表选取R中的对应列组成新表
Denoted by
π
( pi), it takes the formπ<attribute list>(R)
用
π
( pi)表示,形式为π<attribute list>(R)
- Where
R
is a relational algebra expression (whose result is a relation), its simplest caseR
is just name of database relation.R
是关系代数表达式,它的最简形式是被操作表的表名。
- Where
Note:
Result of PROJECT operation is always a relation (as with all other relational algebra operations)
PROJECT操作后的结果依然是一张表(这一点和其他关系操作符一样)
Relation resulting from PROJ ECT has only attributes specified in
<attribute list>
and Attributes in same order as in list.PROJECT操作后的新表只含有
<attribute list>
中的属性,且属性排列顺序和<attribute list>
一致。PROJECT operation is not commutative. PROJECT操作不具有交换律。
π<listl> ( π<list2>(R) ) =π<listl>(R)
,provided<list 2>
contains all attributes in<list 1>
otherwise left hand side is an invalid expression.提供的<list 2>
要包含<list 1>
中的所有属性,否则π<listl>
是无效表达式。Number of tuples in resulting relation is always less than or equal to number of tuples in R
新表的行数小于等于原表
Remember: result is a relation so no duplicates.PROJECT的结果没有重复值。
Number of tuples in resulting relation is always less than or equal to number of tuples in R
新表的行数小于等于原表
The PROJECT operation举例:
The RENAME Operation 改名操作
- 将经过SELECT操作的STUDENT的结果表命名为DEPT_CS.
- 将经过PROJECT操作的DEPT_CS的结果表中的{Fname,Lname}属性命名为RESULT(FirstName, LastName).
UNION, INTERSECTION, DIFFERENCE Operatiion 并,交,补操作
Application Condition应用条件:
Two relations must have same type of tuples. 两张表必须行数相同。
Formally, two relations
R(A1,A2, ...,An )
andS( B1, B2, ...,Bn)
are union compatible if They have same degree n and
dom( Ai) = dom( Bi)
. R和S可兼容前提是他们的列数相同且每个属性的定义域相同。
UNION 并
- Denoted by
R∪S
- Results in relation that includes all tuples that are either in
R
, or inS
, or in bothR
andS
(Duplicate tuples are eliminated)
- Denoted by
INTERSECTION 交
Denoted by
R∩S
Results in a relation that includes all tuples that are in both
R
andS
DIFFERENCE 补
Denoted by
R - S
一 Results in a relation that includes all tuples that are in
R
but not inS
UNION and INTERSECTION are commutative but DIFFERE NCE is not.并,交操作具有交换律但是补操作没有。
CARTESIAN PRODUCT Operation 笛卡尔积操作
Also a standard binary set operation. 二进制集合操作
Denoted by
×
用×
表示Also known as CROSS PRODUCT or CROSS JOIN operation. 也被称为叉乘或交叉联合操作
Relations on which it is applied do not have to be union compatible. 叉乘的两张表不需要满足联合兼容(属性数同,属性定义域同)
Result of
R( A1, A2,..., An )×S( B1, B2,,,,Bm)
is relationQ
withn + m
attributes in that order i.e.Q( A1, A2, ..., An, S1, S2, ..., Sm)
,Q
has one tuple for each combination of tuples: one from R and one from S- Hence, if
R
hasn
tuples andS
hasm
tuples thenR×S
will haven*m
tuples.笛卡尔积
R( A1, A2,..., An )×S( B1, B2,,,,Bm)
的结果表中有n+m
列和n*m
行
The JOIN Operation
Used to combine related tuples from two relations into single tuples. 被用来将两张表中相关的元组结合成一张表。
Denoted by
⋈
.用⋈表示。Result of the JOIN is a relation
Q
withn + m
attributes in that order. i.e.Q(A1,A2, ...,An, B1, B2, ..., Bm)
JOIN的结果表
Q
有n+m
个属性且按照Q(A1,A2, ...,An, B1, B2, ..., Bm)
的顺序排列。However, unlike CARTESIAN PRODUCT resulting relation Q in a JOIN has one tuple for each combination of tuples whenever combination satisfies join condition.
JOIN的本质是先对A、B两张表做笛卡尔积,然后再按照
join condition
选取合适的行。举例:选取
FEM_STUDS
和DEPARTMENT
表中StudDept
和DName
相等的元组并将新表命名为FEM_STUD_HODS
.
- 先将
FEM_STUDS
和DEPARTMENT
做笛卡尔积,得到:
- 再按照JOIN条件中
StudDept=DName
选出结果:
NATURAL JOIN
NATURAL JOIN was created to get rid of the additional redundant attribute in EQUIJOIN.
NATURAL JOIN在EQUIJOIN的基础上去掉了多余的属性。
- Denoted by
*
用*表示
AGGREGATE FUNCTION Operation 聚合操作
函数名称 | 描述 |
---|---|
COUNT() | 计数 |
SUM() | 求和 |
AVG() | 求平均值 |
MAX() | 最大值 |
MIN() | 平均值 |
… | … |
举例:
OUTER JOIN Operation
Sometimes we might wa nt to keep all tuples in R or S or both in results of JOIN regardless of whether they satisfy join condition or not.
有时,我们可能希望在JOIN的结果中保持所有元组在R或S中,或两者都在,而不管它们是否满足连接条件。
There are 3 types of OUTER JOIN operations.
LEFT OUTER JOIN 左外连接
RIGHT OUTER JOIN 右外连接
FULL OUTER JOIN 全连接
若右表没有对应的值,则将其取值为NULL
若左表没有对应的值,则将其取值为NULL
保留左右表所有的元组,无法对应取值为NULL
数据库复习2. Relational Algebra 关系代数相关推荐
- 数据库复习(数据管理系统)
数据库复习 面向老师的话 重点:关系数据库,数据库是什么,跟文件系统相比的优势,面向数据库比的劣势,跟noSQL数据库比的劣势: 数据库:长期存储在计算机内.有组织的.可共享的.大量的数据集合 比较: ...
- 数据库原理(一)—— 关系代数
关系代数:Relational Algebra. 什么叫代数? 代数是一个较为基础的数学分支.它的研究对象有许多.诸如数.数量.代数式.关系.方程理论.代数结构等等都是代数学的研究对象. 初等代数一般 ...
- 数据库复习1——数据库体系结构和关系系统
数据库复习 CH2 数据库体系结构 2.1 数据库体系结构层次 数据库体系结构能够分成3个层次: 外部层次 概念层次 内部层次 外部层次是指每一个用户用不同的高级语言来訪问数据库所面对的接口.概念层次 ...
- sql创建计算机用户,2015年计算机四级数据库复习要点:SQL Server 登录账户
2015年计算机四级数据库复习要点:SQL Server 登录账户 系统内置的登录账户 1. BUILTIN\Administrators:是一个Windows组账户,表示所有的Windows Adm ...
- 数据库复习 - PART2 - 建模设计与范式
数据库复习 - PART2 - 建模设计与范式 数据库复习 - PART2 - 建模设计与范式 4. 数据建模 4.1 概述 4.2 E-R模型 4.2.1 基本组成 4.2.2 联系专题 4.2.3 ...
- 数据库原理(八)- 关系代数
数据库原理(八)- 关系代数 前言 传统的集合运算 并(union) 差(except) 交(intersection) 笛卡尔积(cartesian product) 专门的集合运算 选择(sele ...
- 数据库复习10——PL/SQL
数据库复习 CH10 PL/SQL 10.1 PL/SQL简介 PL/SQL是Oracle对SQL的过程化的扩展,PL/SQL可以实现SQL相关的过程化程序,并且能够以存储过程和函数的方式让一段SQL ...
- 数据库关系表 ---- Relational table
数据库关系表 ---- Relational table 什么是关系(relation) 关系(relation)的基本属性 约束(Constraints) Integrity Constraints ...
- 基于sqlserver的数据库复习
20220711 -20220724 数据库复习 + B站视频(基于sqlserver) 边看视频边随手记录的,可读性可能不太高,自用吧. 一. 1.为何要连接到数据库:数据库软件不是数据库,我们只是 ...
最新文章
- Javascript及Jquery获取元素节点以及添加和删除操作
- 2021-01-07 matlab数值分析 数值积分与数值微分 复合梯形公式 复合Simpson公式
- 【C++】18.char[] 与 string 的区别 与 互相转化、c_str() 函数用法
- Java 判断密码是否是大小写字母、数字、特殊字符中的至少三种
- 聊聊云计算:为什么构建网站时常会用到负载均衡
- linux 多进程 同步,Linux内核同步,进程,线程同步
- 天池实验室-Task03-Python入门(下)
- Diagrams for Mac(原生流程图制作工具)
- 大话设计模式笔记(十二)の抽象工厂模式
- 软考初级程序员知识点整理之 网络协议与标准
- Unity PID 控制算法可视化学习
- ADS1115 STM32 HAL库驱动 CubeMX
- Intellij中格式化mapper.xml整齐
- 微积分手机版与高清电子投影仪是“绝配”
- 请收下这份秘籍: 这里有关于申请 gTech 职位所需知道的一切
- c语言专业认知实践报告怎么写,C语言课程设计实习报告范本
- 排序算法分析归纳总结
- ieframe.dll修复方法
- 小程序源码:云之道知识付费独立线传版V2-2.4.9-多玩法安装简单
- 实验一 Python安装与开发环境搭建
热门文章
- 说说超链接target属性的取值和作用?
- QImage::scaled: Image is a null image--PySIde2和PyQt5无法显示jpg等图像
- 公交/地铁出行的场景,需要考虑哪些测试点
- 机器人钩阿木木_英雄联盟:机器人不敢钩的四大英雄,只要你敢钩我就让你们团灭!...
- np.apply_along_axis函数用法
- 轻松掌握averageif(s)、countif(s)、sumif(s)等逻辑计算函数用法
- LabVIEW版本与所支持的DAQmx版本
- C语言 计算两个数的平方和
- python内置函数调整_python - 内置函数
- MD5验证文件完整性 C++