[Database] Formal relational query language (1): Relational Algebra

Table of contents

1. Relational Algebra

1. Basic operations

a. Select Operation

b. Project Operation

combination

c. Union Operation

d. Set Difference Operation

e. Cartesian-Product Operation

 f. Rename Operation

2. Formal definition of relational algebra

3. Additional relational algebra (Additional Operations)

a. Set-Intersection Operation

b. Natural Join Operation

c. Division Operation

d. Assignment Operation

e. Outer join operation

4. Extended relational algebra operations

a. Generalized-projection

b. Aggregation operation

2. Tuple Relational Calculus

3. Domain Relational Calculus


This article needs to be further improved ~

     Relational algebra is a procedural query language. It consists of a set of operations that take one or two relations as input and produce a new relation as a result. The basic operations of relational algebra are: selection, projection, union, set difference, Cartesian product and renaming . In addition to the basic operations, there are some other operations, namely set intersection, natural join and assignment . We will define these operations using basic operations.

1. Relational Algebra

1. Basic operations

        The selection, projection, and rename operations are called unary operations because they operate on one relation and the other three operations operate on two relations. Hence they are called binary operations.

a. Select Operation

       The selection (sellct) operation selects tuples that satisfy a given predicate , represented by the symbol σ:

        The selection operation uses a conditional expression as a parameter to judge each tuple in the relationship. Only tuples that meet the condition will be selected to form a new relationship. The selection operator is usually represented by the symbol σ.

The syntax of the selection operation is as follows:
σ<conditional expression>(relationship)

Among them, the conditional expression is a logical expression that can be compared and combined using attributes in the relationship. Comparison operators (such as equal to, greater than, less than, etc.) and logical operators (such as AND, OR, NOT, etc.) can be used to build conditional expressions.

The result of the selection operation is a new relation containing tuples from the original relation that satisfy the condition. The schema (set of attributes) of the new relationship is the same as the original relationship.

For example, suppose there is a relation R containing attributes A, B, and C. We can use the selection operation to select tuples that satisfy the condition A>5. The syntax is σ(A>5)(R).

b. Project Operation

combination

c. Union Operation

     

        1. The relations r and s must be homogeneous, that is, they must have the same number of attributes.

        2. For all i, the domain of the i-th attribute of r must be the same as the domain of the i-th attribute of s.

Note that r and s can be database relations or temporary relations that are the result of relational algebra expressions.

d.Set  Difference Operation

        The set-difference operator, represented by -, allows us to find those tuples that are in one relation but not in another. The result of the expression rs is a relation containing all tuples that are in r but not in s.

e.  Cartesian-Product Operation

 f.Rename  Operation _

2. Formal definition of relational algebra

3. Additional relational algebra (Additional Operations)

a. Set-Intersection Operation

  • Definition: r ∩ s =

  • Key points:

    • r , s must contain the same attributes, that is, the same element
    • r , s the fields of the attributes must be compatible
  • Note that any relational algebra expression that uses set intersection can be rewritten by replacing the set intersection operation with a pair of set difference operations:

        r ∩ s = r − ( r − s )

b.  Natural Join Operation

  • Definition: r⋈s, r the table and  s the table are Cartesian products based on repeated attributes, and finally the repeated attributes are removed

Note that if the relations r(R) and s(S) do not contain any identical properties, that is, R ∩ S =0, then r ∩ s=r XS

  • example:

    • Find the names of all teachers in the School of Computing and the names of the courses they take

    • Find the name of the teacher teaching 'DBS' and 'OS' and the course title

c.  Division Operation

  • Definition: r÷s�÷�
  • R=(A1,A2,...Am,B1,B2,...Bn),S=(B1,B2,...Bn)
  • Explanation: The premise is that  s the attributes of the table are included in  r the table. After  r removing  s the table attributes from the table attributes,  the tuples r containing all the data in the table  are selected. sIn fact, it is difficult to describe in words, and it is better to understand it by looking at pictures.

  • Application: Problems with "containing all elements of a certain set" can be solved using division

  • example:

    • Select the IDs of students who have taken all courses in the Department of Computer Science: ∏courseid,ID(takes)÷(∏courseid(σdepartment=′Comp.Sci.′(course)))∏��������,�� (�����)÷(∏��������(�����������=′����.���.′(������) ))

d.  Assignment Operation

  • Definition: temp←expressio, the query results are saved in the temporary table

e. Outer join operation

4. Extended relational algebra operations

a. Generalized-projection

b. Aggregation operation

2. Tuple Relational Calculus

To be improved

3. Domain Relational Calculus

To be improved

Guess you like

Origin blog.csdn.net/m0_63834988/article/details/133419594