Lecture 06 relational algebra and calculus


Published on

Published in: Education
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Lecture 06 relational algebra and calculus

  1. 1. Lecture 6 Relational Algebra And Calculus
  2. 2. Data Manipulation Languages <ul><li>In order for a database to be useful, it should be possible to store and retrieve information from it. This is the role of the data manipulation language. </li></ul><ul><li>One of the attractions of the relational data model is that it comes with a well-defined data manipulation language. </li></ul>
  3. 3. Types of DML <ul><li>Two types of data manipulation languages </li></ul><ul><ul><li>Navigational (procedural) </li></ul></ul><ul><ul><ul><li>The query specifies (to some extent) the strategy used to find the desired result e.g. relational algebra. </li></ul></ul></ul><ul><ul><li>Non-navigational(non-procedural) </li></ul></ul><ul><ul><ul><li>The query only specifies what data is wanted, not how to find it e.g. relational calculus. </li></ul></ul></ul>
  4. 4. Relational Algebra <ul><li>Codd defined a number of algebraic operations for the relational model. </li></ul><ul><li>Unary operations take as input a single table and produce as output another table. </li></ul><ul><li>Binary operations take as input two tables and produce as output another table. </li></ul>
  5. 5. Types of Relational <ul><li>Traditional Set Operation </li></ul><ul><ul><li>Union </li></ul></ul><ul><ul><li>Intersection </li></ul></ul><ul><ul><li>Difference </li></ul></ul><ul><ul><li>Cartesian product </li></ul></ul><ul><li>Special Operations </li></ul><ul><ul><li>Selection </li></ul></ul><ul><ul><li>Projection </li></ul></ul><ul><ul><li>Join </li></ul></ul><ul><ul><li>Division </li></ul></ul>
  6. 6. Unary Operations: Select <ul><li>Select produces a table that only contains the tuples that satisfy a particular condition, in other words a “horizontal” subset. </li></ul><ul><li>Appearance: </li></ul><ul><ul><li> C (R)  Sigma </li></ul></ul><ul><ul><li>where C is a selection condition </li></ul></ul><ul><ul><li>and R is the relation over which the selection takes place </li></ul></ul>
  7. 7. Example of Select <ul><li>Student </li></ul><ul><li>sid name addr </li></ul><ul><li>123 Fred 3 Oxford </li></ul><ul><li>345 John 6 Hope Rd. </li></ul><ul><li>567 Ann 5 Garden </li></ul><ul><li> sid > 300 (Student) yields </li></ul><ul><li>345 John 6 Hope Rd. </li></ul><ul><li>567 Ann 5 Garden </li></ul>
  8. 8. Unary Operations: Project <ul><li>Project produces a table consisting of only some of the attributes. It creates a “vertical” subset. </li></ul><ul><li>Note that a project eliminates duplicates. </li></ul><ul><li>Appearance: </li></ul><ul><li>П A (R) П =Pi </li></ul><ul><ul><li>where A is a set of attributes of R </li></ul></ul><ul><ul><li>and R is the relation over which the project takes place. </li></ul></ul>
  9. 9. Example of Project <ul><li>Enrol </li></ul><ul><li>sid cid grade </li></ul><ul><li>123 CS51T 76 </li></ul><ul><li>234 CS52S 50 </li></ul><ul><li>345 CS52S 55 </li></ul><ul><li>П cid (Enrol) yields </li></ul><ul><li>CS51T </li></ul><ul><li>CS52S </li></ul>
  10. 10. Binary Operations <ul><li>Two relations are (union) compatible if they have the same set of attributes. </li></ul><ul><li>Example, one table may represent suppliers in one country, while another table with same schema represents suppliers in another country. </li></ul><ul><li>For the union, intersection and set-difference operations, the relations must be compatible. </li></ul>
  11. 11. Union, Intersection, Set-difference <ul><li>R1   R2 </li></ul><ul><ul><li>The union is the table comprised of all tuples in R1 or R2. </li></ul></ul><ul><li>R1  R2 </li></ul><ul><ul><li>The intersection is the table comprised of all tuples in R1 and R2 </li></ul></ul><ul><li>R1 - R2 </li></ul><ul><ul><li>The set-difference between R1 and R2 is the table consisting of all tuples in R1 but not in R2. </li></ul></ul>
  12. 12. Cartesian Product <ul><li>R1  R2 </li></ul><ul><ul><li>The Cartesian product is the table consisting of all tuples formed by concatenating each tuple in R1 with a tuple in R2, for all tuples in R2. </li></ul></ul>
  13. 13. Example of a Cartesian Product <ul><li>R1 A B </li></ul><ul><li>1 x </li></ul><ul><li>2 y </li></ul><ul><li>R2 C D </li></ul><ul><li>a s </li></ul><ul><li>b t </li></ul><ul><li>c u </li></ul><ul><li>R1  R2 A B C D </li></ul><ul><li>1 x a s </li></ul><ul><li>1 x b t </li></ul><ul><li>1 x c u </li></ul><ul><li>2 y a s </li></ul><ul><li>2 y b t </li></ul><ul><li>2 y c u </li></ul>
  14. 14. Natural Join <ul><li>R1 R2 </li></ul><ul><ul><li>Assume R1 and R2 have attributes A in common. Natural join is formed by concatenating all tuples from R1 and R2 with same values for A, and dropping the occurrences of A in R2 </li></ul></ul><ul><ul><li>R1 R2 = П A’ (  C ( R1  R2 )) </li></ul></ul><ul><ul><ul><li>where C is the condition that the values for R1 and R2 are the same for all attributes in A and A’ is all attributes in R1 and R2 apart from the occurrences of A in R2. </li></ul></ul></ul><ul><ul><ul><li>hence, natural join is syntactic sugar </li></ul></ul></ul>
  15. 15. Example of a Natural Join I <ul><li>Course </li></ul><ul><li>cid title eid </li></ul><ul><li>CS51T DBMS 123 </li></ul><ul><li>CS52S OS 345 </li></ul><ul><li>CS52T Networking 345 </li></ul><ul><li>CS51S ES 456 </li></ul><ul><li>Instructor </li></ul><ul><li>eid ename </li></ul><ul><li>123 Rao </li></ul><ul><li>345 Allen </li></ul><ul><li>456 Mansingh </li></ul>
  16. 16. Example of a Natural Join II <ul><li>Course Instructor </li></ul><ul><li>cid title eid ename </li></ul><ul><li>CS51T DBMS 123 Rao </li></ul><ul><li>CS52S OS 345 Allen </li></ul><ul><li>CS52T Net... 345 Allen </li></ul><ul><li>CS51S ES 456 Mansingh </li></ul>
  17. 17. Division <ul><li>R1  R2 </li></ul><ul><ul><li>Assume that the schema for R2 is a proper subset of the one for R1. </li></ul></ul><ul><ul><li>We form the division by </li></ul></ul><ul><ul><ul><li>Ordering the tuples in R1 so that all the tuples with the same value for the non-common attributes are grouped together. </li></ul></ul></ul><ul><ul><ul><li>Each group contributes a tuple to the result if the group’s values on the common attributes form a superset of the values of these attributes in R2. </li></ul></ul></ul>
  18. 18. Example of Division I <ul><li>Enrol cid sid grade </li></ul><ul><li>CS51T 123 A </li></ul><ul><li>CS52S 123 A </li></ul><ul><li>CS51T 234 C </li></ul><ul><li>CS52S 234 B </li></ul><ul><li>CS51T 345 C </li></ul><ul><li>CS52S 345 C </li></ul><ul><li>Temp sid grade </li></ul><ul><li>123 A </li></ul><ul><li>234 B </li></ul>
  19. 19. Example of Division II <ul><li>Enrol cid sid grade </li></ul><ul><li>CS51T 123 A </li></ul><ul><li>CS51T 234 C </li></ul><ul><li>CS51T 345 C </li></ul><ul><li>CS52S 123 A </li></ul><ul><li>CS52S 234 B </li></ul><ul><li>CS52S 345 C </li></ul><ul><li>Enrol  Temp cid </li></ul><ul><li> CS52S </li></ul><ul><li>Thus, the division gives all courses for which 123 got an A and 234 a B. </li></ul>
  20. 20. Assignment <ul><li>Allows the expression to be written in parts. </li></ul><ul><li>Assigns the part to a temporary variable. </li></ul><ul><li>This variable can be used in subsequent expressions. </li></ul><ul><li>E.g. </li></ul><ul><ul><li> sid (  title = ‘DBMS’ (Enrol Course) </li></ul></ul><ul><ul><li>Could be re-written as: </li></ul></ul><ul><ul><ul><li>r Enrol Course </li></ul></ul></ul><ul><ul><ul><li> sid (  title = ‘DBMS’ (r)) </li></ul></ul></ul>
  21. 21. Database Modification <ul><li>Insert </li></ul><ul><ul><li>r r E </li></ul></ul><ul><ul><li>e.g. </li></ul></ul><ul><ul><ul><li>Course Course {(‘CS51T’,’DBMS’)} </li></ul></ul></ul><ul><li>Delete </li></ul><ul><ul><li>r r - E </li></ul></ul><ul><ul><li>e.g. </li></ul></ul><ul><ul><ul><li>Student Student -  sid=‘1’ (Student) </li></ul></ul></ul><ul><li>Update </li></ul><ul><ul><li>r  F 1 ,F 2 ,…,F n (r) </li></ul></ul><ul><ul><li>e.g. </li></ul></ul><ul><ul><ul><li>Enrol  sid,cid,grade grade + 2 (Enrol) </li></ul></ul></ul> 
  22. 22. Examples <ul><li>Assume the following schema: </li></ul><ul><li>Student( sid ,sname,saddr) </li></ul><ul><ul><li>Course( cid ,title,lid) </li></ul></ul><ul><ul><li>Enrol( sid, cid , grade) </li></ul></ul><ul><ul><li>Lecturer( lid ,lname,deptname) </li></ul></ul><ul><li>Query 1: Find the name of all students that have taken the course entitled ‘Expert Systems’. </li></ul><ul><li>Query 2: Find the titles of all courses that student ‘Mark Smith’ has done. </li></ul><ul><li>Query 3: Find the id of students that have enrolled in all the courses that lecturer with id. = ‘234’ has taught. </li></ul><ul><li>Query 4: Find the highest grade for ‘CS51T’. </li></ul>
  23. 23. Relational Calculus <ul><li>A relational calculus expression defines a new relation in terms of other relations. </li></ul><ul><li>A tuple variable ranges over a named relation. So, its values are tuples from that relation. </li></ul><ul><li>Example: </li></ul><ul><ul><li>Get grades for CS51T </li></ul></ul><ul><ul><li> e(Enrol) </li></ul></ul><ul><ul><li>{<e.grade>: e.cid = ‘CS51T’ } </li></ul></ul>
  24. 24. Basic Syntax for Relational Calculus Expressions <ul><li> r(R),…,  s(S) </li></ul><ul><li>{ <target> : predicate} </li></ul><ul><li>where </li></ul><ul><ul><li>R,..,S are tables </li></ul></ul><ul><ul><li>r,..,s are tuple variables </li></ul></ul><ul><ul><li>target specifies the attributes of the resulting relation </li></ul></ul><ul><ul><li>predicate is a formula giving a condition that tuples must satisfy to qualify for the resulting relation. </li></ul></ul>
  25. 25. The Predicate <ul><li>Predicate is constructed from </li></ul><ul><ul><li>attribute names </li></ul></ul><ul><ul><li>constants </li></ul></ul><ul><ul><li>comparison operators </li></ul></ul><ul><ul><li>       </li></ul></ul><ul><ul><li>logical connectives </li></ul></ul><ul><ul><li>     </li></ul></ul><ul><ul><li>quantified tuple variables </li></ul></ul><ul><ul><li> t(R),  t(R) </li></ul></ul>
  26. 26. Examples of Relational Calculus <ul><li>Example 2 </li></ul><ul><ul><li>Get names and grades for students enrolled in CS51T </li></ul></ul><ul><ul><ul><li> e(Enrol), s(Student) </li></ul></ul></ul><ul><ul><ul><li>{<s.name, e.grade>: </li></ul></ul></ul><ul><ul><ul><li> e.cid = ‘CS51T’  </li></ul></ul></ul><ul><ul><ul><li> s.sid = e.sid} </li></ul></ul></ul><ul><li>In relation algebra </li></ul><ul><ul><li>П cid, name(  CID =‘ CS51T’ (Grade Student)) </li></ul></ul>
  27. 27. Example 3 <ul><li>Give the names of all students who got at least one A. </li></ul><ul><ul><li> s(Student) </li></ul></ul><ul><ul><li>{<s.name>: </li></ul></ul><ul><ul><li>  e(Enrol) </li></ul></ul><ul><ul><li> (e.grade = ‘A’  </li></ul></ul><ul><ul><li> s.sid = e.sid)} </li></ul></ul><ul><li>Tuple variables not mentioned in the target list must be bound in the predicate. </li></ul>
  28. 28. Example 4 <ul><li>Get the names of all students who only got A’s </li></ul><ul><ul><li> s(Student) </li></ul></ul><ul><ul><li>{<s.name>: </li></ul></ul><ul><ul><li>  e(Enrol)( s.sid = e.sid  e.grade = ‘A’) </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li>  e2(Enrol) (s.sid = e2.sid)} </li></ul></ul>
  29. 29. Example 5 <ul><li>Get the names of all students who got an A and a B </li></ul><ul><ul><li> s(Student) </li></ul></ul><ul><ul><li>{<s.name>: </li></ul></ul><ul><ul><li>  e(Enrol) (e.grade = ‘B’  </li></ul></ul><ul><ul><li> s.sid = e.sid) </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li>  e2(Enrol) (e2.grade = ‘A’  </li></ul></ul><ul><ul><li> s.sid = e2.sid)} </li></ul></ul>
  30. 30. Example 6 <ul><li>Get the course titles and names for the courses for which the student did not get an A </li></ul><ul><ul><li> c(Course), s(Student) </li></ul></ul><ul><ul><li>{<s.name, c.title>: </li></ul></ul><ul><ul><li> g(Enrol) s.sid = g.sid  </li></ul></ul><ul><ul><li> g.cid = c.cid  </li></ul></ul><ul><ul><li> g.grade  ‘A’} </li></ul></ul>