Ra Revision

864 views

Published on

All Education Slides

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
864
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
36
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Ra Revision

  1. 1. Database Systems: Chapter 6: Relational Algebra Dr. Taysir Hassan Abdel Hamid Lecturer, IS Department Faculty of Computer and Information Assiut University Contact: [email_address] March 30, 2008
  2. 2. Operations in Relational Algebra <ul><li>Group one : includes set operations from mathematical set theory: </li></ul><ul><li>UNION, INTERSECTION, SET DIFFERENCE, AND CARTESIAN PRODUCT. </li></ul><ul><li>Group two : includes operations specifically for relational databases: </li></ul><ul><li>SELECT, PROJECT, AND JOIN </li></ul><ul><li>Common database requests cannot be performed with the original relational operations. </li></ul>
  3. 3. Unary Relational Operations SELECT <ul><li>SELECT operation is used to select a subset of the tuples from a relation that satisfies a selection condition. </li></ul><ul><li>One can consider the SELECT operation to be a filter that keeps only those tuples that satisfy a qualifying condition. </li></ul><ul><li>SELECT can also be visualized as a horizontal partition of the relation into two sets of tuples. </li></ul>
  4. 4. Select Operator <ul><li>Produce table containing subset of rows of argument table satisfying condition </li></ul><ul><li>Example: </li></ul>1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps 1123 John 123 Main stamps 9876 Bart 5 Pine St stamps Person Id Name Address Hobby <ul><ul><ul><li>Id Name Address Hobby </li></ul></ul></ul>
  5. 5. Selection Condition <ul><li>Operators: <,  ,  , >, =,  </li></ul><ul><li>Simple selection condition: </li></ul><ul><ul><li><attribute> operator <constant> </li></ul></ul><ul><ul><li><attribute> operator <attribute> </li></ul></ul><ul><li><condition> AND <condition> </li></ul><ul><li><condition> OR <condition> </li></ul><ul><li>NOT <condition> </li></ul><ul><li>NOTE: Each attribute in condition must be one of the attributes of the relation name </li></ul>
  6. 6. <ul><li> Id>3000 OR Hobby=‘hiking’ (Person) </li></ul><ul><li> Id>3000 AND Id <3999 (Person) </li></ul><ul><li> NOT(Hobby=‘hiking’) (Person) </li></ul><ul><li> Hobby  ‘hiking’ (Person) </li></ul><ul><li> (Id > 3000 AND Id < 3999) OR Hobby  ‘hiking’ (Person) </li></ul>Selection Condition - Examples
  7. 7. <ul><li>Find all employees with salary more than $40,000. </li></ul><ul><li> Salary > 40000 (Employee) </li></ul>
  8. 8. Projection <ul><li>Extracts some of the columns from a relation. </li></ul><ul><ul><ul><li>SexSalary :=  (EMPLOYEE, (SEX, SALARY)) </li></ul></ul></ul><ul><ul><li>No attribute may occur more than once. </li></ul></ul><ul><ul><li>Duplicate will be removed. </li></ul></ul><ul><li>Projection and selection combined. </li></ul><ul><ul><ul><li> (  (EMPLOYEE, CITY = “LONDON”), (NAME,SSN)) </li></ul></ul></ul><ul><ul><li>This does a selection followed by a projection. </li></ul></ul><ul><ul><li>The DBMS may re-organise this for faster retrieval. </li></ul></ul>
  9. 9. Union <ul><li>Produce a relation which combines two relations by containing all of the tuples from each - removing duplicates. </li></ul><ul><li>The two relations must be &quot; union compatible &quot; i.e. have the same number of attributes drawn from the same domain (but maybe having different names). </li></ul><ul><ul><ul><li>LondonOrRich: =  EMPLOYEE, CITY = “LONDON”)  (EMPLOYEE, SALARY > 60K) </li></ul></ul></ul><ul><li>If attribute names differ, the names from the first one are taken. </li></ul><ul><li>Same can be done using disjuncts! </li></ul>
  10. 10. Intersection <ul><li>Similar to union but returns tuples that are in both relations. </li></ul><ul><ul><ul><li>FemalesInLondon :=  EMPLOYEE, CITY = “LONDON”)  (EMPLOYEE, SEX = “F”) </li></ul></ul></ul><ul><li>Same can be done with conjuncts! </li></ul>
  11. 11. Difference <ul><li>Similar to union but returns tuples that are in the first relation but not the second. </li></ul><ul><ul><ul><ul><li>NonLocals := EMPLOYEE - LOCALS </li></ul></ul></ul></ul><ul><li>Intersection and difference both require union compatibility. </li></ul><ul><li>Intersection and difference use column names from the first relation. </li></ul>
  12. 12. Union Compatible Relations <ul><li>Two relations are union compatible if </li></ul><ul><ul><li>Both have same number of columns </li></ul></ul><ul><ul><li>Names of attributes are the same in both </li></ul></ul><ul><ul><li>Attributes with the same name in both relations have the same domain </li></ul></ul><ul><li>Union compatible relations can be combined using union, intersection, and set difference </li></ul>
  13. 13. Example Tables: Person (SSN, Name, Address, Hobby) Professor (Id, Name, Office, Phone) are not union compatible. However
  14. 14. Cartesian Product <ul><li>If R and S are two relations, R  S is the set of all concatenated tuples <x,y>, where x is a tuple in R and y is a tuple in S </li></ul><ul><ul><li>( R and S need not be union compatible) </li></ul></ul><ul><li>R  S is expensive to compute: </li></ul><ul><ul><li>Factor of two in the size of each row </li></ul></ul><ul><ul><li>Quadratic in the number of rows </li></ul></ul>a b c d a b c d x1 x2 y1 y2 x1 x2 y1 y2 x3 x4 y3 y4 x1 x2 y3 y4 x3 x4 y1 y2 R S x3 x4 y3 y4 R  S
  15. 15. Renaming <ul><li>Result of expression evaluation is a relation </li></ul><ul><li>Attributes of relation must have distinct names. This is not guaranteed with Cartesian product </li></ul><ul><ul><li>e.g., suppose in previous example a = c </li></ul></ul><ul><li>Renaming operator tidies this up. To assign the names A 1 , A 2 ,… A n to the attributes of the n column relation produced by expression use expression [A 1 , A 2 , … A n ] </li></ul>
  16. 16. Example This is a relation with 4 attributes: StudId, SCrsCode, ProfId, PCrsCode
  17. 17. Derived Operation: Join The expression :  join-condition ´ (R  S) where join-condition ´ is a conjunction of terms: A i oper B i in which A i is an attribute of R, B i is an attribute of S, and oper is one of =, <, >,   ,  , is referred to as the (theta) join of R and S and denoted: R join-condition S Where join-condition and join-condition ´ are (roughly) the same …
  18. 18. Join and Renaming <ul><li>Problem : R and S might have attributes with the same name – in which case the Cartesian product is not defined </li></ul><ul><li>Solution : </li></ul><ul><ul><li>Rename attributes prior to forming the product and use new names in join-condition ´ . </li></ul></ul><ul><ul><li>Common attribute names are qualified with relation names in the result of the join </li></ul></ul>
  19. 19. Theta Join – Example <ul><li>Output the names of all employees that earn </li></ul><ul><li>more than their managers. </li></ul><ul><li> Employee.Name (Employee MngrId=Id AND Salary>Salary </li></ul><ul><li>Manager) </li></ul><ul><li>The join yields a table with attributes: </li></ul><ul><li>Employee.Name, Employee.Id, Employee.Salary, MngrId </li></ul><ul><li>Manager.Name, Manager.Id, Manager.Salary </li></ul>
  20. 20. Equijoin Join - Example <ul><li> Name,CrsCode ( Student </li></ul><ul><li>Id=StudId  Grade=‘A’ (Transcript)) </li></ul><ul><li>Id Name Addr Status </li></ul><ul><li>111 John ….. ….. </li></ul><ul><li>222 Mary ….. ….. </li></ul><ul><li>333 Bill ….. ….. </li></ul><ul><li>444 Joe ….. ….. </li></ul><ul><li>StudId CrsCode Sem Grade </li></ul><ul><li>111 CSE305 S00 B </li></ul><ul><li>222 CSE306 S99 A </li></ul><ul><li>333 CSE304 F99 A </li></ul><ul><li>Mary CSE306 </li></ul><ul><li>Bill CSE304 </li></ul><ul><li>The equijoin is commonly </li></ul><ul><li>used since it combines related </li></ul><ul><li>data in different relations. </li></ul><ul><li>Student </li></ul><ul><li>Transcript </li></ul><ul><li>Equijoin: Join condition is a conjunction of equalities . </li></ul>
  21. 21. Natural Join <ul><li>Special case of equijoin: </li></ul><ul><ul><li>join condition equates all and only those attributes with the same name (condition doesn’t have to be explicitly stated) </li></ul></ul><ul><ul><li>duplicate columns eliminated from the result </li></ul></ul><ul><li>Transcript (StudId, CrsCode, Sem, Grade) </li></ul><ul><li>Teaching (ProfId, CrsCode, Sem) </li></ul><ul><li>Transcript </li></ul><ul><li>Teaching = </li></ul><ul><li> StudId, Transcript.CrsCode, Transcript.Sem, Grade, ProfId ( </li></ul><ul><li>Transcript </li></ul><ul><li>CrsCode=CrsCode AND Sem=Sem Teaching) </li></ul><ul><li>[StudId, CrsCode, Sem, Grade, ProfId ] </li></ul>
  22. 22. Natural Join (con’t) <ul><li>More generally: </li></ul>R S =  attr-list (  join-cond (R × S) ) where attr-list = attributes (R)  attributes (S) (duplicates are eliminated) and join-cond has the form: A 1 = A 1 AND … AND A n = A n where {A 1 … A n } = attributes(R)  attributes(S)
  23. 23. Natural Join Example <ul><li>List all Id’s of students who took at least two different courses: </li></ul> StudId (  CrsCode  CrsCode2 ( Transcript Transcript [StudId, CrsCode2, Sem2, Grade2])) (don’t join on CrsCode, Sem, and Grade attributes)
  24. 24. Division <ul><li>Goal: Produce the tuples in one relation, r, that match all tuples in another relation, s </li></ul><ul><ul><li>r (A 1 , …A n , B 1 , …B m ) </li></ul></ul><ul><ul><li>s (B 1 …B m ) </li></ul></ul><ul><ul><li>r/s , with attributes A 1 , …A n , is the set of all tuples <a> such that for every tuple <b> in s, <a,b> is in r </li></ul></ul><ul><li>Can be expressed in terms of projection, set difference, and cross-product </li></ul>
  25. 25. Division (con’t)
  26. 26. Division - Example <ul><li>List the Ids of students who have passed all courses that were taught in spring 2000 </li></ul><ul><li>Numerator : StudId and CrsCode for every course passed by every student </li></ul><ul><ul><li> StudId, CrsCode (  Grade  ‘F’ (Transcript) ) </li></ul></ul><ul><li>Denominator : CrsCode of all courses taught in spring 2000 </li></ul><ul><ul><li> CrsCode (  Semester=‘S2000’ (Teaching) ) </li></ul></ul><ul><li>Result is numerator/denominator </li></ul>

×