1 algebra of queries classical relational algebra it is a collection of operations on relations....
TRANSCRIPT
1
Algebra of Queries
• Classical Relational Algebra
It is a collection of operations on relations.
Each operation takes one or two relations as its operand(s) and produces another relation as its result. Relational algebra is the basis for query languages and was originally defined by Codd.
2
Algebra of Queries
• Extended relational algebraQUERY PROCESSOR turns user queries and data modification commands into a sequence of operations on the relations (and executes those operations). The set of these operations forms the algebra of queries or extended relational algebra
• The Relational algebra is SET based, while SQL is BAG or MULTISET based.
3
Classical Relational Algebra
• Classical Relational algebra operators may be classified into two groups :
1. Traditional Set Operators– a. Union– b. Intersection– c. Difference– d. Cartesian Product
4
Extended Relational Algebra
For union, intersection and difference we require that the schemas of the two argument relations must be the same. But a new set of rules are required for operations on bags
R U S no of times a tuple present in R + no of times tuple present in S
R∩S A tuple t is in the result the minimum of the number of times it is in R and
5
Extended Relational Algebra
R—S means A tuple t is in the result the number of times it is in R minus the number of times it is in S, but not fewer than zero times.
Let R = {A, B, B} and S={C, A, B, C} be two bags then
R U S ={A, A, B, B, B, C, C}
R ∩ S = {A, B}
R—S ={B}
6
Special Relational Operations
• Extended SELECTION Operator
Traditional selection yield a "horizontal" subject of a relation, that is, that subset of tuples within the given relation for which a specified predicate is satisfied.
• The extended selection σc takes a relation R and
condition C. The condition C can involve arithmetic or string operators, comparisons or Boolean operators.
7
Special Relational Operations
• Extended PROJECTION Operator
• The traditional projection yields a "vertical" subset of a given relation, that is, that subset obtained by selecting attributes, in a specified left-to-right order, then eliminating duplicate tuples within the attributes selected
8
Special Relational Operations
• Extended PROJECTION Operator
• If R is a relation, then πL(R) is the
projection of R on to the list L where L is a list of attributes of R.
• The list L can have :
1. A single attribute of R.
9
Special Relational Operations
• Extended PROJECTION Operator
2. An expression x y where x and y are names of attributes: it means we take the attribute x of R and rename it as y.
3. An expression E z where E is an expression involving attributes of R and z is a new name for the attribute that results from the calculation implied by Z. Ex: a+b x
10
Special Relational Operations
• Extended PROJECTION Operator
4. Each tuple of R yields one tuple of the result.
5.Duplicate tuples in R yield duplicate tuples in the result but the result can have duplicates even if R does not. x
11
CITY = 'London' (S)
S # SNAME STATUS CITY
S1 SMITH 20 London
S4 CLARK 20 London
12
WEIGHT < 14 (P)
P # PNAME COLOR WEIGHT CITY
P1 NUT RED 12 London
P5 CAM BLUE 12 Paris
13
CITY(S)
CITY
LONDON
PARIS
ATHENS
14
SNAME,CITY, S#,STATUS (S)
SNAME CITY S # STATUS
SMITH LONDON S1 20
JONES PARIS S2 10
BLAKE PARIS S3 30
CLARK LONDON S4 20
ADAMS ATHENS S5 30
15
STATUS COLOR
20 RED
10 RED
30 RED
20 GREEN
10 GREEN
30 GREEN
20 BLUE
10 BLUE
30 BLUE
COLOR (SP)
16
DUPLICATE ELIMINATION
• This corresponds to the DISTINCT operator in SQL. It is denoted by δ(R).Returns the set consisting of one copy of every tuple that appears one or more times in relation R.
• UNION, INTERSECT and EXCEPT operators of SQL eliminate duplicates but bag operators for union, intersection and difference retain duplicates.
• SQL UNION= δ(R U S).
17
JOINS
Join operators are built from a product followed by selection and projection.
Joins represent the effect of many common SQL queries whose FROM clause is a list of two or more relations and whose WHERE clause applies equalities or some other comparisons
18
NATURAL JOIN
Simplest and most common join. We denote it by
R S = πL(σc(RS))
where C is a condition that equates all pairs of attributes of R and S that have the same name.L is a list of all the attributes of R and S, except that one copy of each pair of equated attributes is omitted. If R.x and S.x are equated attributes, then in the result either we rename either R.x or S.x .
19
THETA JOIN
Another type of join is THETA JOIN
Rc S
where C is some condition, If condition C is of the form x=y then join is called EQUIJOIN.
20
GROUPING AND AGGREGATION
• Aggregation operators : AVG, SUM, COUNT, MIN, MAX
• Grouping: GROUP BY clause in SQL• Having clause must follow a GROUP BY clause• Grouping and aggregation are generally
implemented together. So we have a single operator defining it
•
21
GROUPING AND AGGREGATION
• The relation returned by the expression
• L(R)
• is given as Partition the tuples of R in to groups. Each group consists of all tuples having one particular assignment of values to the grouping attributes in the list L.
22
GROUPING AND AGGREGATION
• If there are no grouping attributes, the entire relation R is one group.
Example Consider relation
StarsIn (title, year, starName)
23
GROUPING AND AGGREGATION
• StarsIn (title, year, starName)
Query: For each star who has appeared in atleast three movies, the earliest year in whichthey appeared. SQL query:
SELECT starName, MIN (year) AS minYear
FROM StarsIn
GROUP BY starName
HAVING COUNT (title) > =3;
24
SORTING OPERATOR
• This is the SQL ORDER BY clause and denoted by the operator
L (R) • where R is a relation and L a list of some of R’s
attributes in the relation R but with the tuples of R sorted in the order indicated by L.
• If L is a1, a2…an, then tuples are first sorted by a1, then a2 until an. By default sorting is in ascending order.
25
EXPRESSION TREES
• Generated by combining several Qerry Algebra operators into one expression by applying one operator to the result(s) of one or more operators.
• The leaves of this tree are names of relations.
Interior nodes are operators, which are applied to the relations represented by its child or children
26
Example
MovieStar (name, address, gender, birthdate)
StarsIn (title, year, starName)
27
Query
Find birthdate and movie title for those female stars who appeared in movies in 1996
28
SQL 1
SELECT title, birthdate
FROM MovieStar, StarsIn
WHERE year = 1996 AND gender = ‘F’
AND StarName = name;
29
SQL 2
SELECT title, birthdate
FROM MovieStar, StarsIn
WHERE year = 1996 AND gender = ‘F’
AND starName = name;
30
SQL
Expression tree is formed as follows:
1. Combine the relations in the FROM list using product operators.
2. Perform a selection that represents the WHERE clause
3. Project on to the list in the SELECT clause
31
πtitle, birthdate |
StarName=name /\
/ \ / \ / \
/ \ σgender = ‘F’ σyear = 1996
| | MovieStar StarsIn
32
πtitle, birthdate
| σyear=1996 AND gender = ‘F’ AND starName = name
|
/\ / \
/ \ / \
MovieStar StarsIn
33
SELECT PROJECT PRODUCT
aabbcc
xyxyxy
UNION INTERSECTION DIFFERENCE
b1b2b3
c1c2c3
a1a2a3
b1b1b2
a1a2a3
b1b1b2
c1c1c2
(NATURAL) JOIN
aaabc
xyzxy
xz
a
DIVIDE
x
y
a
b
c
34
Sample Division
DEND DIVIDE BY DOR
DEND DOR
S # S # P # P # S1 S1 P1 P1 S2 S1 P2 S1 P3 S1 P4 P # S # S1 P5 P2 S1 S1 P6 P4 S4 S2 P1 S2 P2 S3 P2 P # S4 P2 P1 S4 P4 P2 S # S4 P5 P3 S1
P4 P5 P6