首页 > 大数据 > 数据分析 > Relational Algebra
Languages for describing queries on a relational database
Structured Query Language (SQL)
Relational Algebra
Below are fundamental operations that are "complete". That is, this set of operations alone can define any retrieval.
Convenient, natural additions to the set of operations makes
Choose a subset of tuples from a relation based on some criteria, results in another relation called a "result set"
Notation uses lower case sigma:
σcondition(relation)
Notation is lower case pi
πattribute- list(relation)
Union: r ∪ s ? a row is in the result set if it is a row from r or from s; or the set of all tuples that belong to either r or s.
[Intersection: r ∩ s ? a row is in the result set if it is a row from both r and from s.]
Set Difference: r - s ? the set of all tuples in r that are not in s
Both operations require that the sets r and s are union compatible:
Union compatible relations can be combined using union, intersection ∩, and set difference.
If R and S are two relations, R × S is the set of all concatenated tuples , where x is a tuple in R and y is a tuple in S
R × S is expensive to compute:
| R | S | R X S | |||||||
|---|---|---|---|---|---|---|---|---|---|
| a | b | c | d | a | b | c | d | ||
|
xl
|
x2
|
y1
|
y2
|
x1
|
x2
|
y1
|
y2
|
||
|
x3
|
x4
|
y3
|
y4
|
x1
|
x2
|
y3
|
y4
|
||
|
|
|
x3
|
x4
|
y1
|
y2
|
||||
|
x3
|
x4
|
y3
|
y4
|
||||||
Result of an expression evaluation is a relation, called the result set.
Attributes of relation must have distinct names. This is not guaranteed with Cartesian product. Suppose in the previous example attributes a and c have the same name.
Renaming operator tidies this up. To assign the names A1, A2,… An to the attributes of the n column relation produced by
expression expr, use the form expr [A1, A2, … An]
This is a derived operation, i.e., it is based on the basic operations of the relational algebra. It is a convenience operation because it is done so much.
A (general or theta θ) join of R and S is the expression
R
join-condition S
where join-condition is a conjunction of terms Ai oper Bi in which Ai is an attribute of R and Bi is an attribute of S and oper is one of =, <, >, ≤, ≥, ≠
The meaning is essentially:
σjoin.-condition(R X S)
where join-condition in both case are the same, except for possible renamings of attributes.
Problem: R and S might have attributes with the same name – in which case the Cartesian product is not defined
Solution:
This is a derived operation, i.e., it is based on the basic operations of the relational algebra. It is a convenience operation because it is done so much.
A (general or theta θ) join of R and S is the expression
R
join-condition S
where join-condition is a conjunction of terms Ai oper Bi in which Ai is an attribute of R and Bi is an attribute of S and oper is one of =, <, >, ≤, ≥, ≠
The meaning is essentially:
σjoin.-condition(R X S)
where join-condition in both case are the same, except for possible renamings of attributes.
Problem: R and S might have attributes with the same name – in which case the Cartesian product is not defined
Solution:
Goal: Produce the tuples in one relation, r, that match all tuples in another relation, s
Can be expressed in terms of projection, set difference, and cross-product
Cartesian product 笛卡尔积
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26477398/viewspace-2144973/ ,如需转载,请注明出处,否则将追究法律责任。