Download presentation
1
Relational Algebra (Chapter 7)
Database Management COP4540, SCS, FIU
2
What Is Relational Algebra?
A small set of operators that allow us to manipulate relations in limited, but easily implementable and useful ways. Relational algebra is closed. The results of one or more relational operations are always relations. Database Management COP4540, SCS, FIU
3
Operators in Relational Algebra
Set operators: UNION, INTERSECTION, and DIFFERENCE Picking certain rows from a relation: SELECTION Picking certain columns PROJECTION Composing relations in useful way: PRODUCT and JOIN Renaming relations and attributes: RENAMING Database Management COP4540, SCS, FIU
4
UNION UNION operator is to add the tuples in two relations ( R and S) together in a third relation (C). The order of the tuples in the third relation is not important. The duplicate tuples must be eliminated from the third relation. It is denoted by R S. R R S C S Database Management COP4540, SCS, FIU
5
UNION Union compatible
The condition that the relations must meet when imposed the UNION operator. Each relation must have the same number of attributes. The corresponding attributes must have the same domain. Database Management COP4540, SCS, FIU
6
INTERSECTION The INTERSECTION of two relations ( R & S) is a third relation that contains the tuples in the both relations. The relations much be union compatible. It is denoted by R S. R R S C S Database Management COP4540, SCS, FIU
7
DIFFERENCE The DIFFERENCE of two relations ( R & S) is a third relation (C) that contains the tuples in first relation (R) but not in the second relation (S). The relations must be union compatible. It is denoted by R – S. R – S is different from S – R, while R S and R S are the same as S R and S R respectively R R – S C S Database Management COP4540, SCS, FIU
8
PROJECTION PROJECTION is an operator to select specified attributes in a relation R. PROJECTION takes a vertical subset (columns) of a relation. The duplicate tuples must be eliminated if there are any. It is denoted by ΠAttri, ...(R). ΠAttri1, Attri2(R) C R Database Management COP4540, SCS, FIU
9
SELECTION SELECTION operator is to select a set of tuples in a relation (R) according to certain condition. SELECTION takes a horizontal subset of a relation. It is denoted by condi(R). attri1= 1 (R) C R Database Management COP4540, SCS, FIU
10
PRODUCT The PRODUCT of two relations ( R & S) is a relation (C) that contains the concatenation of every tuple of one relation with every tuple of the other relation. It is also called Cartesian product. If R has m tuples, S has n tuples, then C has m n tuples. It is denoted by R S. R R S Database Management COP4540, SCS, FIU C S
11
NATURAL JOIN NATURAL JOIN is a operator between two relations ( R and S). Suppose that R and S have n common attributes namely A1, … , An. A tuple r from R and a tuple s from S are successfully paired if and only if r and s agree on each of the attributes A1, … , An. The result of the pairing is a tuple called a joined tuple. All the joined tuples form the result relation. It is denoted by R S. Database Management COP4540, SCS, FIU
12
Examples of NATURAL JOIN
C = R S S R C = R S S R C = R S S Database Management COP4540, SCS, FIU
13
THETA JOIN A THEAT JOIN of two relations (R and S) based on a condition (Condi) is constructed as follows: Take the product of relations R and S. Select from the product only those tuples that satisfy the condition Condi. The THEAT JOIN is denoted by R Condi S R C = R attri1=attri4 S S Database Management COP4540, SCS, FIU
14
Renaming Renaming operator is to produces a relation identical to R but named S and with attributes, in order, named A1, A2, …, An. It has no effect regarding the tuples of the relation. It is denoted by ρS(A1, …,An ) (R). ρS(Attri4, Attri5, Attri6 ) (R). R S Database Management COP4540, SCS, FIU
15
Schema of the operators
If , , – applied, schemas are the same, so use this schema. Projection: use the attributes listed in the projection. Selection: no change in schema. Product: use attributes of R and S. if they share an attribute A, prefix it with the relation name as R.A, S.A Theta join: same as product. Natural join: use attribute from each relation; common attributes are merged anyway Renaming: Whatever it says. Database Management COP4540, SCS, FIU
16
Combine operations to form query
Algebra = Basis arguments + Ways of constructing expressions For relational algebra Arguments = variable standing for relations + finite, constant relations. Expressions constructed by applying one of the operators + parentheses. Query = expression of relational algebra. Database Management COP4540, SCS, FIU
17
Operator Precedence Unary operators, saying selection, projection and renaming, have highest precedence. Next highest are the “multiplicative” operations, saying natural join, theta join, and product. Lowest are the “additive” operators, saying union, intersection, and difference. Database Management COP4540, SCS, FIU
18
An example Supposing we have relation
Movie ( title, year, length, studio, producer) Query: What are the titles and years of the movies made by Fox that are at least 100 minutes long? Database Management COP4540, SCS, FIU
19
Answers Three selection conditions studio = ‘Fox’ (Movie)
length >= 100 (Movie) title, year ( Movie ). Three Possible Answers: title, year ( studio = ‘Fox’ (Movie) length >= 100 (Movie) ). title, year ( studio = ‘Fox’ AND length >= 100 (Movie) ). title, year ( studio = ‘Fox’ ( length >= 100 (Movie) ) ). Database Management COP4540, SCS, FIU
20
What are the titles of the movies that had been reproduced?
Queries: What are the titles of all the movies made in either 1999 or 2000? What are the titles of the movies made in both 1950 and 1980? What are the titles of the movies that had been reproduced? Database Management COP4540, SCS, FIU
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.