Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.

Similar presentations


Presentation on theme: "Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval."— Presentation transcript:

1 Relational Algebra Ch. 7.4 – 7.6 John Ortiz

2 Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval of data from a database.  Relational QLs are simple & powerful.  Strong formal foundation based on logic.  Allows for much optimization.  Query languages != programming languages!  Not intended for complex calculations.  Support easy, efficient access to large data sets.

3 Lecture 4Relational Algebra3 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 understanding meanings of queries and for representing execution plans. 2.Relational calculus: lets users describe what they want, rather than how to compute it.

4 Lecture 4Relational Algebra4 Preliminaries  A query is applied to relation instances, and the result of a query is also a relation instance.  Schemas of input & result relations are fixed (determined by relations & query language constructs).  A query is specified against schemas (regardless of instances).  Attributes may be referenced either by names or by positions (two notation systems).

5 Lecture 4Relational Algebra5  Basic Operations:  Selection (  ): choose a subset of rows.  Projection (  ): choose a subset of columns.  Cross Product (  ): Combine two tables.  Union (  ): unique tuples from either table.  Set difference (  ): tuples in R1 not in R2.  Renaming (  ): change names of tables & columns  Additional Operations (for convenience):  Intersection, joins (very useful), division, outer joins, aggregate functions, etc.

6 Lecture 4Relational Algebra6 Selection  Format:  selection-condition (R). Choose tuples that satisfy the selection condition.  Result has identical schema as the input.  Major = ‘CS’ (Students) Students Result  Selection condition is a Boolean expression including =, ,, , and, or, not.

7 Lecture 4Relational Algebra7 Projection  Format:  attribute-list (R). Retain only those columns in the attribute-list.  Result must eliminate duplicates.  Major (Students) StudentsResult  Operations can be composed.  Name, GPA (  Major = ‘CS’ (Students))

8 Lecture 4Relational Algebra8 Cross Product  Format: R1  R2. Each row of R1 is paired with each row of R2.  Result schema consists of all attributes of R1 followed by all attributes of R2.  Problem: Columns may have identical names. Use notation R.A, or renaming attributes.  Only some rows make sense. Often need a selection to follow.

9 Lecture 4Relational Algebra9 Example of Cross Product Students  Awards Students Awards

10 Lecture 4Relational Algebra10 Renaming  Format:  S (R) or  S(A1, A2, …) (R): change the name of relation R, and names of attributes of R  CS_Students (  Major = ‘CS’ (Students)) Students CS_Students

11 Lecture 4Relational Algebra11 Union, Intersection, Set Difference  Format: R1  R2 (R1  R2, R1  R2). Return all tuples that belong to either R1 or R2 (to both R1 and R2; to R1 but not to R2).  Requirement: R1 and R2 are union compatible.  With same number of attributes.  Corresponding attributes have same domains.  Schema of result is identical to that of R1. May need renaming.  Duplicates are eliminated.

12 Lecture 4Relational Algebra12 Examples of Set Operations TAs RAs TAs  RAs TAs  RAs TAs  RAs

13 Lecture 4Relational Algebra13 Joins  Theta Join.  Format: R1 join-condition R2.  Returns tuples in  join-condition (R1  R2)  Equijoin.  Same as Theta Join except the join- condition contains only equalities.  Natural Join.  Same as Equijoin except that equality conditions are on common attributes and duplicate columns are eliminated.

14 Lecture 4Relational Algebra14 Examples of Joins  Theta Join. Students Students.Age<=Profs.Age Profs Students Profs Result

15 Lecture 4Relational Algebra15 Examples of Joins (cont.)  Equijoin. Students Prof=PID AND Name=Pname Profs Result  Natural Join. Students Profs Result

16 Lecture 4Relational Algebra16 Some Questions About Joins *  What is the result of R1 R2 if they do not have a common attribute?  What is the result of R R?  Consider relations Students(SSN, Name, GPA, Major, Age, PSSN) Profs(PSSN, Name, Office, Age, Dept)  Which type of join should be used to find pairs of names of students and their advisors?  Can a natural join be used? How?

17 Lecture 4Relational Algebra17 Division  Format: R1  R2. Restriction: Every attribute in R2 is in R1.  For R1(A1,..., An, B1,..., Bm)  R2(B1,..., Bm) and T =  A1,..., An (R1), Return the subset of T, say W, such that every tuple in W  R2 is in R1.  W is the largest subset of T, such that, (W  R2)  R1

18 Lecture 4Relational Algebra18 An Example of Division Takes  CS_Req Takes Result CS_Req  What is the meaning of this expression?

19 Lecture 4Relational Algebra19 Grouping & Aggregate Functions  Format: group_attributes F aggregate_functions ( r )  Partition a relation into groups  Apply aggregate function to each group  Output grouping and aggregation values, one tuple per group  Ex: Major F count(SID), avg(GPA) (Students) Students Result

20 Lecture 4Relational Algebra20 Dangling Tuples in Join  Usually, only a subset of tuples of each relation will actually participate in a join.  Tuples of a relation not participating in a join are dangling tuples.  How do we keep dangling tuples in the result of a join? (Why do we want to do that?)  Use null values to indicate a “no-join” situation.

21 Lecture 4Relational Algebra21 Outer Joins  Left Outer Join.  Format: R1 R2. Similar to a natural join but keep all dangling tuples of R1.  Right Outer Join.  Format: R1 R2. Similar to a natural join but keep all dangling tuples of R2.  (Full) Outer Join.  Format: R1 R2. Similar to a natural join but keep all dangling tuples of both R1 & R2.  Can also have Theta Outer Joins.

22 Lecture 4Relational Algebra22 Examples of Outer Joins  Left Outer Join. Students Awards Students Awards Result

23 Lecture 4Relational Algebra23 Relational Algebra Exercises  Find the result of these expressions.  R S  R R.C=S.C S   B,E ((  B,C R) (  E<7 S))  (  A,B R) -  S(A,B) (  D,C S) RS

24 Lecture 4Relational Algebra24 Queries In Relational Algebra Consider the following database schema: Students(SSN, Name, GPA, Age, MajorDept) Enrollment(SSN, CourseNo, Grade) Courses(CourseNo, Title, DName) Departments(DName, Location, Phone)  Two methods:  Use temporary relations.  One expression per query.

25 Lecture 4Relational Algebra25 Queries In Relational Algebra  List student name and course title such that the student has an A in the course and the course is not offered by the student’s major department.  Find those students who got an A in any course.  Find the department of the students and the courses.  Find the final answer.

26 Lecture 4Relational Algebra26 Queries In Relational Algebra  Find names of students who have the highest GPA.  Find students whose GPA are lower than some other students.  Find students not in the first group.  Find the names of those in the second group.

27 Lecture 4Relational Algebra27 Queries In Relational Algebra  Find names of students who took every course taken by John Green.  Find names of students who never had a grade lower than C.  Find names of departments that offer courses taken by both Mary Smith and John Green.  Find SSNs and names of students who major in CS and take CS544.

28 Lecture 4Relational Algebra28 Queries In Relational Algebra  List name and id of CS students along with titles of courses that they take.  Students.SSN, Name, Title (  Students.MajorDept = ‘CS’ and Students.SSN = Enrollment.SSN and Enrollment.CourseNo = Courses.CourseNo (Students  Enrollment  Courses)) =  Students.SSN,Name,Title ((  MajorDept=‘CS’ (Students) Enrollment) Enrollment.CourseNo=Courses.CourseNo Courses) =  Students.SSN,Name,Title (  MajorDept=‘CS’ (Students) Students.SSN=Enrollment.SSN (Enrollment Courses))

29 Lecture 4Relational Algebra29 Summary  Relational model provides simple yet powerful formal query languages.  Relational algebra is procedural and used for internal representation of queries.  Several ways to express a given query. DBMS should choose the most efficient plan.  Any language able to express all relational algebra queries is relational complete.

30 Lecture 4Relational Algebra30 Summary (cont.) Lots useful properties.   C1 (  C2 (R)) =  C2 (  C1 (R)) =  C1 and C2 (R)   L1 (  L2 (R)) =  L1 (R), if L1  L2  R1  R2 = R2  R1  R1  (R2  R3) = (R1  R2)  R3  R1 R2 = R2 R1  R1 (R2 R3) = (R1 R2) R3

31 Lecture 4Relational Algebra31 Look Ahead  Next topic: Translation form ER/EER to relational model  Read from the textbook:  Chapter 14.1 – 14.2


Download ppt "Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval."

Similar presentations


Ads by Google