Relational Databases - Relational Algebra

1. Traditional set operations

Traditional set operations include union, intersection, difference, and Cartesian product, all of which are relatively simple.
insert image description here


2. Special relational operations

Specialized relational operations include selection, projection, join, division, etc.

1. Choice (restriction)

Select the tuples that satisfy the given conditions in the relation R, denoted as:
σ F (R ) = {t |t ∈ R∧F (t ) = 'True'}

F represents the selection condition , which is a logical expression whose value is "true" or " false " . Operate in terms of rows.

For example: to query all students of the Information Department (IS Department): σ Sdept='IS' (Student)

Note: If the attribute name is a number, do not add single quotes

2. Projection

Select several attribute columns from R to form a new relationship, which is written as:
π A ( R ) = { t [A]| t ∈ R }

A represents the attribute column in R.
The projection operation is mainly performed from the perspective of the column
, but after the projection, not only some columns in the original relationship are canceled, but some tuples may also be canceled (in order to avoid repeated rows).

For example: Query the student's name and department: π Sname, Sdept (Student)

3. join

Connection is also called θ connection, which selects tuples that satisfy certain conditions between attributes from the Cartesian product of two relations
insert image description here

A and B are respectively equal and comparable attribute groups on R and S.
θ is a comparison operator
. Projection operations are mainly performed from the perspective of columns

1) Equivalent connection

Select those tuples with equal attribute values ​​of A and B from the generalized Cartesian product of relations R and S

2) Natural connection

The components to be compared in the two relations must be the same attribute group , and the duplicate attribute columns should be removed in the result .

insert image description here

Suspension tuple : When two relations R and S are connected naturally, the discarded tuples in the relation R and S
Outer connection : save the discarded tuple of the suspension tuple into the result relation, and fill in the blank value on other attributes ( NULL)
left outer join : keep only the suspended tuples in the relation R
on the left right outer join : keep only the suspended tuples in the relation S on the right

insert image description here

4. Except (Division)

Given a relation R(X,Y) and S(Y,Z), where X,Y,Z is an attribute group, Y in R and Y in S can have different attribute names, but must come from the same domain set .
The division operation of R and S obtains a new relationship P(X), P is the projection of the tuple in R that satisfies the following conditions on the X attribute: the image set Yx of the component value x of the tuple on X contains S in
Y The collection of projections on

insert image description here
Explanation :

  • In the relationship R and S, the same attribute group is (B, C), first list all the component values ​​​​of the attribute group A corresponding to the image set
    a 1 The image set is {(b 1 ,c 2 ), (b 2 ,c 3 ), (b 2 ,c 1 )} The image set of
    a 2 is {(b 3 ,c 7 ), (b 2 ,c 3 )} The image set of
    a 3 is {(b 4 ,c 6 )} The image set of
    a 4 is {(b 6 ,c 6 )}
  • Next, observe the image set corresponding to each component value , and find which one contains the projection set S on (B,C) of all attribute groups (B,C) in the relationship S. The
    projection of S on (B,C) is {(b 1 ,c 2 ) , (b 2 ,c 3 ), (b 2 ,c 1 )}
    It can be observed that only the image set of a 1 contains the projection of S on the (B,C) attribute group,
    so R÷S=a 1 , as shown in the figure shown

Guess you like

Origin blog.csdn.net/z135733/article/details/128812350