Set Operators and Relational Operators in Relational Algebra Operations

Relational algebra is an abstract query language and a mathematical tool for studying relational models. The objects of operation in relational algebra are relations, and the results of operations are also relations. Relational algebra operators are mainly divided into two categories: set operators and relational operators.

Set operators and relational operators.png

Set operators include Cartesian product, union, intersection, and difference, and relational operators include division, selection, projection, and connection. The concepts and operation rules of the two types of operators are introduced in detail below.

1. Cartesian product

In mathematics, the Cartesian product is the multiplication of two sets. If the Cartesian product is not performed on the set A and the set B, the first object in the result is a member of A, and the second object is all possibilities of B. One member of an ordered pair.
1692165537495_Cartesian product.png

In the database, the generalized Cartesian product operates on two relations, and the number of records in the new relation is the product of the number of records in the two relations.

Suppose there are relation R and relation S, relation R has n fields, relation S has m fields, the result of the Cartesian product of R and S (ie R×S) is a new relation with n+m fields. In the new relation, the first n fields of the record come from R, the last m fields come from S, and the total number of records is the product of the records in R and S.

There are 2 fields in relation R which are student number and student name; 2 records are (1, Zhang San) and (2, Li Si)

There are 2 fields in relation S which are class number and class name; the 2 records are (001, software class) and (002, network class), and R×S has 2+2 fields in total, which are student number, student Name, class number, and class name.

R×S has a total of 2×2 records, which are (1, Zhang San, 001, software class), (1, Zhang San, 002, network class), (2, Li Si, 001, software class), (2 , Li Si, 002, network class).

1692165994040_Cartesian 1.png

2. Merge, intersect, difference

Union, intersection and difference operations require that the two relations involved in the operation have the same number of fields, and the result of the operation is a new relation with the same number of fields.

Suppose there is a relationship R and a relationship S, RUS means to merge the records in the two relationships, RnS means to find the records that belong to both R and S, and R~S means to find the records that belong to R but not to S.

There are 2 records in the relation R, which are (1, Zhang San) and (2, Li Si).

There are 2 records in relation S, namely (1, Zhang San), (3, Xiao Ming.

RS operation: There is a record (1, Zhang San) in both relation R and relation S, the record needs to be deduplicated, and the result is (1, Zhang San), (2, Li Si), (3, Xiao Ming).

RnS operation: record (1, Zhang San) is in both relation R and relation S, and the result is (1, Zhang San).

RS operation: the record (2, Li Si) belongs to the relation R, but does not belong to the relation S, and the result is (2, Li Si).

combine, intersect, difference

3. Except

The division operation is the inverse of the Cartesian product.

Assuming that there is a relationship R and a relationship S, the field set that needs to satisfy S in the division operation is a proper subset of the R field set, and the result of R÷S is the result of subtracting the S field set from the R field set. For example, the result of R(A,B,C,D)÷S(C,D) consists of two fields A and B.

R÷S1 means querying the courses chosen by the student with the student number 2. From the relationship R, it can be known that the course numbers chosen by the student with the student number 2 are 1, 2, and 3.

R÷S2 means querying the courses chosen by the students with the student number 2 and 3. According to the relationship R, it can be known that the course numbers chosen by the student with the student number 3 are 1 and 2, and the courses chosen by the student with the student number 2 are 1, 2, 3, then the result of R:S2 is 1, 2.

Division

4. Selection and Projection

Selection is to find out the records that meet the conditions in a relationship, that is, to filter in the horizontal direction;

Projection is to remove unnecessary fields in a relationship and keep the required fields, that is, to filter in the vertical direction.

Select operation: oStudent number=1® means to find the student whose student number is 1 in the relationship R, and find the record (1, Zhang San, male);

Projection operation: π student number, student name ® means to find the student number and student name in the relationship R, that is, keep the student number field and student name field, and remove the student gender field.

selection and projection

5. Connect

Joining is to select records that meet certain conditions between fields in the Cartesian product of two relations.

Commonly used connection methods are equivalent connection and natural connection.

Assuming a relation R and a relation S, use A and B to denote equal and comparable field groups in R and S, respectively. Equivalent connection is to select records with equal field values ​​of A and B in the Cartesian product of R and S.

Natural join is a special equivalence join, which requires R and S to have the same field group, and after equivalence join, duplicate field groups are removed.

There are 4 records in relation R and 3 records in relation S, and there are 12 records in total in R×S,

The result of the equivalence connection operation is the records with the same class number in RxS.

The result of the natural join operation is to remove the repeated field group class numbers from the result of the equivalence join operation.

1692167221367_Connection.png

Guess you like

Origin blog.csdn.net/cz_00001/article/details/132324975