Relational database (2) --- relational algebra


foreword

This article mainly explains the relational algebra


1. Relational Algebra

1. Relational operations

Relational operations are set operations, and the objects and results of the operations are sets, which is set-at-a-time. The non-relational data operation method is a record-at-a-time (Record-at-a-time)
insert image description here
relational model. Compared with other models, the most distinctive feature is its database language. This language is flexible and convenient, expressive and functional.

At present, the languages ​​used in relational databases generally have the characteristics of integration of definition, query, update and control, and query is the most important part .

Therefore, the core part of a relational database is query, so it is also called query language, and the query conditions should be expressed by relational operation expressions.

Therefore, relational operations are the basis for designing relational data languages.

According to different methods of expressing queries, relational operations can be divided into two categories: relational algebra and relational calculus .

2. Relational Algebra

Relational algebra is an abstract query language that expresses queries in terms of operations on relations

relational algebra

1.运算对象是关系
2.运算结果亦为关系

There are two types of operators in relational algebra: set operators and specialized relational operators
Traditional set operations are performed from the perspective of the "horizontal" direction of the relationship, that is, rows, and specialized relational operations involve not only rows but also columns

2. Set operation

Traditional set operations:

1.并
2.差
3.交
4.笛卡尔积

Specialized relational operations:

1.选择运算 
2.投影运算 
3.连接运算 
4.除运算

1. Traditional set operations

(1) and operation

insert image description here

A collection of all tuples that appear in at least one of the two relations.
R ⋃ S = { t ∣ t ∈ R ∪ ∈ t ∈ S } R\bigcup S =\{ t | t\in R\cup \in t\in S \}RS={ ttRtS }
If two relations R and S are combined, they must be compatible:

1.关系R和S必须是同元的,即它们的属性数目必须相同。
2.对任意i,R的第i个属性的域必须和S的第i个属性的域相同。

insert image description here

(2) difference operation

insert image description here

Assuming that the relation R and S have the same relation schema, the difference between R and S is a set of tuples belonging to R but not belonging to S, denoted as:

R − S = { t ∣ t ∈ R ∩ t ∉ S } R-S =\{ t | t\in R\cap t\notin S \} RS={ ttRt/S }
R and S must be compatible

insert image description here

(3) Cross operation

insert image description here
A tuple that belongs to both R and S. Write:
R ∩ S = { t ∣ t ∈ R ∩ t ∈ S } R\cap S =\{ t | t\in R \cap t\in S \}RS={ ttRtS}

The intersection of relations can be expressed by difference, that is,
R ∈ S = R − ( R − S ) R \in S = R -(R - S)RS=R(RS)
insert image description here

(4) Cartesian product

The Cartesian product of two relations R and S of n-order and m-order respectively is a ( n + m ) (n+m)(n+m ) a collection of tuples for the columns. The first n columns of the tuple are a tuple of the relation R, and the last m columns are a tuple of the relation S. If R hask 1 k_1k1tuples, S has k 2 k_2k2tuples, then the Cartesian product of relation R and relation S has k 1 × k 2 k_1\times k_2k1×k2tuples. Write:
R × S = { trts ^ ∣ tr ∈ R ∩ ts ∈ S } R\times S=\{\widehat{t_rt_s}|t_r\in R\cap t_s\in S\}R×S={ trts trRtsS}
R × S R\times S R×The degree of S is the sum of the degrees of R and S,
R × S R\times SR×The number of tuples in S is the product of the number of tuples in R and S.
insert image description here

2. Specialized relational operations

(1) Select operation

Find all tuples from a relation that satisfy a given condition.
δ F ( R ) = { t ∣ t ∈ R , F ( t ) = 'true' } \delta_F(R)=\{t | t\in R , F(t) = 'true'\}dF(R)={ ttR,F(t)=' true ' }
The operation is performed from the perspective of the row, that is, the tuple is extracted horizontally.

insert image description here
The result obtained through the selection operation can form a new relationship,Its relationship pattern remains unchanged,But the number of tuples is less than or equal to the number of tuples in the original relation,It is a subset of the original relation.

(2) Projection operation

Select a new relationship composed of several attributes from the relationship.
Π A ( R ) = { t [ A ] ∣ t ∈ R } , A ⊆ R \Pi_A(R) = \{ t[A] | t\in R \} , A\subseteq RPiA(R)={ t[A]tR},AR
operates from the perspective of columns, that is, it extracts tuples vertically.
Identical rows are removed from the projected result.

insert image description here

(3) Connection operation

From the Cartesian product of two relations, select tuples that satisfy certain conditions between attributes
R ⋈ A θ BS = { trts ^ ∣ tr ∈ R ∩ ts ∈ S ∩ tr [ A ] θ ts [ B ] } θ is an arithmetic operation , is called an equijoin when it is an equal sign. A , B are attribute columns with equal and comparable degrees on R and S. R\Join_{A\theta B} S = \{\widehat{t_rt_s}|t_r\in R\cap t_s\in S\cap t_r[A]\theta t_s[B] \} \theta is an arithmetic operation, which is When the equal sign is used, it is called an equijoin. A, B are attribute columns with equal and comparable degrees on R and S.RA θBS={ trts trRtsStr[A]θts[ B ]} θ is an arithmetic operation ,When it is an equal sign, it is called an equijoin. A ,B is an attribute column of equal and comparable degree on R and S.
It can be seen from the definition that the join operation is a selection operation performed on the Cartesian product of two relations.

conditional connection

Relation R and S, find R ⋈ C = ESR \Join_{C=E}SRC=ES

The table below is R
R
and the table below is S
insert image description here
and the result is:
insert image description here

equijoin

The connection operation where θ is "=" is called an equivalence connection

have:

自然连接(Natural join)(特殊)
左连接(Left Join)
右连接(Right Join)

natural connection

Steps:

1.选择同时出现在R和S中属性相等元组;
2.去掉重复属性。    

It can be seen that if two relations have no common attributes, the natural join is a Cartesian product.
The table below is R
R
and the table below is S
insert image description here
and the result is:
insert image description here

Left Join and Right Join

Left Join (Left Join)
R left join S: all tuples from R and those tuples of S where the joined fields are equal.

Right Join
R right join S: all tuples from S and those tuples in R where the joined fields are equal.

Form R:
insert image description here

Table S:
insert image description here
The result of R left join S is:

R.B=S.B ∧ R.C=S.C

insert image description here

The result of R right joining S is:
R.B=S.B ∧ R.C=S.C
insert image description here

(4) Division operation

Elephant pole

Given a relation R(X,Z), X and Z are attribute groups. When t[X]=x, the image set (Images Set) of x in R is:
Z x = { t [ Z ] ∣ t ∈ R , t [ X ] = x } Z_x=\{t[Z] |t\in R,t[X]=x\}Zx={ t[Z]tR,t[X]=x }
It represents the set of components of the tuples whose value is x on attribute group X in R in Z.

In relation R, A can take four values ​​{a1, a2, a3, a4}:

    a1的象集为 {(b1,c2),(b2,c3),(b2,c1)}
 	a2的象集为 {(b3,c7),(b2,c3)}
	a3的象集为 {(b4,c6)}
 	a4的象集为 {(b6,c6)}

insert image description here

operation

Given relations R(X, Y) and S(Y, Z), where X, Y, Z are attribute groups. Y in R and Y in S can have different attribute names, but must come from the same set of domains. The division operation of R and S obtains a new relation P(X), P is the projection of tuples in R satisfying the following conditions on the X attribute column: the image set Y x Y_x of the component value x of the tuple on XYxA collection containing the projections of S on Y.

R ÷ S = { tr [ X ] ∣ tr ∈ R ∩ π Y ( S ) ⊆ Y x } Y x : x = tr [ X ] R÷S =\{t_r [X] | t_r \in R\cap \pi_Y (S) \subseteq Y_x \} \\Y_x: the image set of x in R, x = t_r[X]R÷S={ tr[X]trRPiY(S)Yx}Yx: the icon set of x in R , x=tr[X]
insert image description here

Example 1

R1 table:
insert image description here

R2 table:
insert image description here

The result of R1÷R2:
insert image description here

Example 2

insert image description here

3. Query optimization

The process of converting one relational algebra expression into another more efficient relational algebra expression is called "query optimization".

(1) Optimization criteria

① Execute the selection operation early.
② Combine the Cartesian product and the subsequent selection operation into a connection operation.
③ Simultaneously perform the projection operation and other subsequent operations to avoid repeated scanning of the relationship.
④ Combine the projection operation with the binocular operation before and after it to avoid scanning the relationship again to remove some fields.
⑤ Properly preprocess the relationship before executing the connection. You can quickly find the tuples to join.
⑥ Store common subexpressions.


Summarize

The inappropriateness of the article, please forgive me and correct the readers

Guess you like

Origin blog.csdn.net/weixin_52042488/article/details/126924693
Recommended