Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— Presentation transcript:

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) and produces another relation as its result. Relational algebra is the basis for query languages and was originally defined by Codd.

2 2 Algebra of Queries Extended relational algebra QUERY 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 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 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 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 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 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 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 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 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 11  CITY = 'London' (S) S #SNAMESTATUSCITY S1SMITH20London S4CLARK20London

12 12  WEIGHT < 14 (P) P #PNAMECOLORWEIGHTCITY P1NUTRED12London P5CAMBLUE12Paris

13 13  CITY (S) CITY LONDON PARIS ATHENS

14 14  SNAME,CITY, S#,STATUS (S) SNAMECITYS #STATUS SMITHLONDONS120 JONESPARISS210 BLAKEPARISS330 CLARKLONDONS420 ADAMSATHENSS530

15 15 STATUSCOLOR 20RED 10RED 30RED 20GREEN 10GREEN 30GREEN 20BLUE 10BLUE 30BLUE  COLOR (S  P)

16 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 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 18 NATURAL JOIN Simplest and most common join. We denote it by R  S = π L (σ c (R  S)) 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 19 THETA JOIN   Another type of join is THETA JOIN  R  c S  where C is some condition, If condition C is of the form x=y then join is called EQUIJOIN.

20 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 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 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 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 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 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 26 Example MovieStar (name, address, gender, birthdate) StarsIn (title, year, starName)

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

28 28 SQL 1 SELECT title, birthdate FROM MovieStar, StarsIn WHERE year = 1996 AND gender = ‘F’ AND StarName = name;

29 29 SQL 2 SELECT title, birthdate FROM MovieStar, StarsIn WHERE year = 1996 AND gender = ‘F’ AND starName = name;

30 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 31

32 32

33 33

34 34


Download ppt "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)"

Similar presentations


Ads by Google