Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.

Similar presentations


Presentation on theme: "Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports."— Presentation transcript:

1 Relational Algebra

2 CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful query languages: – Strong formal foundation based on logic. – Allows optimization. Query Languages != programming languages! – QLs not intended to be used for complex calculations. – QLs support easy, efficient access to large data sets.

3 CENG 3513 Formal Relational Query Languages Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: 1.Relational Algebra: More operational, very useful for representing execution plans. 2.Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non- operational, declarative.)  Understanding Algebra & Calculus is key to understanding SQL, query processing!

4 CENG 3514 Preliminaries If a query is applied to relation instances, and the result of a query is also a relation instance. – Schemas of input relations for a query are fixed – The schema for the result of a given query is determined by the definition of query language constructs. Positional vs. named-field notation: – Positional notation easier for formal definitions, named- field notation is more readable. – Both used in SQL

5 CENG 3515 Example Instances R1 S1 S2S2 There are two instances of “ Sailors” one instance for “Reserves” relations.

6 CENG 3516 Relational Algebra Queries in in Algebra are composed of collection of operators. Operators can be unary or binary, which take relations as arguments. Operators are applied in a certain order, primarily based on the order they appear in expressions. Algebraic expressions represent some sort of query evaluation plans.

7 CENG 3517 Relational Algebra Basic operations: – Selection ( σ ) Selects a subset of rows from relation. – Projection ( ) Deletes unwanted columns from relation. – Cross-product (  ) Allows us to combine two relations. – Set-difference ( ) Tuples in reln. 1, but not in reln. 2. – Union (  ) Includes tuples in two argument relations. Additional operations: – Intersection, join, division, renaming: Not essential, but useful operators that can be described in terms of others, since each such operation takes relations as input and returns a relation.

8 CENG 3518 Projection (  ) Deletes attributes that are not in projection list. Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation. Projection operator normally eliminates duplicates! – Note: real systems typically don’t do duplicate elimination unless the user explicitly asks for it.

9 CENG 3519 Selection (  ) Selects rows that satisfy selection condition. No duplicates in result! (Why?) Schema of result identical to schema of (the only) input relation. Result relation can be the input for another relational algebra operation! (Operator composition.)

10 CENG 35110 Union, Intersection, Set-Difference All of these operations take two input relations, which must be union-compatible: – Same number of fields. – `Corresponding’ fields have the same type. What is the schema of result?

11 CENG 35111 Cross-Product (  ) S1  R1 : Each row of S1 is paired with each row of R1. Result schema has one field per field of S1 and R1, with field names `inherited’ if possible from both input relations. – Conflict: Both S1 and R1 have a field called sid.  Renaming operator :

12 CENG 35112 Joins Condition Join: R C S =  c ( R  S ) Result schema same as that of cross-product. Fewer tuples than cross-product, might be able to compute more efficiently, on the fly S1 R1 S1.sid < R1.sid

13 CENG 35113 Joins Equi-Join: A special case of condition join where the condition c contains only equalities on common name field. Result schema similar to cross-product, but only one copy of fields, for which equality is specified. S1 R1 sid

14 CENG 35114 Natural Joins Natural Join: Equi-join on all common (same name) fields. Result is guaranteed not to have two fields with the same name S1 R1

15 CENG 35115 Division Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. Let A have 2 fields, x and y; B have only field y: – A/B = – i.e., A/B contains all x tuples (sailors) such that for every y tuple (boat) in B, there is an x,y tuple in A. – Or: If the set of y values (boats) associated with an x value (sailor) in A contains all y values in B, then B is in A. In general, x and y can be any lists of fields; y is the list of fields in B, and x,y is the list of fields of A.

16 CENG 35116 Examples of Division A/B: A supplier who supply all the parts A B1 B2 B3 A/B1 A/B2 A/B3

17 CENG 35117 Expressing A/B Using Basic Algebraic Operators Division is not an essential op; just a useful shorthand. – (Also true of joins, but joins are so common that systems implement joins specially.) Idea: For A/B, compute all x values that are not `disqualified’ by some y value in B. – x value is disqualified if by attaching y value from B, we obtain an xy tuple that is not in A. Disqualified x values: A/B: all disqualified tuples

18 CENG 35118 More Examples of Relational Algebra Given: Sailors (), Boats (), Reserves () schemas

19 CENG 35119

20 CENG 35120 Find names of sailors who’ve reserved boat #103  Solution 1:  Solution 2:  Solution 3 :  sname bid servesSailors((Re ) )  103  (,)Temp Sailors21  sname bid serves Sailors ((Re ))  103

21 CENG 35121 Find names of sailors who’ve reserved a red boat  Information about boat color is only available in Boats; so need an extra join:  A more efficient solution :  A query optimizer can find this given the first solution!  sname colorred BoatsservesSailors(( '' )Re)   sname sidbidcolorred BoatssSailors((( '' )Re)) 

22 CENG 35122 Find sailors who’ve reserved a red or a green boat Can identify all red or green boats, then find sailors who’ve reserved one of these boats:  Can also define Tempboats using union! (How?)  What happens if is replaced by in this query?  sname Tempboats Reserves Sailors ()

23 CENG 35123 Find sailors who’ve reserved a red and a green boat Previous approach won’t work! Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors):  (,(( '' ) )) Tempred sidcolorred Boats Reserves   sname TempredTempgreenSailors (())   (,(( '' ) )) Tempgreen sidcolorgreen Boats Reserves 

24 CENG 35124 Find the names of sailors who’ve reserved all boats  Uses division; schemas of the input relations to / must be carefully chosen:  To find sailors who’ve reserved all ‘Interlake’ boats:.....  sname TempsidsSailors ()

25 CENG 35125 More Examples  Find the color of the boat reserved by Lubber  Select Lubber Sailor, join with Reserves, join the result with Boats, apply projection on color  Find the names of sailors who’ve reserved at least one boat  Take the join of Sailors and Reserves, apply projection on name  Find the names of sailors who have reserved at least two boats  Join Sailors and Reserves projected on needed columns into T1, Take join of T1 with itself renaming the attributes into T2, Select the rows with the same sid but different bid, applying projection on the sailor name.

26 CENG 35126 More Examples  Find the Sid of the sailors with age over 20 who have not reserved a red boat  Select the Sailors reserving red boats into S1, select Sailors with age over 20 into S2, Find sailor in S2 not in S1, projecting on sid.

27 Database State for COMPANY All examples discussed below refer to the COMPANY database shown here. CENG 35127

28 Examples of Queries in Relational Algebra Retrieve the name and address of all employees who work for the ‘Research’ department. CENG 35128

29 Examples of Queries in Relational Algebra Retrieve the name and address of all employees who work for the ‘Research’ department. RESEARCH_DEPT   DNAME=’Research’ (DEPARTMENT) RESEARCH_EMPS  (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE EMPLOYEE) RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS) CENG 35129

30 Examples of Queries in Relational Algebra Retrieve the names of employees who have no dependents. CENG 35130

31 Examples of Queries in Relational Algebra Retrieve the names of employees who have no dependents. ALL_EMPS   SSN (EMPLOYEE) EMPS_WITH_DEPS ( SSN )   ESSN ( DEPENDENT ) EMPS_WITHOUT_DEPS  (ALL_EMPS - EMPS_WITH_DEPS) RESULT   LNAME, FNAME (EMPS_WITHOUT_DEPS EMPLOYEE) CENG 35131

32 Relational Calculus A relational calculus expression creates a new relation, which is specified in terms of variables that range over rows of the stored database relations (in tuple calculus) or over columns of the stored relations (in domain calculus). In a calculus expression, there is no order of operations to specify how to retrieve the query result—a calculus expression specifies only what information the result should contain. This is the main distinguishing feature between relational algebra and relational calculus. Relational calculus is considered to be a nonprocedural language. This differs from relational algebra, where we must write a sequence of operations to specify a retrieval request; hence relational algebra can be considered as a procedural way of stating a query. CENG 35132

33 Tuple Relational Calculus The tuple relational calculus is based on specifying a number of tuple variables. Each tuple variable usually ranges over a particular database relation, meaning that the variable may take as its value any individual tuple from that relation. A simple tuple relational calculus query is of the form {t | COND(t)} where t is a tuple variable and COND (t) is a conditional expression involving t. The result of such a query is the set of all tuples t that satisfy COND (t). Example: To find the first and last names of all employees whose salary is above $50,000, we can write the following tuple calculus expression: {t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>50000} CENG 35133

34 The Existential and Universal Quantifiers Two special symbols called quantifiers can appear in formulas; these are the universal quantifier   ) and the existential quantifier   ). Informally, a tuple variable t is bound if it is quantified, meaning that it appears in an (  t) or (  t) clause; otherwise, it is free. CENG 35134

35 Example Query Using Existential Quantifier Retrieve the name and address of all employees who work for the ‘Research’ department. Query : {t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) and   d) (DEPARTMENT(d) and d.DNAME=‘Research’ and d.DNUMBER=t.DNO) } CENG 35135


Download ppt "Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports."

Similar presentations


Ads by Google