天天看点

关系代数运算_关系代数

关系代数运算

When working with the relational model, we have 2 groups of operations we can use.

使用关系模型时 ,我们可以使用2组操作。

The first is called relational algebra, and it’s a procedural language.

第一种称为关系代数 ,它是一种过程语言 。

This is what SQL is based upon, and as such it is very important to learn - as SQL is the de-facto standard for working with relational databases.

这是SQL的基础,因此学习非常重要-因为SQL是使用关系数据库的实际标准。

The second is called relational calculus and instead of being procedural, it’s a declarative language. It’s a fundamental difference in how we interact with databases, because you don’t tell the database software what to do, you just tell it what you want, and let it sort out the details of how to do it.

第二种称为关系演算 ,而不是程序性的,它是一种声明性语言 。 这是我们与数据库交互的方式的根本区别,因为您没有告诉数据库软件该怎么做 ,而只是告诉您想要什么 ,然后让它整理出如何做的细节。

This is a common distinction among programming languages. In modern frontend, we say interaction with the DOM in React is declarative. Using vanilla JavaScript to modify the DOM is procedural.

这是编程语言之间的常见区别。 在现代的前端中,我们说与React中的DOM交互是声明性的。 使用原始JavaScript修改DOM是过程性的。

Languages like Datalog, QBE and QUEL have relational calculus as its base. I’m not going to talk about this because I think it’s a much more niche way of doing things compared to the more practical approach followed by SQL, but you can look at it if you want.

诸如Datalog , QBE和QUEL之类的语言都以关系演算为基础。 我不打算讨论这个问题,因为与SQL所采用的更实际的方法相比,这是一种更利基的处理方式,但是您可以根据需要进行查看。

Given this introduction, let’s go on with relational algebra.

有了这个介绍,让我们继续关系代数 。

We have 2 types of operations:

我们有2种操作类型:

  • primary operations

    主要操作

  • join operations

    加盟行动

关系代数中的主要运算 (Primary operations in relational algebra)

Primary operations are:

主要操作是:

  • union to get data from two tables, generating a sum of the tuples, as long as the two tables have the same columns and attribute types (domain).

    只要两个表具有相同的列和属性类型(域),就可以通过并集从两个表中获取数据,并生成元组的和。

  • difference to get data contained in the first table but not in the second table, generating a difference of the tuples, as long as the two tables have the same columns and attribute types (domain).

    差异以获取包含在第一个表中但不包含在第二个表中的数据,从而产生元组的差异,只要两个表具有相同的列和属性类型(域)即可。

  • cartesian product to get data from two tables into and generate one single table that combines the data of them, based on an attribute value.

    笛卡尔乘积可将两个表中的数据获取并生成一个基于属性值将它们的数据组合在一起的单个表。

  • select to only extract some of the tuples (rows) contained in a table based on certain criteria.

    选择仅基于某些条件提取表中包含的一些元组(行)。

  • project to generate a new table containing only one or more attributes (columns) of an existing table

    项目以生成仅包含现有表的一个或多个属性(列)的新表

  • rename used to rename an attribute, used to prevent conflicts when multiple tables have the same name for different data

    重命名用于重命名属性,用于防止多个表对不同数据使用相同名称时发生冲突

关系代数中的联接运算 (Join operations in relational algebra)

Joins are probably the most powerful operations you can perform with relational algebra. They build on top of primary operations, and they allow you to correlate data contained in different relations (tables).

连接可能是关系代数可以执行的最强大的运算。 它们建立在主要操作之上,并且使您可以关联包含在不同关系(表)中的数据。

Note: I’ll soon talk about joins in practice in a DBMS, this is mostly theory.

注意:我将很快讨论DBMS在实践中的联接,这主要是理论上的。

We have 2 main join versions: natural join and theta join. All the other versions are extracted from those 2.

我们有2个主要的join版本: 自然联接和theta联接 。 所有其他版本均摘自那些2。

自然加入 (Natural Join)

Natural join correlates two relations (tables), and creates a new table based on the same values of an attribute.

自然联接将两个关系(表)相关联,并基于属性的相同值创建一个新表。

We need two relations with the same attribute name (column), first. Then if values in the attributes in relation A are unmatched in the attributes in relation B, the row is not part of the result, it’s ignored.

首先,我们需要两个具有相同属性名称(列)的关系。 然后,如果关系A中的属性值与关系B中的属性不匹配,则该行不是结果的一部分,将忽略该行。

Example:

例:

Relation A

关系A

Employee ID Name
1 Mark
2 Tony
3 Rick
员工ID 名称
1个 标记
2 托尼
3 里克

Relation B

关系B

Manager Name Employee ID
Todd 1
Albert 2
经理姓名 员工ID
托德 1个
阿尔伯特 2

We can perform a natural join to get the boss name for each employee:

我们可以进行自然加入以获得每个员工的老板姓名:

Employee ID Name Manager Name
1 Mark Todd
2 Tony Albert
员工ID 名称 经理姓名
1个 标记 托德
2 托尼 阿尔伯特

Since the relations have the Employee ID attribute name in common, it is only present once in the result, not 2 times.

由于这些关系具有共同的Employee ID属性名称,因此在结果中仅出现一次,而不是2次。

The employee #3 present in relation A, Rick, is not included in this table, because there’s no corresponding entry in relation B.

由于在关系B中没有相应的条目,因此关系A中存在的#3雇员Rick不包括在此表中。

θ联接 (Theta-join)

A theta-join allows to perform a join based on any criteria to compare two columns in two different relations, not just equality like the natural join does.

Theta-join允许根据任何条件执行连接以比较两个不同关系中的两个列,而不仅仅是自然连接那样的相等性。

It performs a cartesian product of two tables, and filters the results based on the selection we want to make.

它执行两个表的笛卡尔积,并根据我们要选择的内容过滤结果。

等值联接 (Equi-join)

The equi-join is a theta join, where the selection is based on equality between attribute values in the two different tables.

等值联接是theta联接,其中选择基于两个不同表中属性值之间的相等性。

The difference with the natural join is that we can choose which attributes names (columns) we want to compare.

自然连接的区别在于我们可以选择要比较的属性名称(列)。

We’ll talk much more about joins later when SQL is introduced, so we can use them in practice.

稍后在引入SQL时,我们将详细讨论联接,因此我们可以在实践中使用它们。

翻译自: https://flaviocopes.com/relational-algebra/

关系代数运算