数据库中关系代数是什么_什么是关系代数?

数据库中关系代数是什么

什么是关系代数? (What is Relational Algebra?)

Every database management system must define a query language to allow users to access the data stored in the database. Relational Algebra is a procedural query language used to query the database tables to access data in different ways.

每个数据库管理系统都必须定义一种查询语言,以允许用户访问存储在数据库中的数据。 关系代数是一种过程查询语言,用于查询数据库表以不同方式访问数据。

In relational algebra, input is a relation(table from which data has to be accessed) and output is also a relation(a temporary table holding the data asked for by the user).

在关系代数中,输入是一个关系(必须从中访问数据的表),而输出也是一个关系(一个保存用户要求的数据的临时表)。

数据库中关系代数是什么_什么是关系代数?



Relational Algebra works on the whole table at once, so we do not have to use loops etc to iterate over all the rows(tuples) of data one by one. All we have to do is specify the table name from which we need the data, and in a single line of command, relational algebra will traverse the entire given table to fetch data for you.

关系代数可一次在整个表上工作,因此我们不必使用循环等来遍历数据的所有行(元组)。 我们要做的就是指定要从中获取数据的表名,并且在单行命令中,关系代数将遍历整个给定表以为您获取数据。

The primary operations that we can perform using relational algebra are:

我们可以使用关系代数执行的主要运算是:

  1. Select

    选择
  2. Project

    项目
  3. Union

    联盟
  4. Set Different

    设置不同
  5. Cartesian product

    笛卡尔积
  6. Rename

    改名

选择运算(σ) (Select Operation (σ))

This is used to fetch rows(tuples) from table(relation) which satisfies a given condition.

这用于从满足给定条件的表(关系)中获取行(元组)。

Syntax: σp(r)

语法: σ p (r)

Where, σ represents the Select Predicate, r is the name of relation(table name in which you want to look for data), and p is the prepositional logic, where we specify the conditions that must be satisfied by the data. In prepositional logic, one can use unary and binary operators like =, <, > etc, to specify the conditions.

其中, σ表示选择谓词, r是关系的名称(要在其中查找数据的表名), p是介词逻辑,我们在其中指定数据必须满足的条件。 在介词逻辑中,可以使用一元二进制运算符(例如=<>等)来指定条件。

Let's take an example of the Student table we specified above in the Introduction of relational algebra, and fetch data for students with age more than 17.

让我们以上面在关系代数简介中指定的Student表为例,并获取年龄大于17 岁的 学生的数据。

σage > 17 (Student)

σ age > 17 (Student)

This will fetch the tuples(rows) from table Student, for which age will be greater than 17.

这将从表Student中获取元组(行),其年龄将大于17

You can also use, and, or etc operators, to specify two conditions, for example,

您还可以使用and or etc运算符来指定两个条件,例如,

σage > 17 and gender = 'Male' (Student)

σ age > 17 and gender = 'Male' (Student)

This will return tuples(rows) from table Student with information of male students, of age more than 17.(Consider the Student table has an attribute Gender too.)

这将返回“ 学生”表中的元组(行),其中包含年龄大于17岁的男学生的信息(考虑“学生”表也具有“ Gender属性。)

项目运作(∏) (Project Operation (∏))

Project operation is used to project only a certain set of attributes of a relation. In simple words, If you want to see only the names all of the students in the Student table, then you can use Project Operation.

项目操作仅用于投影关系的特定属性集。 简而言之,如果您只想在“ 学生”表中看到所有学生的姓名 ,则可以使用“项目操作”。

It will only project or show the columns or attributes asked for, and will also remove duplicate data from the columns.

它只会投影或显示所需的列或属性,还将从列中删除重复的数据。

Syntax: A1, A2...(r)

语法: A1, A2... (r)

where A1, A2 etc are attribute names(column names).

其中A1,A2等是属性名称(列名称)。

For example,

例如,

Name, Age(Student)

Name, Age (Student)

Above statement will show us only the Name and Age columns for all the rows of data in Student table.

上面的语句仅向我们显示Student表中所有数据行的NameAge列。

联盟运作(∪) (Union Operation (∪))

This operation is used to fetch data from two relations(tables) or temporary relation(result of another operation).

此操作用于从两个关系(表)或临时关系(另一个操作的结果)中获取数据。

For this operation to work, the relations(tables) specified should have same number of attributes(columns) and same attribute domain. Also the duplicate tuples are autamatically eliminated from the result.

为了执行此操作,指定的关系(表)应具有相同数量的属性(列)和相同的属性域。 同样,从结果中自动删除重复的元组。

Syntax: A ∪ B

语法: A ∪ B

where A and B are relations.

其中A和B是关系。

For example, if we have two tables RegularClass and ExtraClass, both have a column student to save name of student, then,

例如,如果我们有两个表RegularClassExtraClass ,它们都有一个列Student来保存Student的名称,那么,

Student(RegularClass) ∪ ∏Student(ExtraClass)

Student (RegularClass) ∪ ∏ Student (ExtraClass)

Above operation will give us name of Students who are attending both regular classes and extra classes, eliminating repetition.

通过上述操作,我们可以为参加常规课程和额外课程的学生取名,从而避免重复学习。

设置差异(-) (Set Difference (-))

This operation is used to find data present in one relation and not present in the second relation. This operation is also applicable on two relations, just like Union operation.

此操作用于查找以一种关系存在而不以第二种关系存在的数据。 就像联合运算一样,该运算也适用于两个关系。

Syntax: A - B

语法: A - B

where A and B are relations.

其中A和B是关系。

For example, if we want to find name of students who attend the regular class but not the extra class, then, we can use the below operation:

例如,如果我们要查找参加普通班而不是额外班级的学生的姓名,则可以使用以下操作:

Student(RegularClass) - ∏Student(ExtraClass)

Student (RegularClass) - ∏ Student (ExtraClass)

笛卡尔积(X) (Cartesian Product (X))

This is used to combine data from two different relations(tables) into one and fetch data from the combined relation.

这用于将来自两个不同关系(表)的数据组合为一个,并从组合关系中获取数据。

Syntax: A X B

语法: AXB

For example, if we want to find the information for Regular Class and Extra Class which are conducted during morning, then, we can use the following operation:

例如,如果我们想查找早晨进行的常规班和额外班的信息,则可以使用以下操作:

σtime = 'morning' (RegularClass X ExtraClass)

σ time = 'morning' (RegularClass X ExtraClass)

For the above query to work, both RegularClass and ExtraClass should have the attribute time.

为了使以上查询正常工作, RegularClassExtraClass都应具有属性time

重命名操作(ρ) (Rename Operation (ρ))

This operation is used to rename the output relation for any query operation which returns result like Select, Project etc. Or to simply rename a relation(table)

此操作用于重命名返回查询(如Select,Project等)的任何查询操作的输出关系。或者仅重命名关系(表)

Syntax: ρ(RelationNew, RelationOld)

语法: ρ(RelationNew, RelationOld)



Apart from these common operations Relational Algebra is also used for Join operations like,

除了这些常见的运算,关系代数还用于Join运算,例如,

  • Natural Join

    自然加入
  • Outer Join

    外连接
  • Theta join etc.

    Theta连接等

翻译自: https://www.studytonight.com/dbms/relational-algebra.php

数据库中关系代数是什么