34
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.

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)

Embed Size (px)

Citation preview

Page 1: 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)

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.

Page 2: 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)

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.

Page 3: 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)

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

Page 4: 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)

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

Page 5: 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)

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}

Page 6: 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)

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.

Page 7: 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)

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

Page 8: 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)

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.

Page 9: 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)

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

Page 10: 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)

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

Page 11: 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)

11

CITY = 'London' (S)

S # SNAME STATUS CITY

S1 SMITH 20 London

S4 CLARK 20 London

Page 12: 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)

12

WEIGHT < 14 (P)

P # PNAME COLOR WEIGHT CITY

P1 NUT RED 12 London

P5 CAM BLUE 12 Paris

Page 13: 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)

13

CITY(S)

CITY

LONDON

PARIS

ATHENS

Page 14: 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)

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

Page 15: 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)

15

STATUS COLOR

20 RED

10 RED

30 RED

20 GREEN

10 GREEN

30 GREEN

20 BLUE

10 BLUE

30 BLUE

COLOR (SP)

Page 16: 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)

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).

Page 17: 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)

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

   

Page 18: 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)

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 .

Page 19: 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)

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.

Page 20: 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)

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

Page 21: 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)

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.

Page 22: 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)

22

GROUPING AND AGGREGATION

• If there are no grouping attributes, the entire relation R is one group.

  Example   Consider relation

StarsIn (title, year, starName)

Page 23: 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)

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;

Page 24: 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)

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.

Page 25: 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)

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

Page 26: 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)

26

Example

MovieStar (name, address, gender, birthdate)

StarsIn (title, year, starName)

Page 27: 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)

27

  Query

Find birthdate and movie title for those female stars who appeared in movies in 1996

Page 28: 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)

28

SQL 1

SELECT title, birthdate

FROM MovieStar, StarsIn

WHERE year = 1996 AND gender = ‘F’

AND StarName = name;

Page 29: 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)

29

SQL 2

SELECT title, birthdate

FROM MovieStar, StarsIn

WHERE year = 1996 AND gender = ‘F’

AND starName = name;

 

Page 30: 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)

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

Page 31: 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)

31

πtitle, birthdate |

StarName=name /\

/ \ / \ / \

/ \ σgender = ‘F’ σyear = 1996

| | MovieStar StarsIn

Page 32: 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)

32

πtitle, birthdate

| σyear=1996 AND gender = ‘F’ AND starName = name

|

/\ / \

/ \ / \

MovieStar StarsIn

Page 33: 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)

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

Page 34: 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)

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