RDBMS Algebra

791 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
791
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

RDBMS Algebra

  1. 1. The Relational Algebra
  2. 2. Mathematical Relations For two sets D1 and D2, the Cartesian product, D1 X D2 , is the set of all ordered pairs in which the first element is from D1 and the second is from D2 Example D1 = {1,3} and D2 = {a,b,c} D1 X D2 = {(1,a), (1,b), (1,c), (3,a), (3,b), (3,c)} A relation is any subset of the Cartesian productA relation is any subset of the Cartesian product Example R = {(x,y) | x ∈ D1, y ∈ D2, and y = a} R = {(1,a), (3,a)} Could form Cartesian product of 3 sets; relation is any subset of the ordered triples so formed Could extend to n sets, using n-tuples
  3. 3. Database Relations • A relation schema, R, is a set of attributes A1, A2,…,An with their domains D1, D2,…Dn • A relation r on relation schema R is a set of mappings from the attributes to their domains • r is a set of n-tuples (A1:d1, A2:d2, …, An:dn) such that d1ε D1, d2 εD2 , …, dn εDn – Mapping the data in domain Dn to the attribute An • In a table to represent the relation, list the Ai as column headings, and let the (d1, d2, …dn) become the n-tuples, the rows of the table • The name of the attribute becomes the name of the column
  4. 4. Relational Algebra • Theoretical language with operators that apply to one or two relations to produce another relation • Both operands and results are tables• Both operands and results are tables • Can assign name to resulting table (rename) • SELECT, PROJECT, JOIN allow many data retrieval operations
  5. 5. SELECT Operation • Applied to a single table, returns rows that meet a specified predicate, copying them to new table • Returns a horizontal subset of original table SELECT tableName WHERE condition [GIVING newTableName]SELECT tableName WHERE condition [GIVING newTableName] Symbolically, [newTableName = ] σ predicate (table-name) • Predicate is called theta-condition, as in σθ(table-name) • Result table is horizontal subset of operand • Predicate can have operators <, <=, >, >=, =, <>, ∧(AND), ∨(OR), ¬ (NOT)
  6. 6. SELECT Example SELECT Student WHERE stuId = ‘S1013’ GIVING Result Result = σ STDID=‘S1013’ (Student) Student ResultStudent stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 Result stuId lastName firstName major credits S1013 McCarthy Owen Math 0
  7. 7. SELECT Example SELECT Class WHERE room = ‘H225’ GIVING Answer Answer= σ ROOM=‘H225’ (Class) Class AnswerClass classNumber facId schedule room ART103A F101 MWF9 H221 CSC201A F105 TuThF10 M110 CSC203A F105 MThF12 M110 HST205A F115 MWF11 H221 MTH101B F110 MTuTh9 H225 MTH103C F110 MWF11 H225 Answer classNumber facId schedule room MTH101B F110 MTuTh9 H225 MTH103C F110 MWF11 H225
  8. 8. PROJECT Example PROJECT Student OVER major GIVING Temp Temp = Π major (Student) Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 Temp major History MathS1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 Math Art CSC
  9. 9. PROJECT Example PROJECT Class OVER (facId, room) Π facId, room (Class) Class classNumber facId schedule room ART103A F101 MWF9 H221 CSC201A F105 TuThF10 M110 facId room F101 H221 F105 M110CSC201A F105 TuThF10 M110 CSC203A F105 MThF12 M110 HST205A F115 MWF11 H221 MTH101B F110 MTuTh9 H225 MTH103C F110 MWF11 H225 F105 M110 F115 H221 F110 H225
  10. 10. PROJECT and SELECT Combination Example Want the names and student IDs of History majors SELECT Student WHERE major = ‘History’ GIVING Temp PROJECT Temp OVER (lastName, firstName, stuId) GIVING Result Student stuId lastName firstName major credits S1001 Smith Tom History 90 Temp stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 S1005 Lee Perry History 3 Temp stuId lastName firstName major credits S1001 Smith Tom History 90 S1005 Lee Perry History 3 Result lastName firstName stuId Smith Tom S1001 Lee Perry S1005
  11. 11. Product and Theta-join • Binary operations – apply to two tables • Product: Cartesian product – cross-product of A and B – A TIMES B, written A x B – all combinations of rows of A with rows of B– all combinations of rows of A with rows of B – Degree of result is deg of A + deg of B – Cardinality of result is (card of A) * (card of B) • THETA join: TIMES followed by SELECT A |x|θ B = σθ(A x B)
  12. 12. PRODUCT Example Student X Enroll (Produces 63 tuples) Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 Enroll stuId classNumber grade S1001 ART103A A S1001 HST205A C S1002 ART103A D S1002 CSC201A F S1002 MTH103C B S1010 ART103A S1010 MTH103C X S1020 Rivera Jane CSC 15 S1010 MTH103C S1020 CSC201A B S1020 MTH101B A Student.stuId lastName firstName major credits Enroll.stuId classNumber grade S1001 Smith Tom History 90S1001 ART103A A S1001 Smith Tom History 90S1001 HST205A C S1001 Smith Tom History 90S1002 ART103A D S1001 Smith Tom History 90S1002 CSC201A F S1001 Smith Tom History 90S1002 MTH103C B S1001 Smith Tom History 90S1010 ART103A S1001 Smith Tom History 90S1010 MTH103C S1001 Smith Tom History 90S1020 CSC201A B S1001 Smith Tom History 90S1020 MTH101B A S1002 Chin Ann Math 36S1001 ART103A A … … … … …… … …
  13. 13. JOIN Example Faculty JOIN Class or Faculty |X| Class Faculty facId name department rank F101 Adams Art Professor F105 Tanaka CSC Instructor F110 Byrne Math Assistant F115 Smith History Associate F221 Smith CSC Professor Class classNumber facId schedule room ART103A F101 MWF9 H221 CSC201A F105 TuThF10 M110 CSC203A F105 MThF12 M110 HST205A F115 MWF11 H221 MTH101B F110 MTuTh9 H225 |X| MTH103C F110 MWF11 H225 facId name department rank class No schedule room F101 Adams Art Professor ART103A MWF9 H221 F105 Tanaka CSC Instructor CSC203A MThF12 M110 F105 Tanaka CSC Instructor CSC201A TuThF10 M110 F110 Byrne Math Assistant MTH103C MWF11 H225 F110 Byrne Math Assistant MTH101B MTuTh9 H225 F115 Smith History Associate HST205A MWF11 H221
  14. 14. NATURAL JOIN Operation • Requires two tables with common column(s) (at least with same domain) • combines the matching rows-rows of the two tables that have the same values in thetables that have the same values in the common column(s) • symbolized by |x| as in – [newTableName = ] Student |x| Enroll, or – Student JOIN Enroll [GIVING newTableName]
  15. 15. Equi-join and Natural Join • EQUIJOIN formed when θ is equality on column common to both tables (or comparable columns) • The common column appears twice in the• The common column appears twice in the result • Eliminating the repeated column in the result gives the NATURAL JOIN, usually called just JOIN A |x| B
  16. 16. NATURAL JOIN Example Student JOIN Enroll or Student |X| Enroll Equivalent to: Student TIMES Enroll GIVING Temp3 SELECT Temp3 WHERE Student.stuId = Enroll.stuId GIVING Temp4 PROJECT Temp4 OVER (Student.stuId, lastName, firstName, major, credits, classNumber, grade) Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 Enroll stuId classNumber grade S1001 ART103A A S1001 HST205A C S1002 ART103A D S1002 CSC201A F JOINS1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 S1002 CSC201A F S1002 MTH103C B S1010 ART103A S1010 MTH103C S1020 CSC201A B S1020 MTH101B A JOIN stuId lastName firstName major credits classNumber grade S1001 Smith Tom History 90HST205A C S1001 Smith Tom History 90ART103A A S1002 Chin Ann Math 36MTH103C B S1002 Chin Ann Math 36CSC201A F S1002 Chin Ann Math 36ART103A D S1010 Burns Edward Art 63MTH103C S1010 Burns Edward Art 63ART103A S1020 Rivera Jane CSC 15MTH101B A S1020 Rivera Jane CSC 15CSC201A B
  17. 17. EQUIJOIN Example Student EQUIJOIN Enroll (Produce 9 tuples) Equivalent to: Student TIMES Enroll GIVING Temp3 SELECT Temp3 WHERE Student.stuId = Enroll.stuId Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 Enroll stuId classNumber grade S1001 ART103A A S1001 HST205A C S1002 ART103A D S1002 CSC201A F EQUIJOIN Student X Enroll produces 63 tuples S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 S1002 CSC201A F S1002 MTH103C B S1010 ART103A S1010 MTH103C S1020 CSC201A B S1020 MTH101B A EQUIJOIN stuId lastName firstName major credits Enroll.stuId classNumber grade S1001 Smith Tom History 90S1001 HST205A C S1001 Smith Tom History 90S1001 ART103A A S1002 Chin Ann Math 36S1002 MTH103C B S1002 Chin Ann Math 36S1002 CSC201A F S1002 Chin Ann Math 36S1002 ART103A D S1010 Burns Edward Art 63S1010 MTH103C S1010 Burns Edward Art 63S1010 ART103A S1020 Rivera Jane CSC 15S1020 MTH101B A S1020 Rivera Jane CSC 15S1020 CSC201A B Final result Just 9 tuples
  18. 18. More Complicated Queries Find classes and grades of student Ann Chin SELECT Student WHERE lastName=‘Chin’ AND firstName=‘Ann’ GIVING Temp1 Temp1 JOIN Enroll GIVING Temp2 PROJECT Temp2 OVER (classNo, grade) GIVING Answer ΠclassNo,grade((σlastName=‘Chen’ ^ firstName=‘Ann’(Student))|X|Enroll)
  19. 19. More Complicated Queries (cont.) SELECT Student WHERE lastName=‘Chin’ AND firstName=‘Ann’ GIVING Temp1 Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 Temp1 stuId lastName firstName major credits S1002 Chin Ann Math 36 Temp1 JOIN Enroll GIVING Temp2 Enroll stuId classNumber grade S1001 ART103A A S1001 HST205A CS1001 HST205A C S1002 ART103A D S1002 CSC201A F S1002 MTH103C B S1010 ART103A S1010 MTH103C S1020 CSC201A B S1020 MTH101B A Temp2 stuId lastName firstName major credits classNumber grade S1002 Chin Ann Math 36ART103A D S1002 Chin Ann Math 36CSC201A F S1002 Chin Ann Math 36MTH103C B PROJECT Temp2 OVER (classNo, grade) GIVING Answer Answer classNumber grade ART103A D CSC201A F MTH103C B
  20. 20. More Complicated Queries (cont.) Since we only need the stuId from Temp1, we can do a PROJECT on Temp1 before we make the JOIN ΠclassNo,grade(ΠstuId(σlastName=‘Chen’ ^ firstName=‘Ann’(Student))|X|Enroll) A third way is:A third way is: JOIN Student, Enroll GIVING Tempa SELECT Tempa WHERE lastName=‘Chin’ AND firstName = ‘Ann’ GIVING Tempb PROJECT Tempb Over (classNo, grade) But it requires more work by the JOIN (produces 54 tubles), so is less efficient
  21. 21. Semijoins and Outerjoins • Left semijoin A|x B is formed by finding A|x|B and projecting result onto attributes of A • Result is tuples of A that participate in the join • Right semijoin A x| B defined similarly; tuples of B that participate in join • Outerjoin is formed by adding to the join those tuples that• Outerjoin is formed by adding to the join those tuples that have no match, adding null values for the attributes from the other table e.g. A OUTER- EQUIJOIN B consists of the equijoin of A and B, supplemented by the unmatched tuples of A with null values for attributes of B and the unmatched tuples of B with null values for attributes of A • Can also form left outerjoin or right outerjoin
  22. 22. Semijoins Example Student LEFT-SEMIJOIN Enroll or Student |X Enroll Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 Enroll stuId classNumber grade S1001 ART103A A S1001 HST205A C S1002 ART103A D S1002 CSC201A F S1002 MTH103C B S1010 ART103A S1010 MTH103C S1020 CSC201A B S1020 MTH101B A |X stuId lastName firstName major S1001 Smith Tom History S1002 Chin Ann Math S1010 Burns Edward Art S1020 Rivera Jane CSC
  23. 23. Outerjoin Example Student OUTER-EQUIJOIN Faculty Compare Student.lastName with Faculty.name Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 Faculty facId name department rank F101 Adams Art Professor F105 Tanaka CSC Instructor F110 Byrne Math Assistant F115 Smith History Associate F221 Smith CSC Professor OUTERJOIN S1020 Rivera Jane CSC 15 stuId lastName firstName major credits facId name department rank S1001 Smith Tom History 90 F221 Smith CSC Professor S1001 Smith Tom History 90 F115 Smith History Associate S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 F101 Adams Art Professor F105 Tanaka CSC Instructor F110 Byrne Math Assistant
  24. 24. Left-Outer-Equijoin Example Student LEFT-OUTER-EQUIJOIN Faculty Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 Faculty facId name department rank F101 Adams Art Professor F105 Tanaka CSC Instructor F110 Byrne Math Assistant F115 Smith History Associate F221 Smith CSC Professor LEFT-OUTER-EQUIJOIN stuId lastName firstName major credits facId name department rank S1001 Smith Tom History 90 F221 Smith CSC Professor S1001 Smith Tom History 90 F115 Smith History Associate S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15
  25. 25. Right-Outer-Equijoin Example Student RIGHT-OUTER-EQUIJOIN Faculty Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 S1005 Lee Perry History 3 S1010 Burns Edward Art 63 S1013 McCarthy Owen Math 0 S1015 Jones Mary Math 42 S1020 Rivera Jane CSC 15 Faculty facId name department rank F101 Adams Art Professor F105 Tanaka CSC Instructor F110 Byrne Math Assistant F115 Smith History Associate F221 Smith CSC Professor LEFT-OUTER-EQUIJOIN stuId lastName firstName major credits facId name department rank S1001 Smith Tom History 90 F221 Smith CSC Professor S1001 Smith Tom History 90 F115 Smith History Associate F101 Adams Art Professor F105 Tanaka CSC Instructor F110 Byrne Math Assistant
  26. 26. Division • Binary operator where entire structure of one table (divisor) is a portion of structure of the other (dividend) • Result is projection onto those attributes of• Result is projection onto those attributes of the dividend that are not in the divisor of the tuples of dividend that appear with all the rows of the divisor • It tells which values of those attributes appear with all the values of the divisor
  27. 27. Divide Example Club DIVIDED BY Stu or Club ÷÷÷÷ Stu Club ClubName StuNumb StuLastName Computing S1001 Smith Computing S1002 Chin Stu StuNumb StuLastName S1001 Smith S1002 Chin S1005 Lee Computing S1002 Chin Drama S1001 Smith Drama S1002 Chin Drama S1005 Lee Karate S1001 Smith Karate S1002 Chin Karate S1005 Lee Club DIVIDED BY Stu ClubName Drama Karate
  28. 28. Set Operations • Tables must be union compatible – have same basic structure – Have the same degree – Attributes in corresponding position have same– Attributes in corresponding position have same domain • Third column in first relation must have same domain as third column in second relation
  29. 29. Set Operations • A UNION B: set of tuples in either or both of A and B, written A ∪ B • A INTERSECTION B: set of tuples in both A and B simultaneously, wriƩen A ∩ BB simultaneously, wriƩen A ∩ B • Difference or A MINUS B: set of tuples in A but not in B, written A - B
  30. 30. Set Operations (cont.) • A UNION B: set of tuples in either or both of A and B, written A ∪ B – Remove duplicates MainFac FacID name department rank F101 Adams Art Processor F105 Tanaka CSC Instructor F221 Smith CSC Processor BarnchFac FacID name department rank F101 Adams Art Processor F110 Byre Math Assistant F115 Smith History Associate ∪ F221 Smith CSC Processor F115 Smith History Associate F221 Smith CSC Processor MainFac UNION BarnchFac FacID name department rank F101 Adams Art Processor F105 Tanaka CSC Instructor F110 Byre Math Assistant F115 Smith History Associate F221 Smith CSC Processor
  31. 31. Set Operations (cont.) • A INTERSECTION B: set of tuples in both A and B simultaneously, wriƩen A ∩ B MainFac FacID name department rank F101 Adams Art Processor F105 Tanaka CSC Instructor F221 Smith CSC Processor BranchFac FacID name department rank F101 Adams Art Processor F110 Byre Math Assistant F115 Smith History Associate ∩ F221 Smith CSC Processor F115 Smith History Associate F221 Smith CSC Processor MainFac INTERSECTION BranchFac FacID name department rank F101 Adams Art Processor F221 Smith CSC Processor
  32. 32. Set Operations (cont.) • Difference or A MINUS B: set of tuples in A but not in B, written A – B • MainFac - BranchFac MainFac FacID name department rank F101 Adams Art Processor F105 Tanaka CSC Instructor F221 Smith CSC Processor BranchFac FacID name department rank F101 Adams Art Processor F110 Byre Math Assistant F115 Smith History Associate F221 Smith CSC Processor MainFac MINUS BranchFac FacID name department rank F105 Tanaka CSC Instructor - F105 Tanaka CSC Instructor BranchFac - MainFac MainFac FacID name department rank F101 Adams Art Processor F105 Tanaka CSC Instructor F221 Smith CSC Processor BranchFac FacID name department rank F101 Adams Art Processor F110 Byre Math Assistant F115 Smith History Associate F221 Smith CSC Processor MainFac MINUS BranchFac FacID name department rank F110 Byre Math Assistant F115 Smith History Associate -
  33. 33. Relational Calculus • Formal non-procedural language • Two forms: tuple-oriented and domain- oriented • Based on predicate calculus• Based on predicate calculus
  34. 34. Tuple-oriented predicate calculus • Uses tuple variables, which take tuples of relations as values • Query has form {S  P(S)} – S is the tuple variable, stands for tuples of relation– S is the tuple variable, stands for tuples of relation – P(S) is a formula that describes S – Means “Find the set of all tuples, s, such that P(S) is true when S=s.” • Limit queries to safe expressions – test only finite number of possibilities
  35. 35. Example Tuple-oriented Relational Calculus Example 1: Find the names of all faculty members who are Professors in CSC Department {F.name | F ∈ Faculty ^ F.department = ‘CSC’ ^ F.rank = ‘Professor’} Example 2: Find the last names of all students enrolled in CSC201A {S.lastName | S ∈ Student ^ EXISTS E (E ∈ Enroll ^ E.stuId = S.stuId ^ E.classNo = ‘CSC201A)}E.classNo = ‘CSC201A)} Example 3: Find the first and last names of all students who are enrolled in at least one class that meets in room H221. {S.firstName, S.lastName | S ∈ Student ^ EXISTS E(E ∈ Enroll ^ E.stuId = S.stuId ^ EXISTS C (C ∈ Class ^ C.classNo = E.classNo ^ C.room = ‘H221’))}
  36. 36. Domain-oriented predicate calculus • Uses variables that take their values from domains • Query has form {<x1,x2,...,xn>  P(x1,x2,...,xm )} – x1,x2,...,xn are domain variables – P(x1,x2,...,xm) is a predicate– P(x1,x2,...,xm) is a predicate – n<=m – Means set of all domain variables x1,x2,...,xn for which predicate P(x1,x2,...,xm) is true • Predicate must be a formula • Often test for membership condition, <x,y,z >∈ X
  37. 37. Example Domain-oriented Relational Calculus • Example 1: Find the name of all faculty members who are professors in CSC Department – {LN | ∃ FI, DP, RK (<F1, LN, DP, RK> ∈ Faculty ^ RK = ‘Professor’ ^ DP = ‘CSC’)} • Example 2: Find the last names of all students enrolled in CSC201A. – {LN | ∃ SI, FN, MJ, CR (<SI, LN, FN, MJ, CR> ∈ Student ^ ∃ CN, GR (<SI, CN, GR> ∈ Enroll ^ CN = ‘CSC201A’))} • Example 3: Find the first and last names of all students who are enrolled in at least one class that meets in room H221.at least one class that meets in room H221. – {FN, LN | ∃ SI, MJ, CR (<SI, LN, FN, MJ, CR> ∈ Student ^ ∃ CN, GR (<SI, CN, GR> ∈ Enroll ^ ∃ FI, SH, RM (<CN, FI, SH, RM> ∈ Class ^ RM = ‘H221’)))} SI: stuId, LN: lastName, FN: firstName, MJ: major, CR: credits, CN: calssNo, FI: facId, SH: schedule, RM: room, DP: department, RK: rank, GR: grade
  38. 38. Views • External models in 3-level architecture are called external views • Relational views are slightly different • Relational view is constructed from existing (base) tablestables • View can be a window into a base table (subset) • View can contain data from more than one table • View can contain calculated data • Views hide portions of database from users • External model may have views and base tables
  39. 39. Views (cont.) • Original table – Student(stuId, lastName, firstName, ssn, major, credits) • Organize into two tables – PersonalStu(stuId, lastName, firstName, ssn) – AcademicStu(stuId, major, credits) • Create the original table as a view using a natural join• Create the original table as a view using a natural join of PersonalStu and AcademicStu • Restrict updates to views – View must contain primary key to be updatable – Views constructed from summary data are not updatable
  40. 40. Mapping ER to Relational Model • Each strong entity set becomes a table • Non-composite, single-valued attributes become attributes of table • Composite attributes: either make the composite a single attribute or use individual attributes for components, ignoring the composite • Multi-valued attributes: remove them to a new table along with the primary key of the original table; also keep key in original table • Weak entity sets become tables by adding primary key of owner entity• Weak entity sets become tables by adding primary key of owner entity • Binary Relationships: – 1:M-place primary key of 1 side in table of M side as foreign key – 1:1- make sure they are not the same entity. If not, use either key as foreign key in the other table – M:M-create a relationship table with primary keys of related entities, along with any relationship attributes • Ternary or higher degree relationships: construct relationship table of keys, along with any relationship attributes
  41. 41. ER Diagram Example Department deptCode office deptName EmploysChairsHasMajor Offers Textbook isbn title stuId lastName rank Faculty- Class- Textbook Teaches publisher Textbook author zip credit Student stuId major lastName firstName address number city state street Faculty facId firstName phone
  42. 42. ER to Relations Each strong entity set becomes a table • Table name same as entity name • Non-composite, single-valued attributes, represented by single oval, become attributes of the relationof the relation • Department(deptName, office, deptCode) Department deptCode office deptName
  43. 43. ER to Relations Composite attributes • An attribute is atomic, it cannot be composite • Two approaches – Make each ER attribute an attribute in the relation – Represent the total address with just one attribute in the relation • Cannot search of all students in particular zip• Cannot search of all students in particular zip • Student(stuId, lastName, firstName, major, credits, number, street, city, state, zip) • Student(stuId, lastName, firstName, major, credits, address)credit Student stuId major lastName firstName address number city state street zip
  44. 44. ER to Relations Multi-valued attributes • Remove them to a new table along with the primary key of the original table • Keep key in original table • Textbook(isbn, title, publisher) • Author(isbn, lastName, firstName) isbn • Student(stuId, lastName, firstName, major1, major2, credits, number, street, city, state, zip Author(isbn, lastName, firstName) Textbook isbn author title publisher zip credit Student stuId major lastName firstName address number city state street • Student(stuId, lastName, firstName, credits, number, street, city, state, zip Author(isbn, lastName, firstName) • StuMajor(stuId, major)
  45. 45. ER to Relations Multi-valued attributes • If multiple phone numbers for faculty • First approach, multiple attributes in one relation Faculty(facId, lastName, firstName, rank, phone, alternatePhone, street, city, state, zip) • Second approach, create a PhoneNumber relation • Can have primary number, alternate number, cell number, spouse number, home number, etc. • Faculty(facId, lastName, firstName, rank, street, city, state, zip) • PhoneNumber(facId, phoneNumber, typePhone)
  46. 46. ER to Relations Weak entity • Weak entity sets become tables by adding primary key of owner entity Faculty • Faculty(facId, lastName, firstName, rank, phone, alternatePhone, street, city, state, zip) Evaluation IsRated • Evaluation(facId, date, rater, rating)
  47. 47. ER to Relations Binary Relationships – 1:M-place primary key of 1 side in table of M side as foreign key – 1:1- make sure they are not the same entity. If not, use either key as foreign key in the other table – M:M-create a relationship table with primary keys of related entities, along with any relationship attributes Faculty • Faculty(facId, lastName, firstName, rank, phone, alternatePhone, street, city, state, zip) Class Teaches • Teaches(calssNo, facId) • Class(classNo, facId, schedule, room) • Don’t need Teaches relation 1 M
  48. 48. ER to Relations Ternary or higher degree relationships – Construct relationship table of keys, along with any relationship attributes Faculty • Faculty(facId, lastName, firstName, rank, phone, alternatePhone, street, city, state, zip) • Faculty-Class-Textbook(classNo, isbn, facId) • Choose classNo, isbn as primary key, because for each combination of classNo and isbn there is Class Faculty- Class- Textbook each combination of classNo and isbn there is only one faculty member1 M Textbook M
  49. 49. Convert ER to Relational Tables • Department(deptName, office, deptCode) • Student(stuId, lastName, firstName, major, credits, number, street, city, state, zip) • Student(stuId, lastName, firstName, major, credits, address) //cannot search of all students in particular zip//cannot search of all students in particular zip • Textbook(isbn, title, publisher) • Author(isbn, lastName, firstName) • Faculty(facId, office, deptCode, cellPhone, homePhone, officePhone) //may have other phone, eg, spousePhone • Faculty(facId, office, deptCode) • PhoneNumber(facId, number, typePhone)

×