Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
Próximos SlideShares
Carregando em…5
×

# Dbms ii mca-ch5-ch6-relational algebra-2013

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Entre para ver os comentários

### Dbms ii mca-ch5-ch6-relational algebra-2013

1. 1. Aruna Devi.C (DSCASC) 1 Data Base Management System [DBMS] Chapter 5 Unit III Relational Algebra
2. 2. Relational Algebra  Historically, the relational algebra and calculus were developed before the SQL language.  In fact, in some ways, SQL is based on concepts from both the algebra and the calculus.  The algebra defines a set of operations for the relational model.  A relational calculus expression creates a new relation. Aruna Devi.C (DSCASC)
3. 3.  The relational algebra is often considered to be an integral part of the relational data model. Its operations can be divided into two groups:  First group - set operations from mathematical set theory; these are applicable because each relation is defined to be a set of tuples in the formal relational model.  Set operations include UNION, INTERSECTION, SET DIFFERENCE, and CARTESIAN PRODUCT (also known as CROSS PRODUCT).  Second group - consists of operations developed specifically for relational databases—these include SELECT, PROJECT, and JOIN, among others. Relational Algebra (cont.,) Aruna Devi.C (DSCASC)
4. 4. Relational Algebra Six basic operators: Select: σ - Selects tuples that satisfy a given predicate Project: ∏ - Projects specified fields. Union: ∪ Intersection: ∩ Set difference: – Cartesian product: x Rename: ρ Division: ÷ Joints The operators take one or two relations as inputs and produce a new relation as a result. 4Aruna Devi.C (DSCASC)
5. 5. Unary Relational Operations SELECT and PROJECT The SELECT Operation:  The SELECT operation is used to choose a subset of the tuples from a relation that satisfies a selection condition.  The SELECT operation is denoted by σ <selection condition> (R) where the symbol σ (sigma) is used to denote the SELECT operator.  The selection condition is a Boolean expression (condition) specified on the attributes of relation R.  σ Dno=4(EMPLOYEE)  σ Salary>30000(EMPLOYEE) Where Dno is Department No.  <comparison op> is normally one of the operators {=, <, ≤, >, ≥, ≠}.  Clauses can be connected by the standard Boolean operators and, or, and not to form a general selection condition.
6. 6. Select Operation  Notation: σ p(r)  p is called the selection predicate  Defined as: σP(r) = {t | t ∈ r and p(t)} Where p is a formula connected by : ∧ (and), ∨ (or), ¬ (not) Example of selection: σ branch_name=“Perryridge”(account) 6Aruna Devi.C (DSCASC)
7. 7. Select Operation – Example 7 Relation r A B C D α α β β α β β β 1 5 12 23 7 7 3 10 σA=B ^ D > 5 (r) A B C D α β α β 1 23 7 10 Aruna Devi.C (DSCASC)
8. 8. The PROJECT Operation:  The PROJECT operation, selects certain columns from the table and discards the other columns.  The SELECT operation chooses some of the rows from the table while discarding other rows.  The general form of the PROJECT operation is π <attribute list> (R) where π (pi) is the symbol used to represent the PROJECT operation, and <attribute list> is the desired sublist of attributes from the attributes of relation R. For example, to list each employee’s first and last name and salary, we can use the PROJECT operation as follows: π Lname, Fname, Salary(EMPLOYEE) Unary Relational Operations SELECT and PROJECT
9. 9. Project Operation  Notation: where A1, A2 are attribute names and r is a relation name.  The result is defined as the relation of k columns obtained by erasing the columns that are not listed.  Duplicate rows removed from result, since relations are sets Eg: To eliminate the branch_name attribute of account ∏account_number,balance (account) 9 )(,,, 21 rkAAA  ∏ Aruna Devi.C (DSCASC)
10. 10. Project Operation – Example Relation r: 10 A B C α α β β 10 20 30 40 1 1 1 2 A C α α β β 1 1 1 2 = A C α β β 1 1 2 ∏A,C (r) Aruna Devi.C (DSCASC)
11. 11. SELECT and PROJECT Aruna Devi.C (DSCASC) Company Database
12. 12. SELECT and PROJECT  In SQL, the SELECT condition is typically specified in the WHERE clause of a query.  For example, the following operation: σDno=4 AND Salary>25000 (EMPLOYEE) would correspond to the following SQL query: SELECT * FROM EMPLOYEE WHERE Dno=4 AND Salary>25000; Aruna Devi.C (DSCASC) Company Database
13. 13. Sequences of Operations  To retrieve the first name, last name, and salary of all employees who work in department number 5, we must apply a SELECT and a PROJECT operation.  We can write a single relational algebra expression, also known as an in-line expression, as follows: π Fname, Lname, Salary(σDno=5(EMPLOYEE)) Next page shows the result of this in-line relational algebra expression. Aruna Devi.C (DSCASC)
14. 14. Sequences of Operations π Fname, Lname, Salary(σDno=5(EMPLOYEE)) Aruna Devi.C (DSCASC) Company Database
15. 15. Sequences of Operations  To rename the attributes in a relation, we simply list the new attribute names in parentheses, as in the following example: TEMP σDno=5(EMPLOYEE)← R(First_name, Last_name, Salary) πFname, Lname, Salary(TEMP)←  If no renaming is applied, the names of the attributes in the resulting relation of a SELECT operation are the same as those in the original relation and in the same order.  For a PROJECT operation with no renaming, the resulting relation has the same attribute names as those in the projection list and in the same order in which they appear in the list. Aruna Devi.C (DSCASC)
16. 16. Sequences of Operations TEMP ← σDno=5(EMPLOYEE) R(First_name, Last_name, Salary) ← πFname, Lname, Salary(TEMP) Aruna Devi.C (DSCASC) Company Database
17. 17. Rename Operation  Allows us to name, and therefore to refer to, the results of relational-algebra expressions.  Allows us to refer to a relation by more than one name. Example: ρ x (E) returns the expression E under the name X  If a relational-algebra expression E has arity n, then returns the result of expression E under the name X, and with the attributes renamed to A1, A2, …., An. 17 )(),...,,( 21 EnAAAx ρ Aruna Devi.C (DSCASC)
18. 18. Rename Operation  In SQL, a single query typically represents a complex relational algebra expression.  Renaming in SQL is accomplished by aliasing using AS, as in the following example:  SELECT E.Fname AS First_name, E.Lname AS Last_name, E.Salary AS Salary FROM EMPLOYEE AS E WHERE E.Dno=5, Aruna Devi.C (DSCASC) Company Database
19. 19. Union Operation – Example  Relations r, s: 19 r ∪ s: A B α α β 1 2 1 A B α β 2 3 r s A B α α β β 1 2 1 3 Aruna Devi.C (DSCASC)
20. 20. Relational Algebra Operations from Set Theory The UNION, INTERSECTION, and MINUS Operations: We can define the three operations UNION, INTERSECTION, and SET DIFFERENCE on two union-compatible relations R and S as follows: ■ UNION: The result of this operation, denoted by R U S, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated. ■ INTERSECTION: The result of this operation, denoted by R ∩ S, is a relation that includes all tuples that are in both R and S. ■ SET DIFFERENCE (or MINUS): The result of this operation, denoted by R – S, is a relation that includes all tuples that are in R but not in S. Aruna Devi.C (DSCASC)
21. 21. Union Operation  Notation: r ∪ s  Defined as: r ∪ s = {t | t ∈ r or t ∈ s}  For r ∪ s to be valid. 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible (example: 2nd column of r deals with the same type of values as does the 2nd column of s)  Example: to find all customers with either an account or a loan ∏customer_name (depositor) ∪ ∏customer_name (borrower) 21Aruna Devi.C (DSCASC)
22. 22. Set Difference Operation  Relations r, s: 22 r – s: A B α α β 1 2 1 A B α β 2 3 r s A B α β 1 1 Aruna Devi.C (DSCASC)
23. 23. Set Difference Operation Notation r – s Defined as: r – s = {t | t ∈ r and t ∉ s} Set differences must be taken between compatible relations. r and s must have the same arity attribute domains of r and s must be compatible 23Aruna Devi.C (DSCASC)
24. 24. Set-Intersection Operation Notation: r ∩ s Defined as: r ∩ s = { t | t ∈ r and t ∈ s } Assume: r, s have the same arity attributes of r and s are compatible Note: r ∩ s = r – (r – s) 24Aruna Devi.C (DSCASC)
25. 25. Set-Intersection Operation Relation r, s: r ∩ s 25 A B α α β 1 2 1 A B α β 2 3 r s A B α 2 Aruna Devi.C (DSCASC)
26. 26. The CARTESIAN PRODUCT (CROSS PRODUCT) Operation  This set operation produces a new element by combining every member (tuple) from one relation (set) with every member (tuple) from the other relation (set). In general, the result of R(A1, A2, ..., An) X S(B1, B2, ..., Bm) is a relation Q Aruna Devi.C (DSCASC)
27. 27. Cartesian-Product Operation 27 Relations r, s: r x s: A B α β 1 2 A B α α α α β β β β 1 1 1 1 2 2 2 2 C D α β β γ α β β γ 10 10 20 10 10 10 20 10 E a a b b a a b b C D α β β γ 10 10 20 10 E a a b br s Aruna Devi.C (DSCASC)
28. 28. Composition of Operations  Can build expressions using multiple operations  Example: σA=C(r x s)  r x s  σA=C(r x s) 28 A B α α α α β β β β 1 1 1 1 2 2 2 2 C D α β β γ α β β γ 10 10 20 10 10 10 20 10 E a a b b a a b b A B C D E α β β 1 2 2 α β β 10 10 20 a a b Aruna Devi.C (DSCASC)
29. 29. Binary Relational Operations: JOIN and DIVISION The JOIN Operation:  The JOIN operation, denoted by , is used to combine related tuples from two relations into single “longer” tuples.  The join operator allows the combination of two relations to form a single new relation.  The JOIN operation can be specified as a CARTESIAN PRODUCT operation followed by a SELECT operation. Aruna Devi.C (DSCASC)
30. 30. Natural-Join Operation Example: R = (A, B, C, D) S = (E, B, D) Result schema = (A, B, C, D, E) r s is defined as: ∏r.A,r.B,r.C,r.D,s.E (σr.B=s.B∧r.D=s.D (r x s)) 30 Notation: r s Aruna Devi.C (DSCASC)
31. 31. Natural Join Operation Relations r, s: 31 A B α β γ α δ 1 2 4 1 2 C D α γ β γ β a a b a b B 1 3 1 2 3 D a a a b b E α β γ δ ∈ r A B α α α α δ 1 1 1 1 2 C D α α γ γ β a a a a b E α γ α γ δ s r s Aruna Devi.C (DSCASC)
32. 32. Variations of JOIN: The EQUIJOIN and NATURAL JOIN  The only comparison operator used is =, is called an EQUIJOIN.  NATURAL JOIN requires that the two join attributes (or each pair of join attributes) have the same name in both relations.  The equality join condition specified on these two attributes requires the values to be identical in every tuple in the result.  NATURAL JOIN — denoted by *— was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition. Aruna Devi.C (DSCASC)
33. 33. COMPANY Relational database schema
34. 34. Join Operation Result of Join Operation: DEPT_MGR  DEPARTMENT Mgr_ssn= ssn EMPLOYEE
35. 35. NATURAL JOIN Result of two Natural Join: a) PROJ_DEPT PROJECT * DEPT b) DEPT_LOCS  DEPARTMENT * DEPT _LOCATIONS
36. 36. A Complete Set of Relational Algebra Operations Relational algebra operations {σ, π, U, ρ, –, x} is a complete set.  For example, the INTERSECTION operation can be expressed by using UNION and MINUS.  A JOIN operation can be specified as a CARTESIAN PRODUCT followed by a SELECT operation.  A NATURAL JOIN can be specified as a CARTESIAN PRODUCT preceded by RENAME and followed by SELECT and PROJECT operations. Aruna Devi.C (DSCASC)
37. 37. A Complete Set of Relational Algebra Operations (Cont.,)  An example is Retrieve the names of employees who work on all the projects that ‘John Smith’ works on.  First, retrieve the list of project numbers that ‘John Smith’ works on in the intermediate relation SMITH_PNOS: Aruna Devi.C (DSCASC)
38. 38. Division Operation  The DIVISION operation, denoted by ÷, is useful for a special kind of query that sometimes occurs in database applications.  An example is Retrieve the names of employees who work on all the projects that ‘John Smith’ works on. 1) First, retrieve the list of project numbers that ‘John Smith’ works on in the intermediate relation SMITH_PNOS: SMITH ← σFname=‘John’ AND Lname=‘Smith’ (EMPLOYEE) SMITH_PNOS ← πPno (WORKS_ON Essn=Ssn SMITH) 2) Next, create a relation that includes a tuple <Pno, Essn> whenever the employee whose Ssn is Essn works on the project whose number is Pno in the intermediate relation SSN_PNOS: SSN_PNOS ← πEssn, Pno (WORKS_ON) 38Aruna Devi.C (DSCASC) Company Database
39. 39. Division Operation 3) Finally, apply the DIVISION operation to the two relations, which gives the desired employees’ Social Security numbers: SSNS(Ssn) SSN_PNOS← ÷ SMITH_PNOS RESULT πFname, Lname (SSNS← * EMPLOYEE) Company Database
40. 40. Division Operation In general, the DIVISION operation is applied to two relations R(Z) ÷ S(X), where the attributes of R are a subset of the attributes of S. Example: 1) The table SSN_PNOS is from works on table. 2)The table Smith-Pnos is from which project smith work-on, that is from employee table and works-on table. 3) SSNS is the final table where check which all projects smith the same projects all the other employees should work on so only two employees work for both the projects. Aruna Devi.C (DSCASC)
41. 41. Operations of Relational Algebra
42. 42. Additional Relational Operations 1. Generalized Projection:  The generalized projection operation extends the projection operation by allowing functions of attributes to be included in the projection list.  The generalized form can be expressed as: πF1, F2, ..., Fn (R) where F1, F2, ..., Fn are functions over the attributes in relation R and may involve arithmetic operations and constant values.  This operation is helpful when developing reports where computed values have to be produced in the columns of a query result. Aruna Devi.C (DSCASC)
43. 43. Additional Relational Operations (Cont.,)  As an example, consider the relation EMPLOYEE (Ssn, Salary, Deduction, Years_service)  A report may be required to show Net Salary = Salary – Deduction, Bonus = 2000 * Years_service, and Tax = 0.25 * Salary.  Then a generalized projection combined with renaming may be used as follows: REPORT ρ(Ssn, Net_salary, Bonus, Tax)(πSsn, Salary – Deduction, 2000 * Years_service, 0.25 * Salary(EMPLOYEE))← Aruna Devi.C (DSCASC)
44. 44. 2. Aggregate Functions and Grouping:  Another type of request that cannot be expressed in the basic relational algebra is to specify mathematical aggregate functions on collections of values from the database.  Examples: Retrieving the average or total salary of all employees or the total number of employee tuples.  Common functions applied to collections of numeric values include SUM, AVERAGE, MAXIMUM, MINIMUM and COUNT.  Another common type of request involves grouping the tuples in a relation.  Example: Group EMPLOYEE tuples by Dno. Additional Relational Operations (Cont.,) Aruna Devi.C (DSCASC)
45. 45. Additional Relational Operations (Cont.,)  We can define an AGGREGATE FUNCTION operation, using the symbol .ℑ <grouping attributes> <function list> (ℑ R) Example: Aruna Devi.C (DSCASC)
46. 46. Additional Relational Operations (Cont.,) 3. Recursive Closure Operations: This operation is applied to a recursive relationship between tuples of the same type, such as the relationship between an employee and a supervisor. Aruna Devi.C (DSCASC)
47. 47. Additional Relational Operations (Cont.,) 4. OUTER JOIN Operations:  The JOIN operations described earlier match tuples that satisfy the join condition.  For example, for a NATURAL JOIN operation R * S, only tuples from R that have matching tuples in S—and vice versa—appear in the result.  Hence, tuples without a matching (or related) tuple are eliminated from the JOIN result.  Tuples with NULL values in the join attributes are also eliminated.  This type of join, where tuples with no match are eliminated, is known as an inner join.  There is an amount of loss in information. Aruna Devi.C (DSCASC)
48. 48. Additional Relational Operations (Cont.,) Different JOINs:  Join: Return rows when there is at least one match in both tables.  Left Outer Join: Return all rows from the first or left table, even if there are no matches in the right table. R S  Right Outer Join: Return all rows from the second or right table, even if there are no matches in the left table. R S  Full Outer Join: Return rows when there is a match in one of the tables. R S Aruna Devi.C (DSCASC)
49. 49. Examples of Queries in Relational Algebra
50. 50. Examples of Queries in Relational Algebra (Cont.,)  Query 1. Retrieve the name and address of all employees who work for the ‘Research’ department.  Query 2. For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date.  we first select the projects located in Stafford, then join them with their controlling departments, and then join the result with the department managers. Finally, we apply a project operation on the desired attributes.
51. 51.  Query 3. Find the names of employees who work on all the projects controlled by department number 5.  In this query, we first create a table DEPT5_PROJS that contains the project numbers of all projects controlled by department 5.  Then we create a table EMP_PROJ that holds (Ssn, Pno) tuples, and apply the division operation.  Notice that we renamed the attributes so that they will be correctly used in the division operation.  Finally,we join the result of the division, which holds only Ssn values, with the EMPLOYEE table to retrieve the desired attributes from EMPLOYEE. Examples of Queries in Relational Algebra (Cont.,) Aruna Devi.C (DSCASC)
52. 52.  Query 4. Make a list of project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as a manager of the department that controls the project.  In this query, we retrieved the project numbers for projects that involve an employee named Smith as a worker in SMITH_WORKER_PROJS. Then we retrieved the project numbers for projects that involve an employee named Smith as manager of the department that controls the project in SMITH_MGR_PROJS. Finally, we applied the UNION operation on SMITH_WORKER_PROJS and SMITH_MGR_PROJS. Examples of Queries in Relational Algebra (Cont.,) Aruna Devi.C (DSCASC)
53. 53. Other Example Queries  Find all loans of over \$1200 53 Find the loan number for each loan of an amount greater than \$1200 σamount > 1200 (loan) ∏loan_number (σamount > 1200 (loan))  Find the names of all customers who have a loan, an account, or both, from the bank Find the names of all customers who have a loan and an account at bank. ∏customer_name (borrower) ∪ ∏customer_name (depositor) ∏customer_name (borrower) ∩ ∏customer_name (depositor)
54. 54. Example Queries 1) Find the names of all customers who have a loan at the Perryridge branch. 54 2) Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank. ∏customer_name (σbranch_name = “Perryridge” (σborrower.loan_number = loan.loan_number(borrower x loan))) – ∏customer_name(depositor) ∏customer_name (σbranch_name=“Perryridge” (σborrower.loan_number = loan.loan_number(borrower x loan))) Aruna Devi.C (DSCASC)
55. 55. Example Queries  Find the names of all customers who have a loan at the Perryridge branch. 55 Query 2 ∏customer_name(σloan.loan_number = borrower.loan_number ( (σbranch_name = “Perryridge” (loan)) x borrower)) Query 1 ∏customer_name (σbranch_name = “Perryridge” ( σborrower.loan_number = loan.loan_number (borrower x loan))) Aruna Devi.C (DSCASC)
56. 56. Relational Database Design Using ER-to-Relational Mapping Chapter 6
57. 57. Step 1: Mapping of Regular Entity Types. Step 2: Mapping of Weak Entity Types. Step 3: Mapping of Binary 1:1 Relationship Types. Step 4: Mapping of Binary 1:N Relationship Types. Step 5: Mapping of Binary M:N Relationship Types. Step 6: Mapping of Multi valued Attributes. Step 7: Mapping of N-ary Relationship Types. Relational Database Design Using ER-to-Relational Mapping (Cont.,)
58. 58. Relational Database Design Using ER-to-Relational Mapping (Cont.,) Step 1: Mapping of Regular Entity Types:  For each regular (strong) entity type E in the ER schema, create a relation R that includes all the simple attributes of E.  Include only the simple component attributes of a composite attribute.  Choose one of the key attributes of E as the primary key for R.  If the chosen key of E is a composite, then the set of simple attributes that form it will together form the primary key of R.  If multiple keys were identified for E during the conceptual design, the information describing the attributes that form each additional key is kept in order to specify secondary (unique) keys of relation R.  We create the relations EMPLOYEE, DEPARTMENT, and PROJECT in Figure 2 to correspond to the regular entity types EMPLOYEE, DEPARTMENT, and PROJECT in Figure 1.
59. 59. Relational Database Design Using ER-to-Relational Mapping (Cont.,)  The foreign key and relationship attributes, if any, are not included yet; they will be added during subsequent steps.  Example, we choose Ssn, Dnumber, and Pnumber as primary keys for the relations EMPLOYEE, DEPARTMENT, and PROJECT, respectively.  The relations that are created from the mapping of entity types are sometimes called entity relations because each tuple represents an entity instance.
60. 60. Relational Database Design Using ER-to-Relational Mapping ER-to-Relational Mapping Algorithm:
61. 61. Relational Database Design Using ER-to-Relational Mapping (Cont.,)
62. 62. Relational Database Design Using ER-to-Relational Mapping (Cont.,) Step 2: Mapping of Weak Entity Types:  For each weak entity type W in the ER schema with owner entity type E, create a relation R and include all simple attributes (or simple components of composite attributes) of W as attributes of R.  In addition, include as foreign key attributes of R, the primary key attribute(s) of the relation(s) that correspond to the owner entity type(s); this takes care of mapping the identifying relationship type of W.  The primary key of R is the combination of the primary key(s) of the owner(s) and the partial key of the weak entity type W, if any. Aruna Devi.C (DSCASC)
63. 63. Relational Database Design Using ER-to-Relational Mapping (Cont.,)  We create the relation DEPENDENT in this step to correspond to the weak entity type DEPENDENT.  The primary key of the DEPENDENT relation is the combination {Essn, Dependent_name}, because Dependent_name is the partial key of DEPENDENT. Aruna Devi.C (DSCASC)
64. 64. Relational Database Design Using ER-to-Relational Mapping (Cont.,) Step 3: Mapping of Binary 1:1 Relationship Types:  For each binary 1:1 relationship type R in the ER schema, identify the relations S and T that correspond to the entity types participating in R.  There are three possible approaches: (a) the foreign key approach, (b) the merged relationship approach, and (c) the cross reference or relationship relation approach. a. Foreign key approach:  Choose one of the relations—S, say—and include as a foreign key in S the primary key of T.  We include the primary key of the EMPLOYEE relation as foreign key in the DEPARTMENT relation and rename it Mgr_ssn. Aruna Devi.C (DSCASC)
65. 65. Relational Database Design Using ER-to-Relational Mapping (Cont.,) b. Merged relation approach:  An alternative mapping of a 1:1 relationship type is to merge the two entity types and the relationship into a single relation. c. Cross-reference or relationship relation approach:  The third option is to set up a third relation R for the purpose of cross- referencing the primary keys of the two relations S and T representing the entity types. Aruna Devi.C (DSCASC)
66. 66. Relational Database Design Using ER-to-Relational Mapping (Cont.,) Step 4: Mapping of Binary 1:N Relationship Types:  For each regular binary 1:N relationship type R, identify the relation S that represents the participating entity type at the N-side of the relationship type.  Include any simple attributes (or simple components of composite attributes) of the 1:N relationship type as attributes of S.  Example, we now map the 1:N relationship types WORKS_FOR, CONTROLS, and SUPERVISION from Figure 1.  For WORKS_FOR we include the primary key Dnumber of the DEPARTMENT relation as foreign key in the EMPLOYEE relation and call it Dno.  For SUPERVISION we include the primary key of the EMPLOYEE relation as foreign key in the EMPLOYEE relation itself—because the relationship is recursive— and call it Super_ssn.
67. 67. Relational Database Design Using ER-to-Relational Mapping (Cont.,) Step 5: Mapping of Binary M:N Relationship Types:  For each binary M:N relationship type R, create a new relation S to represent R.  Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types; their combination will form the primary key of S.  Example, we map the M:N relationship type WORKS_ON from Figure 1 by creating the relation WORKS_ON in Figure 2.  We include the primary keys of the PROJECT and EMPLOYEE relations as foreign keys in WORKS_ON and rename them Pno and Essn, respectively.
68. 68. Relational Database Design Using ER-to-Relational Mapping (Cont.,) Step 6: Mapping of Multi valued Attributes:  For each multi valued attribute A, create a new relation R.  This relation R will include an attribute corresponding to A, plus the primary key attribute K—as a foreign key in R—of the relation that represents the entity type or relationship type that has A as a multi valued attribute.  The primary key of R is the combination of A and K.  Example, we create a relation DEPT_LOCATIONS. Aruna Devi.C (DSCASC)
69. 69. Relational Database Design Using ER-to-Relational Mapping (Cont.,)  The attribute Dlocation represents the multivalued attribute LOCATIONS of DEPARTMENT, while Dnumber—as foreign key—represents the primary key of the DEPARTMENT relation.  The primary key of DEPT_LOCATIONS is the combination of {Dnumber, Dlocation}. Aruna Devi.C (DSCASC)
70. 70. Relational Database Design Using ER-to-Relational Mapping (Cont.,) Step 7: Mapping of N-ary Relationship Types:  For each n-ary relationship type R, where n > 2, create a new relation S to represent R.  Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types.  The primary key of S is usually a combination of all the foreign keys that reference the relations representing the participating entity types. Aruna Devi.C (DSCASC)
71. 71. Summary of Mapping constructs and constraints Table : Correspondence between ER and Relational Models ER Model Relational Model Entity type “Entity” relation 1:1 or 1:N relationship type Foreign key (or “relationship” relation) M:N relationship type “Relationship” relation and two foreign keys n-ary relationship type “Relationship” relation and n foreign keys Simple attribute Attribute Composite attribute Set of simple component attributes Multivalued attribute Relation and foreign key Value set Domain Key attribute Primary (or secondary) key Aruna Devi.C (DSCASC)